【问题标题】:AutoRest from Swagger Documentation - IEnumerable<long> generated as IEnumerable<long?>来自 Swagger 文档的 AutoRest - IEnumerable<long> 生成为 IEnumerable<long?>
【发布时间】:2017-01-31 17:32:37
【问题描述】:

我有一个使用 ASP.NET MVC 5、Swashbuckle 和 AutoRest 的项目。当我使用 Autorest 为我的 API 生成客户端时,我的参数正在从 IEnumerable&lt;long&gt; 转换为 IEnumerable&lt;long?&gt;

控制器方法

[HttpPost]
public IHttpActionResult Foo([FromBody] IEnumerable<long> ids)
{
    // do work
}

生成的 AutoRest 签名

Task<HttpOperationResponse<IList<ResponseModel>> FoWithMessageAsync(IList<long?> ids, ...)

我尝试过的

  • 使用获取方法。导致“多”类型,这是 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
            }
        }
    }
}

【问题讨论】:

  • 如果您可以粘贴生成此文件的 api 规范的相关部分,这将有助于获得答案。
  • @SumitMaingi,我添加了 Swashbuckle 生成的 swagger 规范。
  • 抱歉,kevin,试了 15 分钟(这是我的极限),无法正常工作
  • 很公平。还是谢谢。

标签: c# asp.net swagger swashbuckle autorest


【解决方案1】:

我可以在 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。

【讨论】:

  • 遗憾的是,作为持续集成步骤的一部分,Autorest 正在生成我的客户端。问题更多是关于如何让 Autorest 生成 swagger,而不是 swagger 本身。
  • 你的意思是 Swashbuckle 是如何产生招摇的? AutoRest 只是遵守 Swagger 规范(或至少尝试这样做)。如前所述,我从未使用过 Swashbuckle,但可能有一种方法可以说服它生成 x-nullable: false,至少我在他们的仓库中发现了一些提及 (github.com/domaindrivendev/Swashbuckle/blob/…)
  • 是的,问题归结为具有可空原始类型概念或假设的语言之间的差异。 Swashbuckle 通过使用 [Required] 属性标记属性来使用原始类型解决此问题。一个 IEnumerable 的原语是通过循环抛出 AutoRest。这只是 AutoRest 和 Swashbuckle 之间的一个问题,但它在 C# 领域的某个地方,在 Swagger 生成之前。
  • 是的,Container&lt;SomeType&gt; 是一个真正的问题,不幸的是,人们无法将 inner 东西标记为必需(只有容器本身,但谁在乎),这是其中之一添加了x-nullable 的原因:/ 希望很快会有 Swashbuckle 支持
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-13
  • 1970-01-01
  • 2015-10-18
  • 1970-01-01
  • 2018-02-24
  • 1970-01-01
相关资源
最近更新 更多