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 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;
}
}
And here is 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
)
]
)]
When I dump my AccessCode id in the normalizer I have no problem and it is flush correctly in database.