I'm using API Platform
with jsonapi
format and a custom normalizer to resolve entities and their relationships. In my custom item normalizer, I'm using the Symfony\Component\Serializer\Normalizer\NormalizerInterface
class. However, I'm having trouble resolving empty relationships with the jsonapi
format normalizer after normalizing the entity. I've tried passing skip_null_values
as false but it didn't work.
Here's my custom normalizer class:
class ApiItemNormalizer implements NormalizerInterface, DenormalizerInterface, SerializerAwareInterface
{
public function __construct(private readonly NormalizerInterface $decorated, ...) { ... }
public function normalize($object, $format = null, array $context = [])
{
$responseObject = $this->decorated->normalize($object, $format, $context);
...
}
}
I would like to resolve the entity relationship whether it is empty or not. However, the jsonapi format normalizer ignores the empty relationship:
"data": [
{
"id": 2,
"name": "does not have any relation",
"created_at": "2023-04-24T11:46:16+02:00",
},
{
"id": 1,
"name": "have two relations",
"created_at": "2023-04-13T11:37:53+02:00",
"category": [
{
"name": "cat 1",
"updated_by": null,
"id": 1
},
{
"name": "cat 2",
"updated_by": null,
"id": 3
}
]
}
]
How can I resolve the category attribute in the first item with an empty array as a value?