[英]Swashbuckle adding 200 OK response automatically to generated Swagger file
I am building swagger docs using Swashbuckle in my WebApi 2 project.我正在我的 WebApi 2 项目中使用 Swashbuckle 构建 swagger 文档。
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.然而,生成的 Swagger 文件也包含 HTTP 200 OK,尽管它没有在任何地方指定。
/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?有没有办法摆脱那 200 OK? It's confusing as it's not a valid response.这令人困惑,因为它不是有效的响应。
Thanks for suggestions.感谢您的建议。
您可以通过使用SwaggerResponseRemoveDefaults
属性装饰方法来删除默认响应 (200 OK)。
As vampiire points out in their comment, SwaggerResponseRemoveDefaults
is no longer in Swashbuckle.正如吸血鬼在他们的评论中指出的那样, SwaggerResponseRemoveDefaults
不再在 Swashbuckle 中。 The way to achieve this now is to include both a <response>
XML-doc and a [ProducesResponseType()]
attribute to the method:现在实现这一点的方法是在方法中包含一个<response>
XML-doc和一个[ProducesResponseType()]
属性:
/// ...
/// <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.这将删除默认的 200 响应。 It's taken from Microsoft's Swashbuckle documentation on Swashbuckle 5.5.0 and ASP.NET Core 3.1它取自微软关于 Swashbuckle 5.5.0 和 ASP.NET Core 3.1 的 Swashbuckle 文档
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");
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.