5

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

{
  "first_name": "John",
  "last_name": "Doe",
  "email": "[email protected]",
  "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="[email protected]"),
 *              @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": "[email protected]",
  "interest_ids": [
    [
      1,
      2
    ]
  ]
}

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

1 Answer 1

7

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"
 *         )
 *       )
Sign up to request clarification or add additional context in comments.

1 Comment

Wow, this was indeed the solution, can't believe I didn't try that earlier. Thank you very much!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.