【发布时间】:2018-08-24 23:36:12
【问题描述】:
我正在创建一个 C# ASP.NET Core 2.0 REST API,大部分情况下一切顺利。它使用 MVC 路由来生成 REST API。控制器非常简单。
// POST: api/Volume/{zoneID}/Set/{volume}
[HttpPost("{zoneId:int}/[action]/{volume:int}", Name = "Set")]
public IActionResult Set(int zoneId, int volume)
{
return CreateAndSend(strZonesLevel, zoneId, $"{volume:X2}");
}
使用最新的一切,为 AspNetCore 2.3.0 安装了 Swagger/Swashbuckle,并且 UI 提供了 API 和所有内容。 SwashBuckle UI 运行良好,我可以测试 API 等。
一个例外是在 UI 上,响应类型总是返回为“未知响应类型”。
https://i.stack.imgur.com/6qqBh.jpg
我的类前面有以下属性(所有方法都返回相同的类型)
[Produces("application/json")]
[Route("api/Volume")]
[ProducesResponseType(typeof(ControllerResponseModel), 200)]
[ProducesResponseType(typeof(ControllerResponseModel), 400)]
[ProducesResponseType(typeof(ControllerResponseModel), 500)]
生成的 JSON 似乎没问题,ControllerResponseModel 在定义中,并被 Volume API 在所有正确的地方引用。这是一个子集。
{
"swagger": "2.0",
"info": {
"version": "v1",
"title": "AVController API"
},
"paths": {
"/api/Volume/{zoneId}/Set/{volume}": {
"post": {
"tags": ["Volume"],
"operationId": "ApiVolumeByZoneIdSetByVolumePost",
"consumes": [],
"produces": ["application/json"],
"parameters": [
{
"name": "zoneId",
"in": "path",
"required": true,
"type": "integer",
"format": "int32"
},
{
"name": "volume",
"in": "path",
"required": true,
"type": "integer",
"format": "int32"
}
],
"responses": {
"200": {
"description": "Success",
"schema": {
"type": "array",
"items": { "$ref": "#/definitions/ControllerResponseModel" }
}
},
"400": {
"description": "Bad Request",
"schema": {
"type": "array",
"items": { "$ref": "#/definitions/ControllerResponseModel" }
}
},
"500": {
"description": "Server Error",
"schema": {
"type": "array",
"items": { "$ref": "#/definitions/ControllerResponseModel" }
}
}
}
}
}
},
"definitions": {
"ControllerResponseModel": {
"type": "object",
"properties": {
"command": { "type": "string" },
"message": { "type": "string" },
"url": { "type": "string" }
}
}
}
}
任何想法为什么 UI 不会显示返回类型和值?我尝试了很多东西,比如使用gets代替posts以及使用[SwaggerResponse]属性,但结果是一样的。
【问题讨论】:
-
我建议在 GitHub 上的 Swagger UI 存储库中使用 opening an issue。
-
是的,那是我的下一站。会的。
-
原来这是 Swagger/Swashbuckle 的 Edge 浏览器问题。它在 Chrome/FF 中运行良好。 Swashbuckle 团队正在调查。
标签: swagger swagger-ui swagger-2.0 swashbuckle openapi