Swagger array of objects

假装没事ソ 提交于 2019-12-20 07:38:06

问题


I am having some issues with swagger: I have an array of objects (address) described in this way in the .yaml file:

Address:
  properties:
    street:
      type: string
    city:
      type: string
    state:
      type: string
    country:
      type: string

and this is the other yaml file with the definitions of the API (address is a params):

 - name: addresses
   in: formData
   description: List of adresses of the user. The first one is the default one.
   type: array
   items:
     $ref: '#/definitions/Address'

And this is the text I put in the swagger UI:

[
  {
    "street": "Bond street",
    "city": "Torino",
    "state": "Italy",
    "country": "Italy"
  }
]

but in node.js, if I print what I receive:

{"addresses":["["," {"," \"street\": \"Bond street\","," \"city\": \"Torino\","," \"state\": \"Italy\","," \"country\": \"Italy\""," }","]"]}

And I get a parsing error... There are some extra [ and ". It seems that swagger parse it as string (?)


回答1:


To send JSON data, you need to use use an in: body parameter (not in: formData) and specify that the operation consumes application/json. formData parameters are used for operations that consume application/x-www-form-urlencoded or multipart/form-data.

paths:
  /something:
     post:
       consumes:
         - application/json
       parameters:
         - in: body
           name: addresses
           required: true
           schema:
             type: array
             items:
               $ref: "#/definitions/Address"  # if "Address" is in the same file
               # or
               # $ref: "anotherfile.yaml#/definitions/Address"  # if "Address" is in another file


来源:https://stackoverflow.com/questions/43711744/swagger-array-of-objects

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