如何在招摇属性中添加多个示例值? [英] How to add multiple example values in swagger properties?

查看:24
本文介绍了如何在招摇属性中添加多个示例值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 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"

我想给示例标签赋予多个值有什么办法吗?

I want to give multiple values to the example tag is there any way ?

更新

下面的用户 Helen 给出了正确答案我有缩进问题,因此存在嵌套数组(二维数组)

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

我的方式(这是错误的)

My way ( Which was wrong )

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]

这篇关于如何在招摇属性中添加多个示例值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆