|
37 | 37 | use Symfony\Component\Serializer\NameConverter\MetadataAwareNameConverter;
|
38 | 38 | use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
|
39 | 39 | use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer;
|
| 40 | +use Symfony\Component\Serializer\Normalizer\BackedEnumNormalizer; |
40 | 41 | use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
|
41 | 42 | use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
|
42 | 43 | use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
|
@@ -769,6 +770,23 @@ public function supportsNormalization(mixed $data, string $format = null, array
|
769 | 770 |
|
770 | 771 | $this->assertSame('called', $object->bar);
|
771 | 772 | }
|
| 773 | + |
| 774 | + public function testDenormalizeUnionOfEnums() |
| 775 | + { |
| 776 | + $serializer = new Serializer([ |
| 777 | + new BackedEnumNormalizer(), |
| 778 | + new ObjectNormalizer( |
| 779 | + classMetadataFactory: new ClassMetadataFactory(new AnnotationLoader()), |
| 780 | + propertyTypeExtractor: new PropertyInfoExtractor([], [new ReflectionExtractor()]), |
| 781 | + ), |
| 782 | + ]); |
| 783 | + |
| 784 | + $normalized = $serializer->normalize(new DummyWithEnumUnion(EnumA::A)); |
| 785 | + $this->assertEquals(new DummyWithEnumUnion(EnumA::A), $serializer->denormalize($normalized, DummyWithEnumUnion::class)); |
| 786 | + |
| 787 | + $normalized = $serializer->normalize(new DummyWithEnumUnion(EnumB::B)); |
| 788 | + $this->assertEquals(new DummyWithEnumUnion(EnumB::B), $serializer->denormalize($normalized, DummyWithEnumUnion::class)); |
| 789 | + } |
772 | 790 | }
|
773 | 791 |
|
774 | 792 | class AbstractObjectNormalizerDummy extends AbstractObjectNormalizer
|
@@ -1186,3 +1204,21 @@ public function __sleep(): array
|
1186 | 1204 | throw new \Error('not serializable');
|
1187 | 1205 | }
|
1188 | 1206 | }
|
| 1207 | + |
| 1208 | +enum EnumA: string |
| 1209 | +{ |
| 1210 | + case A = 'a'; |
| 1211 | +} |
| 1212 | + |
| 1213 | +enum EnumB: string |
| 1214 | +{ |
| 1215 | + case B = 'b'; |
| 1216 | +} |
| 1217 | + |
| 1218 | +class DummyWithEnumUnion |
| 1219 | +{ |
| 1220 | + public function __construct( |
| 1221 | + public readonly EnumA|EnumB $enum, |
| 1222 | + ) { |
| 1223 | + } |
| 1224 | +} |
0 commit comments