phpswaggeropenapiswagger-php

Set example of array with ids in Swagger Property (OAS3)


I'm trying to specify the following requestBody in swagger (OAS3):

{
  "first_name": "John",
  "last_name": "Doe",
  "email": "user@example.com",
  "interest_ids": [
     1,2
  ]
}

I currently have specified the requestBody as follows:

 *     @OA\RequestBody(
 *          required=true,
 *          @OA\JsonContent(
 *              required={"email"},
 *              @OA\Property(property="first_name", type="string", format="string", example="John"),
 *              @OA\Property(property="last_name", type="string", format="string", example="Doe"),
 *              @OA\Property(property="email", type="string", format="email", example="user@example.com"),
 *              @OA\Property(property="interest_ids", type="array", @OA\Items(
 *                      type="integer",
 *                      example=3
 *                  ),
 *              ),
 *          ),
 *     ),

This gives me a correct requestBody, however this obviously gives me only one key with value 3 as example value for the interest_ids array. How can I specify multiple ids without setting the type to string? I have tried multiple solutions, for example setting examples as example={1,2}, this unfortunately gives me another array within the interest_ids array:

{
  "first_name": "John",
  "last_name": "Doe",
  "email": "user@example.com",
  "interest_ids": [
    [
      1,
      2
    ]
  ]
}

I've also tried setting collectionFormat on the property type to different values, but that didn't help either.


Solution

  • Have you tried moving the example from @OA\Items up into the property?

    Right now the example is the example of a single value of the array.

    I suppose what you want is an example of interest_ids itself.

     *       @OA\Property(property="interest_ids", type="array",
     *         example={3, 4},
     *         @OA\Items(
     *           type="integer"
     *         )
     *       )