我在 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 中,如果我打印收到的内容:
{"地址":["["," {"," \"街道\": \"邦德街\",","
\“城市\”:\“都灵\”,“,”\“州\”:\“意大利\”,“,”\“国家\”:
\“意大利\””,” }”,”]”]}
我得到一个解析错误...有一些额外的 [ 和 "。似乎 swagger 将它解析为字符串(?)
要发送 JSON 数据,您需要使用in: body
参数(不in: formData
)并指定该操作消耗application/json
. formData
参数用于消耗的操作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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)