How to document body parameter using Swashbuckle and .net core

Viewed 2075

We are using Swashbuckle.AspNetCore Version="5.6.3" to generate swagger (v2) documents for our .Net core 3.1 web service. In .Net core there are 2 ways of specifying body parameters.

1. public void someActionMethod (Guid id, [FromBody] item){...}
2. public void SomeActionMethod (Guid id) {
              var item = Request.Body;
   }

We are using second option for body parameters using APIController. In that swashbuckle is unable infer the required body parameter and is missing from the documentation. Can anyone please point if there is a way to specify and generate the documentation for the body parameter without using [FromBody]? It is an existing API so I want to avoid a change to the signature just for documentation purposes.

1 Answers

please find the solution for dotnet core 3.1 @ https://github.com/domaindrivendev/Swashbuckle.AspNetCore if you configure the newtonsoft json formatter, the api project should install the Swashbuckle.AspNetCore.Newtonsoft

 public void ConfigureServices(IServiceCollection services)
    {
         services.AddControllers().AddNewtonsoftJson(); //Configure Newtonsoft json formatter

            services.AddSwaggerGen(c =>
        {
             c.SwaggerDoc("test", new OpenApiInfo { Title = "test  API Service", Version = "v2.0.1"});

            c.AddSecurityDefinition("oauth2", new OpenApiSecurityScheme
            {
                Description = "Access Token Authentication. Example: \"bearer {token}\"",
                In = ParameterLocation.Header,
                Name = "Authorization",
                Type = SecuritySchemeType.ApiKey
            });
       
            c.OperationFilter<SecurityRequirementsOperationFilter>();                             
        });

        services.AddSwaggerGenNewtonsoftSupport(); //Add swagger support for newtonsoft json formatter
    }
    
     public void Configure(IApplicationBuilder app, IWebHostEnvironment e)
    {   
           app.UseSwagger();
      
        app.UseSwaggerUI(c =>
        {
            c.SwaggerEndpoint("/swagger/test/swagger.json", "Test  API v1");
           // c.DefaultModelsExpandDepth(-1);
           // c.DocExpansion(DocExpansion.None);
           // c.EnableFilter();
           // c.EnableDeepLinking();                
        });
    }
Related