我有一个使用ASP.NET MVC 5,Swashbuckle和AutoRest的项目 . 当我使用Autorest为我的API生成客户端时,我的参数正从 IEnumerable<long>
转换为 IEnumerable<long?>
控制器方法
[HttpPost]
public IHttpActionResult Foo([FromBody] IEnumerable<long> ids)
{
// do work
}
生成的AutoRest签名
Task<HttpOperationResponse<IList<ResponseModel>> FoWithMessageAsync(IList<long?> ids, ...)
我尝试了什么
-
使用Get方法 . 结果为"multi"类型,这是Autorest known bug in AutoRest中的已知错误 .
-
消除[FromBody]属性
-
将IEnumerable包装在自定义模型中
这是一个奇怪的错误,但我已经与Swagger和Autorest合作了一段时间,所以我只能假设它是一个不起眼的bug /配置(可能)或我错过了一些愚蠢的(可能) . 在此先感谢您的帮助 .
更新
这是Swashbuckle生成的Swagger规范
{
"swagger": "2.0",
"info": {
"version": "v1",
"title": "FooBar",
},
"host": "localhost:5000",
"schemes": [
"http"
],
"paths": {
"/api/v1/Foo": {
"post": {
"operationId": "foo",
"consumes": [
"application/json",
"text/json"
],
"produces": [
"application/json",
"text/json"
],
"parameters": [
{
"name": "ids",
"in": "body",
"description": "",
"required": true,
"schema": {
"type": "array",
"items": {
"format": "int64",
"type": "integer"
}
}
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"type": "array",
"items": {
"$ref": "#/definitions/Foo"
}
}
},
"404": {
"description": "NotFound"
},
"500": {
"description": "InternalServerError"
}
},
"deprecated": false
}
}
}
}
1 回答
我可以告诉你如何在Swagger文件中解决你的问题并使用 latest 版本的AutoRest(https://github.com/Azure/autorest),但我不知道Swashbuckle,即它是否可以生成以下内容:
请注意,我已拼写出内联架构而不是引用
"$ref": "#/definitions/Foo"
以保持简单 . 重要的是设置"x-nullable": false
,它会覆盖可以为空的默认行为(期望服务器响应中的最差) . 在您的情况下,必须将属性添加到引用的模式.../Foo
.此功能只有几天,所以请务必拉出最新的AutoRest .