gpt4 book ai didi

arrays - Swagger 的对象数组

转载 作者:行者123 更新时间:2023-12-05 09:18:46 24 4
gpt4 key购买 nike

我在 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com