Ciao,
I'm working on a asp.net web api core (target framework .NET Core 2.1). I'm documenting my API using Swagger specifications. I chose to use Swashbuckle.AspNetCore library.
I have one simple create action like the following:
/// <summary>
/// My summary
/// </summary>
/// <remarks>My remarks</remarks>
/// <param name="value">value</param>
/// <response code="201">Created</response>
/// <response code="400">Data not valid</response>
/// <response code="500">Internal Server Error</response>
[HttpPost]
public IActionResult Post([FromBody, BindRequired] EntityDto value)
{
...
}
The problem is that my generated swagger.json automatically created a "200 response". But because my action only creates entities it only returns 201 response. This is the following json fragment:
{
...
"responses": {
"200": {
"description": "Success"
},
"201": {
"description": "Created"
},
"400": {
"description": "Data not valid"
},
"500": {
"description": "Internal Server Error"
}
}
...
}
Surfing on internet I've found SwaggerResponseRemoveDefaults attribute but seems that it is only supported in Full Framework projects.
How can I remove 200 response?
AspNetCore. Swagger: a Swagger object model and middleware to expose SwaggerDocument objects as JSON endpoints. Swashbuckle. AspNetCore. SwaggerGen: a Swagger generator that builds SwaggerDocument objects directly from your routes, controllers, and models.
The {documentName} refers to the name you specify in the AddSwaggerGen() method. The following code uses myapi as the name for a swagger document. builder.Services.AddSwaggerGen(options => options.SwaggerDoc("myapi", new OpenApiInfo { Title = "My API", Version = "v1" }) );
SwaggerOperation is a useful attribute where you can set the summary, description, id, and the tags of an individual request/route.
Getting Started. Install the standard Nuget package into your ASP.NET Core application. In the ConfigureServices method of Startup. cs , register the Swagger generator, defining one or more Swagger documents.
In order not to include default response code into swagger, you can declare possible return codes
/// <summary>
/// My summary
/// </summary>
/// <remarks>My remarks</remarks>
/// <param name="value">value</param>
/// <returns>A newly created TodoItem</returns>
/// <response code="201">Created</response>
/// <response code="400">Data not valid</response>
/// <response code="500">Internal Server Error</response>
[HttpPost]
[ProducesResponseType(201)]
[ProducesResponseType(400)]
[ProducesResponseType(500)]
public void Post([FromBody] string value)
{
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With