如何在swagger属性中添加多个示例值? [英] How to add multiple example values in swagger properties?
问题描述
我正在使用Swagger OpenAPI规范工具,在以下定义之一中具有字符串数组属性:
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"
我的API会生成JSON结果,因此对于上述对象,以下结果会出现在响应中:
My API produce JSON result so for above object following result appears in response :
{
"cities": [
"Pune"
]
}
尝试用逗号分隔字符串,如下所示:
Tried comma separated strings like below :
cities:
type: array
items:
type: string
example: "Pune", "Mumbai", "Bangaluru"
预期结果为:
{
"cities": [
"Pune",
"Mumbai",
"Bangaluru"
]
}
但是编辑器显示错误. 缩进错误"
But the editor shows error. "Bad indentation"
我想给example标记赋予多个值,有什么办法吗?
I want to give multiple values to the example tag is there any way ?
更新
下面的用户Helen给出了正确的答案,我有缩进问题,因此存在嵌套数组(2d数组)
User Helen below has given correct answer I had indentaion problem hence there were nested arrays ( 2d arrays )
正确的方式:
cities:
type: array
items:
type: string
example:
- Pune
- Mumbai
我的方式(错了)
cities:
type: array
items:
type: string
example:
- Pune
- Mumbai
在上述两种情况下寻找example
标记的缩进会有所不同,其YAML缩进很重要.
Look for indentation of example
tag in above two cases which makes the difference, Its YAML indentation matters.
推荐答案
要显示包含多个项目的数组示例,请在数组级别而不是项目级别添加example
:
To display an array example with multiple items, add the example
on the array level instead of item level:
cities:
type: array
items:
type: string
example:
- Pune
- Mumbai
- Bangaluru
# or
# example: [Pune, Mumbai, Bangaluru]
这篇关于如何在swagger属性中添加多个示例值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!