arrays - 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'

这是我放在 swagger UI 中的文本:

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

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

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

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

最佳答案

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

关于arrays - Swagger 的对象数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43711744/

相关文章:

angular - HTML5 Websql - 如何与 Ionic 3 和 Angular 4 一

r - 在 shinyapps.io 中部署 R 应用程序 - "Error:parsing man

c# - Unity Particle Collider 与粒子

r - 观星者,无输出

json - JSON 模式中的变量对象名称

linux - 错误 "caution: filename not matched"- 解压缩命令

perl - 在 Perl 中打印哈希值

php - 当要比较的元数据是序列化数组时,Wordpress meta_query 值数组?

qt - 对 `dlsym' 的 undefined reference

django - 值错误 : Cannot use Queryset for "": Use a Q