Swashbuckle 自动向生成的 Swagger 文件添加 200 OK 响应
Swashbuckle adding 200 OK response automatically to generated Swagger file
我正在我的 WebApi 2 项目中使用 Swashbuckle 构建 swagger 文档。
我对方法的定义如下:
[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);
}
但是生成的 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
有没有办法去掉那个 200 OK?令人困惑,因为它不是有效的回复。
感谢您的建议。
您可以通过使用 SwaggerResponseRemoveDefaults
属性修饰方法来删除默认响应 (200 OK)。
正如 vampiire 在他们的评论中指出的那样,SwaggerResponseRemoveDefaults
不再出现在 Swashbuckle 中。现在实现这一点的方法是在方法中包含一个 <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)
{
...
}
这将删除默认的 200 响应。它取自 Microsoft's Swashbuckle documentation on Swashbuckle 5.5.0 和 ASP.NET Core 3.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");
}
}
}
}
我正在我的 WebApi 2 项目中使用 Swashbuckle 构建 swagger 文档。
我对方法的定义如下:
[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);
}
但是生成的 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
有没有办法去掉那个 200 OK?令人困惑,因为它不是有效的回复。
感谢您的建议。
您可以通过使用 SwaggerResponseRemoveDefaults
属性修饰方法来删除默认响应 (200 OK)。
正如 vampiire 在他们的评论中指出的那样,SwaggerResponseRemoveDefaults
不再出现在 Swashbuckle 中。现在实现这一点的方法是在方法中包含一个 <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)
{
...
}
这将删除默认的 200 响应。它取自 Microsoft's Swashbuckle documentation on Swashbuckle 5.5.0 和 ASP.NET Core 3.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");
}
}
}
}