2

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.

Scooby
  • 635
  • 3
  • 9
  • 21
  • I believe I need to do as suggested [here](https://stackoverflow.com/questions/41141137/how-can-i-tell-swashbuckle-that-the-body-content-is-required). – Scooby Nov 10 '20 at 00:11
  • I ended up implementing solution described here - https://stackoverflow.com/questions/41141137/how-can-i-tell-swashbuckle-that-the-body-content-is-required – Scooby Jan 28 '21 at 00:28

1 Answers1

-2

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();                
        });
    }
giri-webdev
  • 525
  • 10
  • 20
  • 1
    Thanks for the reply, I am using this services.AddSwaggerGenNewtonsoftSupport(); in my startup. However the API parameter which is read from the request body is not documented. e.g. `/// The ID. /// The image data, in the form of a byte array. public async Task MethodAsync(Guid tenantId, CancellationToken cancellationToken) { byte[] imageData = await ReadRequestBodyAsync(Request.Body, cancellationToken); }` – Scooby Nov 09 '20 at 23:27