我有一个使用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方法。结果为“多”类型,这是Autorest known bug in AutoRest中的已知错误。
消除[FromBody]属性
将IEnumerable包装在自定义模型中


这是一个奇怪的错误,但是我在Swagger和Autorest上工作了一段时间,所以我只能假设它是一个模糊的错误/配置(可能),或者我错过了一些愚蠢的东西(可能)。在此先感谢您的帮助。

更新资料

这是Swashbuckle生成的Swagger Spec

{
    "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
            }
        }
    }
}

最佳答案

我可以在Swagger文件中使用最新版本的AutoRest(https://github.com/Azure/autorest)告诉您如何解决您的问题,但是我不知道Swashbuckle,即它是否可以生成以下内容:

"200": {
    "description": "OK",
    "schema": {
        "type": "array",
        "items": {
          "x-nullable": false,
          "type": "integer",
          "format": "int64"
        }
    }
},


请注意,我已经阐明了一个内联模式,而不是引用"$ref": "#/definitions/Foo"来使其保持简单。
重要的是设置"x-nullable": false,它会覆盖默认为空(默认为服务器响应)的行为。
在您的情况下,必须将属性添加到引用的架构.../Foo

该功能仅使用了几天,因此请确保拉最新的AutoRest。

09-15 23:23