【问题标题】:How to get nested array in swagger definition [duplicate]如何在招摇定义中获取嵌套数组[重复]
【发布时间】:2017-10-08 04:18:12
【问题描述】:

试图用大摇大摆的定义让这个 Json 胜出:

{
"attachments":[{
"attachment": 
  {
    "filename": "string",
    "filedata": "string",
    "filetype": "string"
  }
},
{
"attachment": 
  {
    "filename": "string",
    "filedata": "string",
    "filetype": "string"
  }
}]
}

我的 swagger.json 看起来像这样:

"attachments": {
    "type":"array",
    "items": {
    "$ref": "#/definitions/attachment"
  }
},          

"attachment": {
      "type": "object",
      "properties": {
        "filename": {
          "type": "string"
        },
        "filedata": {
          "type": "string"
        },
        "filetype": {
          "type": "string"
        }
      }
    },

我根据请求得到了这个 Json:

"attachments": [
  {
    "filename": "string",
    "filedata": "string",
    "filetype": "string"
  }
],

如何通过 Swagger 引用获得第一个 Json 语法?

【问题讨论】:

  • 您说的是“嵌套数组”,但您的第一个示例是对象/哈希图。哪一个是正确的?
  • 对不起,我们的目标是得到一个数组,我用正确的语法编辑了这个问题。谢谢。

标签: json swagger swagger-2.0


【解决方案1】:

您需要为具有attachment 属性的包装器对象进行额外定义:

"attachmentWrapper": {
  "type": "object",
  "properties": {
    "attachment": {
      "$ref": "#/definitions/attachment"
    }
  }
}

然后更改您的数组定义以使用此包装器作为项目类型:

"attachments": {
  "type":"array",
  "items": {
    "$ref": "#/definitions/attachmentWrapper"
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-08-30
    • 2021-04-19
    • 2021-12-22
    • 2016-05-06
    • 1970-01-01
    • 2017-08-16
    • 2016-04-09
    相关资源
    最近更新 更多