如何在 swagger 上添加多个示例项目 [英] Ho to add multiple example items on swagger

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

问题描述

需要有关如何在 swagger 上执行此操作的帮助.

Need help on how to do this on swagger.

@SWG\Property(property="LineItems", type="array", @SWG\Items(ref="#/definitions/LineItem"))

@SWG\Definition(
     definition="LineItem",
     required={"Description","Quantity","UnitAmount"},
     @SWG\Property(property="Description", type="string", example="Item 1"),
     @SWG\Property(property="Quantity", type="integer", example=100),
     @SWG\Property(property="UnitAmount", type="float", example=11)
)

@SWG\Definition(
     definition="LineItem2",
     required={"Description","Quantity","UnitAmount"},
     @SWG\Property(property="Description", type="string", example="Item 2"),
     @SWG\Property(property="Quantity", type="integer", example=10),
     @SWG\Property(property="UnitAmount", type="float", example=21)
)

我想在 LineItems 属性上添加 LineItem 和 LineItem2,我希望输出应该是这样的

I want to add LineItem and LineItem2 on LineItems property, I want the output should be like this

"LineItems": [
        {
          "Description": "Item 1",
          "Quantity": 100,
          "UnitAmount": 11,
        },
        {
          "Description": "Item 2",
          "Quantity": 100,
          "UnitAmount": 22,
        }
      ]

推荐答案

要在 Swagger UI 中显示具有多个项目的数组示例,您需要一个数组级别的 example,例如:

To display array example with multiple items in Swagger UI, you need an array-level example, such as:

LineItems:
  type: array
  items:
    $ref: '#/definitions/LineItem'

  # Multi-item example
  example:
    - Description: Item 1
      Quantity: 100
      UnitAmount: 11
    - Description: Item 2
      Quantity: 100
      UnitAmount: 22

也就是说,数组项(LineItem)只有一个定义,多项示例是在数组级别使用example关键字定义的.

That is, there is a single definition for array items (LineItem), and the multi-item example is defined using the example keyword on the array level.

Swagger-PHP 版本将是:

The Swagger-PHP version of this would be:

* @SWG\Property(
*   property="LineItems",
*   type="array",
*   @SWG\Items(ref="#/definitions/LineItem"),
*   example = {
*     {"Description": "Item 1", "Quantity": 100, "UnitAmount": 11},
*     {"Description": "Item 2", "Quantity": 100, "UnitAmount": 22}
*   }
* )

这篇关于如何在 swagger 上添加多个示例项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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