How to use external model definitions in nelmio_api_doc

I am trying to use shared obejcts for my documentation but It does not appear to work.

Based on nelmio decumentation I did the following:

On my serailizer:

/**
 * @OASchema(
 *     @OAProperty(property="id", type="string", example="00000000-0000-0000-0000-000000000000"),
 *     @OAProperty(property="email", type="string", example="[email protected]"),
 *     @OAProperty(property="status", type="string", example="active"),
 *     @OAProperty(property="roles", type="array", [email protected](type="string"), example={"ROLE_USER", "ROLE_ADMIN"}),
 * )
 */
class AdminUserTransformer extends TransformerAbstract
{
    public function transform(AdminUser $admin)
    {
        return [
            'id' => $admin->getId(),
            'email' => $admin->getEmail(),
            'status' => $admin->getStatus(),
            'roles' => $admin->getRoles(),
        ];
    }
}

Then in controller:

    /**
     * @Route("/admin-user/{id}", methods={"GET"})
     *
     * @OAResponse(response="200", description="Success", @Model(type=AdminUserTransformer::class))
     *
     * @OATag(name="Admin users")
     */
    public function read($id)
    {
        $admin = $this->queryBus->dispatch(new GetAdminUser($id));

        return $this->fractal->create($admin, AdminUserTransformer::class);
    }

But instead of getting my model I am getting error that makes no sense:

Property "Resources that can be included if requested." is an array, but its items type isn't specified. You can specify that by using the type `string[]` for instance or `@OAProperty(type="array", @OAItems(type="string"))`

Inserting the documentation directly, without the reference works fine:

    /**
     * @Route("/{id}", methods={"GET"})
     *
     * @OAResponse(response="200",
     *     @OAJsonContent(
     *         @OAProperty(property="id", type="string", example="00000000-0000-0000-0000-000000000000"),
     *         @OAProperty(property="email", type="string", example="[email protected]"),
     *         @OAProperty(property="status", type="string", example="active"),
     *         @OAProperty(property="roles", type="array", [email protected](type="string"), example={"ROLE_USER", "ROLE_ADMIN"}),
     *     )
     * )
     *
     * @OATag(name="Admin users")
     */
    public function read($id)
    {
        $admin = $this->queryBus->dispatch(new GetAdminUser($id));

        return $this->fractal->create($admin, AdminUserTransformer::class);
    }

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *