如何在Swagger 2.0中将long数组表示为查询参数?

时间:2018-12-13 05:52:38

标签: c# swagger swagger-2.0 swagger-codegen

在我希望查询中的参数为long(C#)列表的情况下,我很难弄清楚什么是正确的Swagger 2.0规范。这是我根据看到的示例进行尝试的示例,其中在查询中传递的参数是简单的数据类型,例如intboolean。但这似乎不起作用。看起来好像没有正确解析。

我的URI是这样的: https://.../testinstance/FeatureTexts?api-version=2016-09-13&featureIds=1629988727&featureIds=1924980024

在我的API级测试中,在 api-version = 2016-09-13&featureIds =

部分之后,它没有解决任何相似的问题
      "get": {
        "tags": [
          "FeatureText"
        ],
        "operationId": "RenderFeatureTexts",
        "description": "The operation to get feature texts for specified features",
        "parameters": [
          {
            "name": "featureIds",
            "in": "query",
            "required": true,
            "schema": {
              "type": "array",
              "collectionFormat": "multi",
                "items": {
                  "type": "integer",
                  "format": "int64"
                }
              },
.......
Swagger Codegen生成的

C#代码:

public static async System.Threading.Tasks.Task<object> ListFeatureTextsAsync(this IAgentClient operations, object featureIds, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken))
{
    using (var _result = await operations.ListFeatureTextsWithHttpMessagesAsync(featureIds, null, cancellationToken).ConfigureAwait(false))
    {
        return _result.Body;
    }
}

1 个答案:

答案 0 :(得分:1)

如下所示更改参数定义,即将typeitemscollectionFormat移出schema。在OpenAPI 2.0中,schema仅用于主体参数,其他参数类型直接使用type等。

    "parameters": [
      {
        "name": "featureIds",
        "in": "query",
        "required": true,
        "type": "array",
        "collectionFormat": "multi",
        "items": {
          "type": "integer",
          "format": "int64"
        }

通过将规范粘贴到Swagger Editor中,您可以轻松捕获此类语法错误。