Search code examples
symfonyapi-platform.com

Normalizer with Api Platform : Unable to generate an IRI for the item of type


I'm working on a project with Symfony and Api Platform and I think that I'm missing something about the serialization process.

I have an entity AccessCode with an endpoint to POST and another to GET all codes. I made an AccessCodeNormalizer to format my data. It works for the getter and my data are ok but I have an issue with the POST "Unable to generate an IRI for the item of type AccessCode".

Here are my endpoints :

#[ApiResource(
    operations: [
        new GetCollection(
            uriTemplate: '/operations/{operationId}/access-codes',
            uriVariables: ['operationId' => new Link(toProperty: 'operation', fromClass: Operation::class)],
            status: 200,
            normalizationContext: ['groups' => ['code:read:bo', 'lifecycle']]
        ),
        new Post(
            uriTemplate: '/operations/{operationId}/access-code',
            uriVariables: ['operationId' => new Link(toProperty: 'operation', fromClass: Operation::class)],
            input: AccessCodeDto::class,
            provider: ObjectProvider::class, // added a provider to avoid Doctrine to try to fetch something that does not exist
            processor: AccessCodeProcessor::class
        )
    ]
)]

And here is my Normalizer :

class AccessCodeNormalizer implements NormalizerInterface
{
    public function __construct(
        private EntityManagerInterface $entityManager,
        private AccessCodeUtils $accessCodeUtils,
        private ObjectNormalizer $normalizer
    ) {
    }

    public function normalize($object, string $format = null, array $context = [])
    {
        $object->setCode($this->accessCodeUtils->formatAccessCode($this->accessCodeUtils->decryptAccessCode($object->getCode(), $this->accessCodeUtils->decodeIv($object->getIv()))));

        return $this->normalizer->normalize($object, $format, $context);
    }

    public function supportsNormalization($data, string $format = null, array $context = [])
    {
        return $data instanceof AccessCode;
    }
}

When I dump my AccessCode id in the normalizer I have no problem and it is flush correctly in database.

enter image description here

enter image description here

AccessCodeDto :

namespace App\BackOffice\Dto;

class AccessCodeDto
{
    public ?int $amount = null;
    public ?string $wallet = null;
    public array $participantInfos;
}

Solution

  • I finally found an answer for this issue. I moved the POST endpoint from my entity to my AccessCodeDto and the response is normalize correctly without error.

    I'm curious to know why it doesn't work on the entity if anyone understands where the error may be coming from.