How to get nested array in swagger definition [duplicate]

久未见 提交于 2019-12-29 02:10:55

问题


Trying to get this Json outpun with swagger definitions:

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

My swagger.json looks like this:

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

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

And I get this Json on the request:

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

How do I get the first Json syntax trough Swagger references?


回答1:


You need an extra definition for the wrapper object that has the attachment property:

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

Then change your array definition to use this wrapper as the item type:

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


来源:https://stackoverflow.com/questions/43876840/how-to-get-nested-array-in-swagger-definition

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!