Swagger-php: How to define a dynamic object key?

Created on 15 May 2017  路  4Comments  路  Source: zircote/swagger-php

I want to create a api for order, I post the shopping cart data as cart like this:

{
    sku_id: order_quantity,
    ....,
}

example:
cart: {
    23: 1,
    34: 2
},

The sku_id represent product id, order_quantity means the number user want to book. And I write my swagger code like this.

@SWG\Parameter(
    name="cart",
    description="cart data",
    type="object",
    required=true,
    ??? How to define the object key here?
)

Would you please teach me How to define the object key? Thank you.

Most helpful comment

The JSON spec has patternProperties

But this isn't supported in the Swagger v2.0 specification.

If you change the request into:

"cart":[
  { "product_id": 23, "amount": 1 },
  { "product_id": 34, "amount": 2 }
]

You'd be able to document that with:

* @SWG\Parameter(
 *   name="body",
 *   in="body",
 *   description="cart data",
 *   required=true,
 *   @SWG\Schema(
 *       type="object",
 *       @SWG\Property(
 *         property="cart",
 *         type="array",
 *         @SWG\Items(
 *           type="object",
 *           @SWG\Property(property="product_id", type="integer"),
 *           @SWG\Property(property="amount", type="integer")
 *         )
 *       )
 *     )
 *   )
 * ),

All 4 comments

The JSON spec has patternProperties

But this isn't supported in the Swagger v2.0 specification.

If you change the request into:

"cart":[
  { "product_id": 23, "amount": 1 },
  { "product_id": 34, "amount": 2 }
]

You'd be able to document that with:

* @SWG\Parameter(
 *   name="body",
 *   in="body",
 *   description="cart data",
 *   required=true,
 *   @SWG\Schema(
 *       type="object",
 *       @SWG\Property(
 *         property="cart",
 *         type="array",
 *         @SWG\Items(
 *           type="object",
 *           @SWG\Property(property="product_id", type="integer"),
 *           @SWG\Property(property="amount", type="integer")
 *         )
 *       )
 *     )
 *   )
 * ),

@bfanger I know how to do now, thank you.

@WenXuanYuan How do you solve this problem?

Changed object model

Was this page helpful?
0 / 5 - 0 ratings