gpt4 book ai didi

yaml - 在Swaggerhub中返回对象数组

转载 作者:行者123 更新时间:2023-12-04 10:14:32 26 4
gpt4 key购买 nike

我正在swaggerhub中定义API规范。 / contacts请求返回一个联系人数组。定义如下:

/contacts:     
get:
tags:
- contacts
summary: Get all the contacts
description: This displays all the contacts present for the user.
operationId: getContact
produces:
- application/json
- application/xml
responses:
200:
description: successful operation
schema:
$ref: '#/definitions/AllContacts'
400:
description: Invalid id supplied
404:
description: Contact not found
500:
description: Server error
definitions:
AllContacts:
type: array
items:
- $ref: '#/definitions/ContactModel1'
- $ref: '#/definitions/ContactModel2'


ContactModel1:
type: object
properties:
id:
type: integer
example: 1
firstName:
type: string
example: 'someValue'
lastName:
type: string
example: 'someValue'

ContactModel2:
type: object
properties:
id:
type: integer
example: 2
firstName:
type: string
example: 'someValue1'
lastName:
type: string
example: 'someValue1'


由于某种原因,它仅返回第二个对象,而不返回整个对象数组。我正在使用OpenAPI spec 2.0,并怀疑此版本未很好地支持阵列

最佳答案

对象的数组定义如下。 items的值必须是描述阵列项目的单个模型。

definitions:
AllContacts:
type: array
items:
$ref: '#/definitions/ContactModel'

ContactModel:
type: object
properties:
id:
type: integer
example: 1
firstName:
type: string
example: Sherlock
lastName:
type: string
example: Holmes


默认情况下,Swagger UI仅显示一项显示数组示例,如下所示:

[
{
"id": 1,
"firstName": "Sherlock",
"lastName": "Holmes"
}
]


如果希望数组示例包含多个项目,请在数组模型中指定多项目 example

definitions:
AllContacts:
type: array
items:
$ref: '#/definitions/ContactModel1'
example:
- id: 1
firstName: Sherlock
lastName: Holmes
- id: 2
firstName: John
lastName: Watson

关于yaml - 在Swaggerhub中返回对象数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46167981/

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