Swagger对象数组

时间:2017-04-30 21:54:06

标签: arrays object swagger

我遇到了一些swagger的问题:我在.yaml文件中有这样描述的对象数组(地址):

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

这是另一个带有API定义的yaml文件(地址是一个参数):

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

这是我在招摇的UI中添加的文字:

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

但在node.js中,如果我打印收到的内容:

  

{"地址":[" ["" {"" \" street \":\" Bond street \",","   \" city \":\"都灵\","," \#34;州\":\"意大利\","," \"国家\&#34 ;:   \"意大利\""" }""]"]}

我得到一个解析错误......有一些额外的[和"。似乎swagger将其解析为字符串(?)

1 个答案:

答案 0 :(得分:4)

要发送JSON数据,您需要使用in: body参数(不是in: formData)并指定操作使用application/jsonformData参数用于使用application/x-www-form-urlencodedmultipart/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