Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swashbuckle adding 200 OK response automatically to generated Swagger file

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.

like image 306
Václav Holuša Avatar asked Jun 14 '16 13:06

Václav Holuša


People also ask

What is SwaggerResponse?

Reads a struct decorated with swagger:response and uses that information to fill up the headers and the schema for a response. A swagger:route can specify a response name for a status code and then the matching response will be used for that operation in the swagger definition.

How do you get swagger response time?

Using swagger: In Chrome, open the tools (F12). Bring up the network tab and watch the request. Compare the time instant when the request is completed and the page is displayed.

What is SwaggerResponse C#?

SwaggerResponse is an attribute where you can set for each status code a description. Example, how to set SwaggerResponse for different HTTP status codes is shown below: Below image shows how SwaggerResponse for different status codes will be displayed in swagger UI documentation: SWAGGER RESPONSE FOR DIFF.

What is swagger and swashbuckle?

Introduction. Swashbuckle is an open source project for generating Swagger documents for Web APIs that are built with ASP.NET Core. There are three core components: AspNetCore. SwaggerGen - provides the functionality to generate JSON Swagger documents that describe the objects, methods, return types, etc.


1 Answers

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

   }
like image 169
Marius Avatar answered Oct 21 '22 00:10

Marius