Search code examples
asp.nettypescript.net-coreswagger

How to create swagger documentation for dynamic request/response


We have an API application which is driven dynamically based on SQL defined metadata. This API app is a reporting application and the report is driven by what kind of request is passed into the API. This means if JSON request is passed like this - ex 1

 {
    "Field1": "Value1"
    "GroupBy": ["GroupByValue1", "GroupByValue2"]
    }

Then it gives below resultset -

{
"GroupByValue1": "SomeValue1"
"GroupByValue2": "SomeValue2"
... other fields based on GroupBy1 and GroupBy2
}

ex 2

{
"Field1": "Value1"
"GroupBy": ["GroupByValue3"]
}

Then it gives below resultset -

{
"GroupByValue3": "SomeValue1"
... other fields based on GroupBy3
}

So the mapping of request v/s fields for that request is defined in the SQL database. And we need to generate swagger documentation for this kind of dynamic requests. So my question is since we use swashbuckle for swagger documentation, we have to give it specific resultset based on the request being passed. Now we have APIs to give us those request/resultset relationship, but is there a way to generate documentation completely dynamically based on this API (using c# code or typescript) that gives us request/resultset relationship.


Solution

  • You can create dynamic Swagger documentation using IDocumentFilter. We also use dynamic SQL metadata at this example.