Ho to add multiple example items on swagger

那年仲夏 提交于 2019-11-28 13:07:04

问题


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)
)

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,
        }
      ]

回答1:


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

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.

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}
*   }
* )


来源:https://stackoverflow.com/questions/46317503/ho-to-add-multiple-example-items-on-swagger

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!