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