arrays - Swagger 的对象数组

标签 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'

这是我放在 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/

相关文章:

JavaScript 对象 : is it not OK to redefine the prototype?

javascript - 在没有 eval() 的情况下动态地将字符串评估为对象?

java - 为什么我会遇到数组索引越界异常?

c# - 在 C# 的 string[] 数组中插入字符串

java - 如何拆分Java对象的元素?

c# - Action 需要 Swagger 的独特方法/路径组合

asp.net-web-api - Swashbuckle(Swagger)中的默认模型示例

swagger - Swagger 的多个 Api 版本

c - 地址上的二维 float 组

javascript - D3 SVG 图表中的现有元素未更新 - 全部被视为 exit() 和 Enter() 集