14

I am building swagger docs using Swashbuckle in my WebApi 2 project.

I have the following definition of the method:

[HttpPost]
[ResponseType(typeof(Reservation))]
[Route("reservations")]
[SwaggerResponse(HttpStatusCode.Created, Type = typeof(Reservation))]
[SwaggerResponse(HttpStatusCode.BadRequest) ]
[SwaggerResponse(HttpStatusCode.Conflict)]
[SwaggerResponse(HttpStatusCode.NotFound)]
[SwaggerResponse(HttpStatusCode.InternalServerError)]        
public async Task<HttpResponseMessage> ReserveTickets([FromBody] ReserveTicketsRequest reserveTicketRequest)
{
    // ...
    return Request.CreateResponse(HttpStatusCode.Created, response);
}

However the generated Swagger file contains HTTP 200 OK as well, although it's not specified anywhere.

/reservations: 
  post: 
    tags: 
      - "Booking"
    operationId: "Booking_ReserveTickets"
    consumes: 
      - "application/json"
      - "text/json"
    produces: 
      - "application/json"
      - "text/json"
    parameters: 
      - 
        name: "reserveTicketRequest"
        in: "body"
        required: true
        schema: 
          $ref: "#/definitions/ReserveTicketsRequest"
    responses: 
      200: 
        description: "OK"
        schema: 
          $ref: "#/definitions/Reservation"
      201: 
        description: "Created"
        schema: 
          $ref: "#/definitions/Reservation"
      400: 
        description: "BadRequest"
      404: 
        description: "NotFound"
      409: 
        description: "Conflict"
      500: 
        description: "InternalServerError"
    deprecated: false

Is there a way to get rid of that 200 OK? It's confusing as it's not a valid response.

Thanks for suggestions.

venerik
  • 5,766
  • 2
  • 33
  • 43
Václav Holuša
  • 311
  • 3
  • 14

3 Answers3

13

You can remove the default response (200 OK) by decorating the method with the SwaggerResponseRemoveDefaults attribute.

Jaguar
  • 5,929
  • 34
  • 48
venerik
  • 5,766
  • 2
  • 33
  • 43
  • Is there any way to add this globally rather than decorate each method in my controllers? – michaelmsm89 Feb 25 '17 at 01:28
  • The attribute can be applied to a controller too. To add this behaviour globally you have to create your own `IDocumentFilter` – venerik Feb 27 '17 at 11:35
  • 4
    if anyone sees this now `SwaggerResponseRemoveDefaults` is no longer in the lib. you have to explicitly add [ProducesStatus(204)] to any NoContent to suppress the default 200 that swagger adds – vampiire Mar 26 '20 at 21:05
3

As vampiire points out in their comment, SwaggerResponseRemoveDefaults is no longer in Swashbuckle. The way to achieve this now is to include both a <response> XML-doc and a [ProducesResponseType()] attribute to the method:

/// ...
/// <response code="201">Returns the newly reserved tickets</response>
/// <response code="400">If the input parameters are invalid</response>
/// ...
[HttpPost]
[Route("reservations")]
[ProducesResponseType(StatusCodes.Status201Created)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
...
public async Task<HttpResponseMessage> ReserveTickets([FromBody] ReserveTicketsRequest reserveTicketRequest)
{
    ...
}

This will remove the default 200 response. It's taken from Microsoft's Swashbuckle documentation on Swashbuckle 5.5.0 and ASP.NET Core 3.1

jbb
  • 346
  • 3
  • 9
1
        services.AddSwaggerGen(c =>
        {
            c.OperationFilter<Api.Swagger.RemoveDefaultResponse>();
        });

   public class RemoveDefaultResponse : IOperationFilter
   {

    public void Apply(OpenApiOperation operation, OperationFilterContext context)
    {
        if (operation.Responses.TryGetValue("200", out var response)) {
            if (response.Description == "Success") {
                operation.Responses.Remove("200");
            }
        }
    }

   }
Marius
  • 9,208
  • 8
  • 50
  • 73
  • Useful, but removes it even if you have a default `///Success` which is common. To ensure it isn't removed you need to use a value other than "Success". Is there no other way or fix? I'm surprised Swashbuckle does this. – lonix Feb 22 '22 at 03:30