如何在Openapi/Swagger中添加多个示例值?
我正在使用Swagger OpenAPI规范工具。我在一个定义之一中有一个字符串数组属性,如下所示:
cities:
type: array
items:
type: string
example: "Pune"
我的API产生JSON结果,因此Swagger UI显示了以下示例的响应示例:
{
"cities": [
"Pune"
]
}
如何为copies> cities
数组添加多个示例值?期望结果为:
{
"cities": [
"Pune",
"Mumbai",
"Bangaluru"
]
}
在示例
键中尝试的逗号分隔字符串如下:
cities:
type: array
items:
type: string
example: "Pune", "Mumbai", "Bangaluru"
但是Swagger Editor显示了一个错误,“不良凹痕”。
有什么方法可以在示例
密钥中指定多个值?
更新
以下用户Helen给出了正确的答案。我有一个凹痕问题,因此有嵌套数组(2D数组)
正确的方法:
cities:
type: array
items:
type: string
example:
- Pune
- Mumbai
我的方式(错误)
cities:
type: array
items:
type: string
example:
- Pune
- Mumbai
寻找示例>示例的凹痕
在上面的两种情况下,这与众不同YAML凹痕很重要。
I am using Swagger OpenAPI Specification tool. I have a string array property in one of the definitions as follows:
cities:
type: array
items:
type: string
example: "Pune"
My API produces JSON result, so Swagger UI displays the following example for the response:
{
"cities": [
"Pune"
]
}
How can I add multiple example values for the cities
array? Expecting the result as:
{
"cities": [
"Pune",
"Mumbai",
"Bangaluru"
]
}
Tried comma-separated strings in the example
key like below:
cities:
type: array
items:
type: string
example: "Pune", "Mumbai", "Bangaluru"
But the Swagger Editor shows an error, "Bad indentation".
Is there any way to specify multiple values in the example
key?
Update
User Helen below has given the correct answer. I had an indentation problem hence there were nested arrays (2d arrays)
Correct way:
cities:
type: array
items:
type: string
example:
- Pune
- Mumbai
My way (which was wrong)
cities:
type: array
items:
type: string
example:
- Pune
- Mumbai
Look for the indentation of the example
key in the above two cases which makes the difference, its YAML indentation matters.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
要显示一个带有多个项目的数组示例,请在数组级别而不是项目级别上添加
示例
:对于对象数组,
示例
看起来像这样:To display an array example with multiple items, add the
example
on the array level instead of item level:In case of array of objects, the
example
would look like this:路径:
/products_1:
得到:
回答:
“ 200”:
描述:“ xyz”
内容:
应用程序/JSON:
模式:
类型:数组
项目:
$ ref:'#/组件/schemas/pro'
例子:
- 名称:“笔记本电脑”
DIST:“ HP笔记本电脑”
- 名称:“移动”
DIST:“ Apple Mobile”
组件:
模式:
Pro:
类型:对象
特性:
prodName:
类型:字符串
示例:“笔记本电脑”
proddesc:
类型:字符串
示例:“生产描述”
paths:
/products_1:
get:
responses:
'200':
description: "XYZ"
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/pro'
example:
- name : "Laptop"
dist : "HP LAPTOP"
- name : "Mobile"
dist : "Apple Mobile"
components:
schemas:
pro:
type: object
properties:
prodName:
type: string
example: "Laptop"
prodDesc:
type: string
example: "Produc Description"
对于OpenAPI版本-3.0.0+
For openapi version - 3.0.0+