Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alter parameter name for Swagger JSON generated through Swashbuckle in .NET Web API 2

Goal: I am designing a REST API that allows user to pass parameters on the query string for an HTTP GET request. Something like

http://fake.api.com/search?api-key=1235&term=car&rows=10

Implementation: on the server-side I have a model binder that takes those 3 parameters - api-key, term, and rows and converts them to a C# object so that my controller action method doesn't have to parse the query string. So the controller action method signature looks something like

public IHttpActionResult Get(RequestObject request)

Problem: what I am running into with Swagger is when it generates the documentation and the test harness, it is listing the input parameters as request.api-key, request.term, and request.rows. This is because JSON it is getting from the server is prefixing object name to these values. So when you try to use Swagger UI to do an HTTP GET request, it builds the URL like

http://fake.api.com/search?request.api-key=1235&request.term=car&request.rows=10

Which is an incorrect request for the server. Now I can easily make my model binder smarter to just ignore "request." part, but that seems like a backward way of fixing this problem.

Question: how can I customize JSON generated by Swashbuckle -> Swagger on the server side so that parameter names for this particular object aren't prefixed with the object name? I did look into both ISchemaFilter and IDocumentFilter that Swashbuckle extensibility API exposes, but I am not seeing a clean way to do this. May be it is possible, but an example would help in that case.

like image 602
codelove Avatar asked Jul 20 '15 01:07

codelove


People also ask

How do I create a Swagger API using Swashbuckle?

The API is created using ASP.NET Core with Swashbuckle. Add the required Nuget packages to the project, set the GenerateDocumentationFile element to true and also add the NoWarn element, if all the C# code is not documented. In the Startup class, add the Swagger configuration in the ConfigureServices method.

How to generate Swagger metadata dynamically?

Generating Swagger metadata manually (JSON or YAML file) can be a tedious work if you have to write it manually. However, you can automate API discovery of ASP.NET Web API services by using the Swashbuckle NuGet package to dynamically generate Swagger API metadata.

Does Swagger have a client generator for web APIs?

Once you have a Web API that can describe itself in Swagger, you've opened the treasure chest of Swagger-based tools including a client generator that can be targeted to a wide range of popular platforms. See swagger-codegen for more details. Swashbuckle Core Features: Auto-generated Swagger 2.0

How to automate API discovery with Swagger in NuGet?

However, you can automate API discovery of ASP.NET Web API services by using the Swashbuckle NuGet package to dynamically generate Swagger API metadata. Swashbuckle is seamlessly and automatically adds Swagger metadata to ASP.NET Web Api projects.


1 Answers

Found a solution, which turned out to be the one thing I ignored - IOperationFilter. See https://github.com/domaindrivendev/Swashbuckle/issues/128 for details.

Update

Here is the code for the solution mentioned in the comment

public class IgnorePrefixParamsNameBeforeDot : IOperationFilter
{
    public void Apply(Operation operation, SchemaRegistry schemaRegistry, ApiDescription apiDescription)
    {
        if (operation.parameters != null)
        {
            foreach (Parameter param in operation.parameters.Where(p => p.name.Contains('.')))
            {
                param.name = param.name.SplitWithoutEmpty('.').Last();
            }
        }
    }
}
like image 114
codelove Avatar answered Oct 20 '22 17:10

codelove