Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit667db5d

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 parentscd5af8b +11378ef commit667db5d

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
namespaceSymfony\Component\Serializer\Normalizer;
1313

14-
useSymfony\Component\PropertyAccess\Exception\InvalidArgumentException;
14+
useSymfony\Component\PropertyAccess\Exception\InvalidArgumentExceptionasPropertyAccessInvalidArgumentException;
1515
useSymfony\Component\PropertyAccess\Exception\NoSuchPropertyException;
1616
useSymfony\Component\PropertyAccess\Exception\UninitializedPropertyException;
1717
useSymfony\Component\PropertyAccess\PropertyAccess;
@@ -21,6 +21,7 @@
2121
useSymfony\Component\Serializer\Encoder\JsonEncoder;
2222
useSymfony\Component\Serializer\Encoder\XmlEncoder;
2323
useSymfony\Component\Serializer\Exception\ExtraAttributesException;
24+
useSymfony\Component\Serializer\Exception\InvalidArgumentException;
2425
useSymfony\Component\Serializer\Exception\LogicException;
2526
useSymfony\Component\Serializer\Exception\MissingConstructorArgumentsException;
2627
useSymfony\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
useSymfony\Component\Serializer\NameConverter\MetadataAwareNameConverter;
3838
useSymfony\Component\Serializer\Normalizer\AbstractNormalizer;
3939
useSymfony\Component\Serializer\Normalizer\AbstractObjectNormalizer;
40+
useSymfony\Component\Serializer\Normalizer\BackedEnumNormalizer;
4041
useSymfony\Component\Serializer\Normalizer\DateTimeNormalizer;
4142
useSymfony\Component\Serializer\Normalizer\DenormalizerInterface;
4243
useSymfony\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+
publicfunctiontestDenormalizeUnionOfEnums()
775+
{
776+
$serializer =newSerializer([
777+
newBackedEnumNormalizer(),
778+
newObjectNormalizer(
779+
classMetadataFactory:newClassMetadataFactory(newAnnotationLoader()),
780+
propertyTypeExtractor:newPropertyInfoExtractor([], [newReflectionExtractor()]),
781+
),
782+
]);
783+
784+
$normalized =$serializer->normalize(newDummyWithEnumUnion(EnumA::A));
785+
$this->assertEquals(newDummyWithEnumUnion(EnumA::A),$serializer->denormalize($normalized, DummyWithEnumUnion::class));
786+
787+
$normalized =$serializer->normalize(newDummyWithEnumUnion(EnumB::B));
788+
$this->assertEquals(newDummyWithEnumUnion(EnumB::B),$serializer->denormalize($normalized, DummyWithEnumUnion::class));
789+
}
772790
}
773791

774792
class AbstractObjectNormalizerDummyextends AbstractObjectNormalizer
@@ -1186,3 +1204,21 @@ public function __sleep(): array
11861204
thrownew \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+
publicfunction__construct(
1221+
publicreadonlyEnumA|EnumB$enum,
1222+
) {
1223+
}
1224+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp