【发布时间】:2018-07-29 21:47:01
【问题描述】:
我在使用 Swashbuckle 为 API 生成 Swagger 文档时遇到一些问题,该 API 返回包含属性的自定义类型,该属性是字典,其值为 IEnumerable。
public class MyReturnModel
{
public IDictionary<int, IEnumerable<MyModel>> Data { get; set;}
public MyReturnModel()
{
Data = new Dictionary<int, IEnumerable<MyModel>>();
}
}
public class MyModel
{
public int Value { get; set; }
}
如果我有这样的 API 端点:
[ProducesResponseType(typeof(MyReturnModel), (int)HttpStatusCode.OK)]
[HttpGet]
public async Task<IActionResult> GetAsync()
{
var myReturn = await MyReturnModelRepository.GetAsync();
return Ok(myReturn);
}
我可以看到的 Swagger UI 显示响应类型如下:
MyReturnModel {
data (inline_model, optional)
}
inline_model {}
如何让 Swagger 生成器为集合和内部模型生成文档?
【问题讨论】:
-
如果你把,
[ProducesResponseType(typeof(Dictionary<int, IEnumerable<MyModel>>) -
如果我这样做(a)它是不正确的,因为它返回 MyReturnModel 具有该类型的成员(我已经为示例简化了它,但 MyReturnModel 有其他成员),并且(b)它只是表示该方法返回 Inline_Model。
-
愿意提供帮助... _ 您有重现此问题的最小项目吗? _ 如果没有,你能在 GitHub 上创建一个吗?
-
没关系.. 尝试重现,但它起作用了......意识到我的新解决方案有 swashbuckle 2.0.0,而我的旧解决方案有 1.2.0...... 更新了,它可以工作。
标签: c# swagger asp.net-core-2.0 asp.net-core-webapi swashbuckle