我在 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-urlencoded
或 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
关于arrays - Swagger 的对象数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43711744/