13

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?

ilMattion
  • 1,841
  • 2
  • 24
  • 47

1 Answers1

14

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)
    {
    }
Igor
  • 198
  • 1
  • 11
  • 2
    That doesn't seem to work for me. I always get a default set of response codes regardless of the ProducesResponseType attributes that I use on the endpoint – Myles J Feb 21 '20 at 09:52
  • @MylesJ. you need to add the [ProducesResponseType(201)] [ProducesResponseType(400)] [ProducesResponseType(500)] in the controller method, have you tried this? – Ray Apr 10 '20 at 06:37