Moving from nelmio/api-doc-bundle's PHP annotations to attributes with Rector

212 Views Asked by At

I am working on a Symfony REST API project and trying to update the framework and all my dependencies to the latest version. I recently moved to PHP 8.3 and am trying to embrace the new PHP attributes to replace most of the annotations.

Rector helps with that, providing some predefined sets (DoctrineSetList::ANNOTATIONS_TO_ATTRIBUTES, SymfonySetList::ANNOTATIONS_TO_ATTRIBUTES). However, all the endpoints of my API are defined using nelmio/open-api-bundle annotations, that I would like to migrate to attributes. For example, I would like to refactor this code:

    /**
     * @OA\Response(
     *     response=200,
     *     description="Returns paginated list of filtered users",
     *     @OA\JsonContent(ref=@Model(type=UserListOutputModel::class))
     * )
     *
     * @QueryParam(name="page", default=1, description="Page number")
     * @QueryParam(name="itemsPerPage", default=10, description="Items per page")
     * @QueryParam(name="username", requirements="[a-zA-Z0-9]+", description="The username")
     *
     * @Get("api/user", name="user_search", defaults={ "_format" = "json" })
     */

into this:

    #[OA\Response(
        response: Response::HTTP_OK,
        description: "Returns paginated list of filtered users",
        content: new OA\JsonContent(ref: new Model(type: UserListOutputModel::class))
    )]
    #[QueryParam(name:"page", default:1, description:"Page number")]
    #[QueryParam(name:"itemsPerPage", default:10, description:"Items per page")]
    #[QueryParam(name:"username", requirements: "[a-zA-Z0-9]+", description:"Username to search")]
    #[Get("api/user", name:"user_search", defaults:["_format" => "json"])]

For reference, I have been following this discussion on GitHub: https://github.com/zircote/swagger-php/issues/1047 where some people suggest custom configured rules, but I have been unable to automate this process.

Does anybody have an idea on how to do this (besides manually)? Any help is appreciated

0

There are 0 best solutions below