Ho to add multiple example items on swagger

2019-03-03 11:26发布

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条回答
走好不送
2楼-- · 2019-03-03 12:20

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}
*   }
* )
查看更多
登录 后发表回答