如何在 swagger 属性中添加多个示例值?

How to add multiple example values in swagger properties?

我正在使用 Swagger OpenAPI 规范工具,我在其中一个定义中有一个字符串数组 属性 如下:

cities:
        type: array
        items:
          type: string
          example: "Pune"

我的 API 产生 JSON 结果,因此对于上述对象,以下结果出现在响应中:

{
  "cities": [
    "Pune"
  ]
}

试过逗号分隔的字符串,如下所示:

cities:
            type: array
            items:
              type: string
              example: "Pune", "Mumbai", "Bangaluru"

预期结果为:

{
      "cities": [
        "Pune",
        "Mumbai",
        "Bangaluru"
      ]
    }

但是编辑器显示错误。 "Bad indentation"

我想为示例标签提供多个值,有什么办法吗?

更新

下面的用户 Helen 给出了正确答案我有缩进问题因此有嵌套数组(2d 数组)

正确的方法:

cities:
        type: array
        items:
          type: string
        example: 
        - Pune
        - Mumbai

我的方式(错了)

cities:
        type: array
        items:
          type: string
          example: 
          - Pune
          - Mumbai

在上述两种情况下寻找 example 标签的缩进,这会有所不同,它的 YAML 缩进很重要。

要显示包含多个项目的数组示例,请在数组级别而不是项目级别添加 example

cities:
  type: array
  items:
    type: string
  example:
    - Pune
    - Mumbai
    - Bangaluru

  # or
  # example: [Pune, Mumbai, Bangaluru]

如果是对象数组,example 将如下所示:

type: array
items:
  type: object
  properties:
    id:
      type: integer
    name:
      type: string
example:
  - id: 1
    name: Prashant
  - id: 2
    name: Helen

对于 openapi 版本 - 3.0.0+

  major:
      type: array
      items:
        type: string
        enum:
          - Accounting
          - Business Contacts
          - Economy
          - Finance
          - Graphic Design
          - International Business Administration
          - International Relations
          - Law
          - Marketing
          - others
          - Political Science
          - Statistics