Swashbuckle SwaggerResponseRemoveDefaults 属性仍然向 Swagger 添加 200 成功响应

Swashbuckle SwaggerResponseRemoveDefaults attribute still adding 200 Success response to Swagger

我在 .NET Core 3.1 项目中使用 Swashbuckle 和 Swagger UI,并将我的 XML 评论导入 Swagger。我在控制器上有一个 POST 请求,我想在 Swagger 中注册一些响应状态(201、401、403、404)。问题是我还在 swagger.json 文件和 Swagger UI 界面中看到 200 Success 响应与我明确指定的状态代码响应一起列出。

正如在多个不同地方所建议的那样,我正在使用 [SwaggerResponseRemoveDefaults] 属性来尝试防止这种情况发生,但是我尝试的所有操作仍然会导致列出默认的 200 响应。

我试过:

以及以上的所有组合。我还尝试将这些与以下内容结合使用:

从我的 Swagger UI 和 swagger.json.

中删除 200 次成功 结果没有任何结果

TrackerController.cs

/// <summary>...</summary>
/// <response code="401">User is not authenticated.</response>
/// <response code="404">Tracker not found.</response>
[Authorize]
[ApiController]
[Route("[controller]")]
[SwaggerResponseRemoveDefaults]
public partial class TrackersController : AbstractController
{
    ...

    /// <summary>...</summary>
    /// <param name="tracker">The details of the tracker to be created.</param>
    /// <response code="201">The tracker was successfully created.</response>
    /// <response code="403">User is not authorized to modify this resource.</response>
    [HttpPost]
    [SwaggerResponseRemoveDefaults]
    [ResponseType(typeof(TrackerDto))]
    [SwaggerResponse(201, Description = "The tracker was successfully created.")]
    public async Task<IActionResult> CreateTracker([FromBody] TrackerDto tracker)
    {
        ...
    }

    ...

}

swagger.json

{
  "openapi": "3.0.1",
  "info": {
    "title": "My API",
    "version": "v1"
  },
  "paths": {
    "/Trackers": {
      "post": {
        "tags": [
          "Trackers"
        ],
        "summary": "Create a new tracker.",
        "requestBody": {
          "description": "The details of the tracker to be created.",
          "content": {
            "application/json-patch+json": {
              "schema": {
                "$ref": "#/components/schemas/TrackerDto"
              }
            },
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/TrackerDto"
              }
            },
            "text/json": {
              "schema": {
                "$ref": "#/components/schemas/TrackerDto"
              }
            },
            "application/*+json": {
              "schema": {
                "$ref": "#/components/schemas/TrackerDto"
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Success"
          },
          "201": {
            "description": "The tracker was successfully created."
          },
          "401": {
            "description": "User is not authenticated."
          },
          "403": {
            "description": "User is not authorized to modify this resource."
          },
          "404": {
            "description": "Tracker not found."
          }
        }
      }
    }
  }
}

Swagger UI Screenshot

现在,您告诉 Swagger 您正在生成一个 TrackerDto,默认类型为 200 ,另外 另一个响应为 201。您需要制作它一对配对。

删除 ResponseType 属性并将类型放在它所属的位置:

[SwaggerResponse(201, Description = "The tracker was successfully created.", typeof(TrackerDto))]

您也可以试试:

[ProducesResponseType(typeof(TrackerDto), 201)]

重要的是将类型和状态代码放在同一个属性中,以便 Swagger 知道它们属于一起。