在 Swagger 属性 (OAS3) 中设置带有 ids 的数组示例

use*_*227 3 php swagger swagger-php openapi

我试图在 swagger (OAS3) 中指定以下 requestBody:

{
  "first_name": "John",
  "last_name": "Doe",
  "email": "user@example.com",
  "interest_ids": [
     1,2
  ]
}
Run Code Online (Sandbox Code Playgroud)

我目前已指定 requestBody 如下:

 *     @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
 *                  ),
 *              ),
 *          ),
 *     ),
Run Code Online (Sandbox Code Playgroud)

这给了我一个正确的 requestBody,但是这显然只给了我一个键,其值3作为interest_ids数组的示例值。如何在不将类型设置为字符串的情况下指定多个 id?我尝试了多种解决方案,例如将示例设置为example={1,2},不幸的是,这给了我数组中的另一个数组interest_ids

{
  "first_name": "John",
  "last_name": "Doe",
  "email": "user@example.com",
  "interest_ids": [
    [
      1,
      2
    ]
  ]
}
Run Code Online (Sandbox Code Playgroud)

我还尝试将collectionFormat属性类型设置为不同的值,但这也没有帮助。

小智 5

您是否尝试过将示例从@OA\Items上方移至该属性中?

现在的示例是数组的单个值的示例。

我想你想要的是一个interest_ids本身的例子。

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