Skip to content

Commit 667db5d

Browse files
committed
bug #51475 [Serializer] Fix union of enum denormalization (mtarld)
This PR was merged into the 6.3 branch. Discussion ---------- [Serializer] Fix union of enum denormalization | Q | A | ------------- | --- | Branch? | 6.3 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | Fix #47797 | License | MIT | Doc PR | Try other types when a `InvalidArgumentException` occurs for a union type. Commits ------- 11378ef [Serializer] Fix union of enum denormalization
2 parents cd5af8b + 11378ef commit 667db5d

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-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: 36 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,21 @@ public function __sleep(): array
11861204
throw new \Error('not serializable');
11871205
}
11881206
}
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

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