. */ declare(strict_types=1); namespace App\Serializer\APIPlatform; use Symfony\Component\Serializer\Normalizer\DenormalizerAwareInterface; use Symfony\Component\Serializer\Normalizer\DenormalizerAwareTrait; use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; /** * The idea of this denormalizer is to allow to override the type of the object created using a certain context key. * This is required to resolve the issue of the serializer/API platform not correctly being able to determine the type * of the "element" properties of the Attachment and Parameter subclasses. */ class OverrideClassDenormalizer implements DenormalizerInterface, DenormalizerAwareInterface { use DenormalizerAwareTrait; public const CONTEXT_KEY = '__override_type__'; public function denormalize(mixed $data, string $type, ?string $format = null, array $context = []): mixed { //Deserialize the data with the overridden type $overrideType = $context[self::CONTEXT_KEY]; unset($context[self::CONTEXT_KEY]); return $this->denormalizer->denormalize($data, $overrideType, $format, $context); } public function supportsDenormalization(mixed $data, string $type, ?string $format = null, array $context = []): bool { return isset($context[self::CONTEXT_KEY]); } public function getSupportedTypes(?string $format): array { return [ '*' => false, ]; } }