我在.NET Core 3.1项目中使用Swashbuckle和Swagger UI,并将我的XML注释导入Swagger。我在控制器上有一个POST请求,我想在Swagger中注册许多响应状态(201、401、403、404)。问题是,在swagger.json
文件和Swagger UI界面中,我还在显式指定的状态代码响应旁边看到了200 Success响应。
正如在多个不同地方所建议的,我正在使用[SwaggerResponseRemoveDefaults]
属性来尝试防止这种情况,但是我尝试执行的所有操作仍然会导致列出默认的200响应。
我尝试过:
以及以上所有组合。我还尝试了以下方法:
<response code="XXX"></response>
注释标签指定所需的响应类型,和[SwaggerResponse(XXX)]
端点指定所需的响应类型。什么都没有从我的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."
}
}
}
}
}
}
我在.NET Core 3.1项目中使用Swashbuckle和Swagger UI,并将我的XML注释导入Swagger。我在控制器上有一个POST请求,我想注册多个响应状态...
现在,您告诉Swagger您正在生成默认类型为200的TrackerDto
。[[另外