简体   繁体   English

Swashbuckle 自动向生成的 Swagger 文件添加 200 OK 响应

[英]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.

相关问题 Swashbuckle SwaggerResponseRemoveDefaults 属性仍在向 Swagger 添加 200 成功响应 - Swashbuckle SwaggerResponseRemoveDefaults attribute still adding 200 Success response to Swagger Swashbuckle生成的Swagger有错误 - Swashbuckle generated Swagger has errors 使用Swashbuckle Swagger上传文件 - Upload a file on with Swashbuckle Swagger 在 Swashbuckle.AspNetCore 中,Swagger JSON 和使用 Results.Ok 时生成的 UI 没有内容类型 - In Swashbuckle.AspNetCore, Swagger JSON and UI generated without content type when Results.Ok is used Swashbuckle - 返回响应的大摇大摆的文档? - Swashbuckle - swagger documentation of returned response? 带有Swagger和Swashbuckle的HTML示例响应 - Html Example Response with Swagger and Swashbuckle 使用在 Swashbuckle 的后期构建操作中生成的 swagger.json 文件,而不是在运行时生成的文件 - Use swagger.json file generated in post build actions in Swashbuckle instead of the file generated at runtime 我可以指定 Web-Api 方法需要 Swashbuckle/Swagger 生成的页面/json 中的文件吗 - Can I specify that Web-Api method expects a file in Swashbuckle/Swagger generated page/json 如何使用swagger swashbuckle保护生成的API文档 - How to secure generated API documentation using swagger swashbuckle 如何使用Swashbuckle.AspNetCore隐藏响应代码200? - How can I hide response code 200 with Swashbuckle.AspNetCore?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM