|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace App\Serializer; |
| 6 | + |
| 7 | +use Symfony\Component\DependencyInjection\Attribute\AsDecorator; |
| 8 | +use Symfony\Component\Serializer\Exception\NotNormalizableValueException; |
| 9 | +use Symfony\Component\Serializer\Normalizer\BackedEnumNormalizer; |
| 10 | +use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; |
| 11 | +use Symfony\Component\Serializer\Normalizer\NormalizerInterface; |
| 12 | + |
| 13 | +/** |
| 14 | + * Reproducer for https://github.com/api-platform/demo/issues/601. |
| 15 | + * |
| 16 | + * This decorator simulates the behavior introduced in symfony/serializer by |
| 17 | + * Symfony PR #62574 (commit 35b1aec), which improves BackedEnumNormalizer |
| 18 | + * error messages. That change was temporarily in v8.0.5 (then reverted) and |
| 19 | + * will ship in Symfony 8.1. |
| 20 | + * |
| 21 | + * The improved normalizer distinguishes two error cases: |
| 22 | + * 1. Type mismatch: data is not int/string → expectedTypes = [$backingType] |
| 23 | + * 2. Invalid value: data is the right type but not a valid enum case |
| 24 | + * → expectedTypes = null, message lists valid values |
| 25 | + * |
| 26 | + * Case 2 exposes a bug in api-platform/state's DeserializeProvider which does |
| 27 | + * not handle null/empty expectedTypes, producing: "This value should be of type ." |
| 28 | + * |
| 29 | + * @todo Remove this decorator once Symfony 8.1 is adopted and the upstream |
| 30 | + * API Platform bug is fixed. |
| 31 | + */ |
| 32 | +#[AsDecorator('serializer.normalizer.backed_enum')] |
| 33 | +final class BackedEnumNormalizerDecorator implements NormalizerInterface, DenormalizerInterface |
| 34 | +{ |
| 35 | + public function __construct( |
| 36 | + private readonly BackedEnumNormalizer $inner, |
| 37 | + ) { |
| 38 | + } |
| 39 | + |
| 40 | + public function normalize(mixed $data, ?string $format = null, array $context = []): int|string |
| 41 | + { |
| 42 | + return $this->inner->normalize($data, $format, $context); |
| 43 | + } |
| 44 | + |
| 45 | + public function supportsNormalization(mixed $data, ?string $format = null, array $context = []): bool |
| 46 | + { |
| 47 | + return $this->inner->supportsNormalization($data, $format, $context); |
| 48 | + } |
| 49 | + |
| 50 | + public function denormalize(mixed $data, string $type, ?string $format = null, array $context = []): mixed |
| 51 | + { |
| 52 | + if (!is_subclass_of($type, \BackedEnum::class)) { |
| 53 | + return $this->inner->denormalize($data, $type, $format, $context); |
| 54 | + } |
| 55 | + |
| 56 | + $backingType = (new \ReflectionEnum($type))->getBackingType()?->getName(); |
| 57 | + |
| 58 | + // Case 1: Type mismatch — data is not the expected backing type |
| 59 | + if (null === $data || ('int' === $backingType && !\is_int($data)) || ('string' === $backingType && !\is_string($data))) { |
| 60 | + throw NotNormalizableValueException::createForUnexpectedDataType( |
| 61 | + \sprintf('The data must be of type %s.', $backingType), |
| 62 | + $data, |
| 63 | + [$backingType], |
| 64 | + $context['deserialization_path'] ?? null, |
| 65 | + true, |
| 66 | + ); |
| 67 | + } |
| 68 | + |
| 69 | + // Case 2: Invalid value — right type but not a valid enum case |
| 70 | + try { |
| 71 | + return $type::from($data); |
| 72 | + } catch (\ValueError|\TypeError $e) { |
| 73 | + $validValues = array_map( |
| 74 | + static fn (\BackedEnum $case): string => \is_string($case->value) |
| 75 | + ? \sprintf("'%s'", $case->value) |
| 76 | + : (string) $case->value, |
| 77 | + $type::cases(), |
| 78 | + ); |
| 79 | + |
| 80 | + throw new NotNormalizableValueException( |
| 81 | + message: \sprintf('The data must be one of the following values: %s', implode(', ', $validValues)), |
| 82 | + previous: $e, |
| 83 | + path: $context['deserialization_path'] ?? null, |
| 84 | + useMessageForUser: true, |
| 85 | + ); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + public function supportsDenormalization(mixed $data, string $type, ?string $format = null, array $context = []): bool |
| 90 | + { |
| 91 | + return $this->inner->supportsDenormalization($data, $type, $format, $context); |
| 92 | + } |
| 93 | + |
| 94 | + public function getSupportedTypes(?string $format): array |
| 95 | + { |
| 96 | + return $this->inner->getSupportedTypes($format); |
| 97 | + } |
| 98 | +} |
0 commit comments