Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I hide response code 200 with Swashbuckle.AspNetCore?

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?

like image 387
ilMattion Avatar asked Oct 19 '18 09:10

ilMattion


People also ask

What is swashbuckle AspNetCore SwaggerGen?

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.

What is documentName in swagger?

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" }) );

What is SwaggerOperation attribute?

SwaggerOperation is a useful attribute where you can set the summary, description, id, and the tags of an individual request/route.

How do I install swashbuckle AspNetCore code in Visual Studio?

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.


1 Answers

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)
    {
    }
like image 94
Igor Kovalev Avatar answered Sep 18 '22 03:09

Igor Kovalev