如何在Openapi/Swagger中添加多个示例值?

发布于 2025-02-07 13:40:48 字数 1063 浏览 3 评论 0原文

我正在使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

ぺ禁宫浮华殁 2025-02-14 13:40:48

要显示一个带有多个项目的数组示例,请在数组级别而不是项目级别上添加示例

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

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

对于对象数组,示例看起来像这样:

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

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]

In case of array of objects, the example would look like this:

type: array
items:
  type: object
  properties:
    id:
      type: integer
    name:
      type: string
example:
  - id: 1
    name: Prashant
  - id: 2
    name: Helen
孤城病女 2025-02-14 13:40:48

路径:
/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"

凉月流沐 2025-02-14 13:40:48

对于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

For openapi version - 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文