【问题标题】:Swashbuckle adding 200 OK response automatically to generated Swagger fileSwashbuckle 自动向生成的 Swagger 文件添加 200 OK 响应
【发布时间】:2016-06-14 13:22:27
【问题描述】:

我正在我的 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?这令人困惑,因为它不是有效的响应。

感谢您的建议。

【问题讨论】:

    标签: c# swagger swashbuckle


    【解决方案1】:

    您可以通过使用SwaggerResponseRemoveDefaults 属性装饰方法来删除默认响应(200 OK)。

    【讨论】:

    • 有什么方法可以全局添加而不是装饰我的控制器中的每个方法?
    • 该属性也可以应用于控制器。要全局添加此行为,您必须创建自己的 IDocumentFilter
    • 如果有人现在看到这个SwaggerResponseRemoveDefaults 不再在库中。您必须将 [ProducesStatus(204)] 显式添加到任何 NoContent 以抑制 swagger 添加的默认 200
    【解决方案2】:

    正如 vampiire 在他们的评论中指出的那样,SwaggerResponseRemoveDefaults 不再出现在 Swashbuckle 中。现在实现这一点的方法是在方法中同时包含 &lt;response&gt; 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 响应。它取自 Swashbuckle 5.5.0 和 ASP.NET Core 3.1 上的 Microsoft's Swashbuckle documentation

    【讨论】:

      【解决方案3】:
              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");
                  }
              }
          }
      
         }
      

      【讨论】:

      • 有用,但即使您有默认的 ///&lt;response code="200"&gt;Success&lt;/response&gt; 也将其删除,这很常见。为确保它不被删除,您需要使用“成功”以外的值。没有其他方法或解决方法吗?我很惊讶 Swashbuckle 会这样做。
      猜你喜欢
      • 2020-06-08
      • 1970-01-01
      • 2021-04-13
      • 2019-05-06
      • 1970-01-01
      • 1970-01-01
      • 2020-10-30
      • 2017-04-03
      • 1970-01-01
      相关资源
      最近更新 更多