Skip to content

Commit fe49e3b

Browse files
committed
[Serializer] Fix union of enum denormalization
1 parent 897a054 commit fe49e3b

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
namespace Symfony\Component\Serializer\Normalizer;
1313

14-
use Symfony\Component\PropertyAccess\Exception\InvalidArgumentException;
14+
use Symfony\Component\PropertyAccess\Exception\InvalidArgumentException as PropertyAccessInvalidArgumentException;
1515
use Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException;
1616
use Symfony\Component\PropertyAccess\Exception\UninitializedPropertyException;
1717
use Symfony\Component\PropertyAccess\PropertyAccess;
@@ -21,6 +21,7 @@
2121
use Symfony\Component\Serializer\Encoder\JsonEncoder;
2222
use Symfony\Component\Serializer\Encoder\XmlEncoder;
2323
use Symfony\Component\Serializer\Exception\ExtraAttributesException;
24+
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
2425
use Symfony\Component\Serializer\Exception\LogicException;
2526
use Symfony\Component\Serializer\Exception\MissingConstructorArgumentsException;
2627
use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
@@ -387,7 +388,7 @@ public function denormalize(mixed $data, string $type, string $format = null, ar
387388

388389
try {
389390
$this->setAttributeValue($object, $attribute, $value, $format, $attributeContext);
390-
} catch (InvalidArgumentException $e) {
391+
} catch (PropertyAccessInvalidArgumentException $e) {
391392
$exception = NotNormalizableValueException::createForUnexpectedDataType(
392393
sprintf('Failed to denormalize attribute "%s" value for class "%s": '.$e->getMessage(), $attribute, $type),
393394
$data,
@@ -562,7 +563,7 @@ private function validateAndDenormalize(array $types, string $currentClass, stri
562563
if (('is_'.$builtinType)($data)) {
563564
return $data;
564565
}
565-
} catch (NotNormalizableValueException $e) {
566+
} catch (NotNormalizableValueException|InvalidArgumentException $e) {
566567
if (!$isUnionType) {
567568
throw $e;
568569
}

src/Symfony/Component/Serializer/Tests/Normalizer/AbstractObjectNormalizerTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
use Symfony\Component\Serializer\NameConverter\MetadataAwareNameConverter;
3838
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
3939
use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer;
40+
use Symfony\Component\Serializer\Normalizer\BackedEnumNormalizer;
4041
use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
4142
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
4243
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
@@ -769,6 +770,23 @@ public function supportsNormalization(mixed $data, string $format = null, array
769770

770771
$this->assertSame('called', $object->bar);
771772
}
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+
}
772790
}
773791

774792
class AbstractObjectNormalizerDummy extends AbstractObjectNormalizer
@@ -1186,3 +1204,18 @@ public function __sleep(): array
11861204
throw new \Error('not serializable');
11871205
}
11881206
}
1207+
1208+
enum EnumA : string {
1209+
case A = 'a';
1210+
}
1211+
1212+
enum EnumB : string {
1213+
case B = 'b';
1214+
}
1215+
1216+
class DummyWithEnumUnion {
1217+
public function __construct(
1218+
public readonly EnumA|EnumB $enum,
1219+
) {
1220+
}
1221+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy