2

我正在编写 OpenAPI 规范并尝试从请求路由/路径的注释中自动(使用 swagger-php)生成我可能的查询参数。我知道我可以为每条路由输入所有可能的参数选项,但我确实需要能够使用注释自动从类的属性中生成可能的参数,就像我可以为请求正文做的那样。(我们将拥有大量的类/路径,并且除非它们像请求正文/JsonContent 那样生成,否则很可能不会发生这种情况。这对于 swagger-php 甚至是一般的 OpenAPI 是否可行?

我让它与 put 和 request body 一起工作,但是对于仍然使用类属性的 get 请求,我该如何做呢?

我可以为请求正文执行此操作:

    /**
     * @return Response
     *
     * * @OA\Put(
     *     path="/persons",
     *     tags={"Person"},
     *     @OA\RequestBody(
     *          request="person",
     *          required=false,
     *          description="Optional Request Parameters for Querying",
     *          @OA\JsonContent(ref="#/components/schemas/Person")
     *      ),
     *     @OA\Response(
     *          response="200",
     *          description="Returns matching Person Object",
     *          @OA\JsonContent(
     *              type="array",
     *              @OA\Items(ref="#/components/schemas/Person")
     *          )
     *     )
     * )
     */

写出 30 多个类的每个参数将无法维护:

     /** @OA\Get(
     *     path="/events",
     *     tags={"Events"},
     *     @OA\Parameter(
     *          name="eventID",
     *          in="query",
     *          required=false,
     *          description="The event ID specific to this event",
     *          @OA\Schema(
     *              type="string"
     *          ),
     *     ),
    *
   * ....etc
4

1 回答 1

0

Swagger-PHP 需要注释来记录查询参数。@OA\Parameter您可以通过添加可以使用引用的顶级注释在一定程度上减少代码重复如此处和此处$ref="#/components/parameters/PARAM_NAME"所示。

/**
 * @OA\Parameter(
 *   parameter="eventID_in_query",
 *   name="eventID",
 *   description="The event ID specific to this event",
 *   @OA\Schema(
 *     type="string"
 *   ),
 *   in="query",
 *   required=false
 * )
 */

...

     /** @OA\Get(
     *     path="/events",
     *     tags={"Events"},
     *     @OA\Parameter(ref="#/components/parameters/eventID_in_query"),
于 2019-03-25T16:47:43.817 回答