摇摇欲坠的对象数组 [英] Swagger array of objects
问题描述
我有些招摇不定:.yaml文件中有一个以这种方式描述的对象(地址)数组:
I am having some issues with swagger: I have an array of objects (address) described in this way in the .yaml file:
Address:
properties:
street:
type: string
city:
type: string
state:
type: string
country:
type: string
这是另一个具有API定义的yaml文件(地址是一个参数):
and this is the other yaml file with the definitions of the API (address is a params):
- name: addresses
in: formData
description: List of adresses of the user. The first one is the default one.
type: array
items:
$ref: '#/definitions/Address'
这是我放在招摇的用户界面中的文字:
And this is the text I put in the swagger UI:
[
{
"street": "Bond street",
"city": "Torino",
"state": "Italy",
"country": "Italy"
}
]
但是在node.js中,如果我打印出我收到的内容:
but in node.js, if I print what I receive:
{地址":["[","{","\"街道\:\"邦德街\,", \"city \":\"Torino \",," \"state \":\意大利\",," \国家\": \意大利\","},"]]}
{"addresses":["["," {"," \"street\": \"Bond street\","," \"city\": \"Torino\","," \"state\": \"Italy\","," \"country\": \"Italy\""," }","]"]}
我得到一个解析错误...还有一些额外的[和.似乎大张旗鼓地将其解析为字符串(?)
And I get a parsing error... There are some extra [ and ". It seems that swagger parse it as string (?)
推荐答案
要发送JSON数据,您需要使用in: body
参数(而不是in: formData
)并指定该操作使用application/json
. formData
参数用于消耗application/x-www-form-urlencoded
或multipart/form-data
的操作.
To send JSON data, you need to use use an in: body
parameter (not in: formData
) and specify that the operation consumes application/json
. formData
parameters are used for operations that consume 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
这篇关于摇摇欲坠的对象数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!