如何在swagger定义中获取嵌套数组

时间:2017-05-09 17:59:38

标签: json swagger swagger-2.0

尝试以宽松的定义来获得这个Json outpun:

{
"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语法?

1 个答案:

答案 0 :(得分:1)

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

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

然后更改数组定义以将此包装器用作项类型:

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