Skip to content

Commit 54d6c0b

Browse files
committed
bug #57197 [Serializer] Fix denormalizing a collection of union types (HypeMC)
This PR was merged into the 7.1 branch. Discussion ---------- [Serializer] Fix denormalizing a collection of union types | Q | A | ------------- | --- | Branch? | 7.1 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | Fix #56874 | License | MIT Commits ------- 0691eb8 [Serializer] Fix denormalizing a collection of union types
2 parents a2890a6 + 0691eb8 commit 54d6c0b

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
use Symfony\Component\Serializer\Mapping\ClassMetadataInterface;
3333
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
3434
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
35+
use Symfony\Component\TypeInfo\Exception\LogicException as TypeInfoLogicException;
3536
use Symfony\Component\TypeInfo\Type;
3637
use Symfony\Component\TypeInfo\Type\CollectionType;
3738
use Symfony\Component\TypeInfo\Type\IntersectionType;
@@ -702,7 +703,11 @@ private function validateAndDenormalize(Type $type, string $currentClass, string
702703
}
703704

704705
if ($collectionValueType) {
705-
$collectionValueBaseType = $collectionValueType instanceof UnionType ? $collectionValueType->asNonNullable()->getBaseType() : $collectionValueType->getBaseType();
706+
try {
707+
$collectionValueBaseType = $collectionValueType->getBaseType();
708+
} catch (TypeInfoLogicException) {
709+
$collectionValueBaseType = Type::mixed();
710+
}
706711

707712
if ($collectionValueBaseType instanceof ObjectType) {
708713
$typeIdentifier = TypeIdentifier::OBJECT;

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1147,6 +1147,25 @@ public function testDenormalizeCollectionOfScalarTypesPropertyWithPhpDocExtracto
11471147

11481148
$this->assertEquals($expected, $normalizer->denormalize($data, ScalarCollectionDocBlockDummy::class));
11491149
}
1150+
1151+
public function testDenormalizeCollectionOfUnionTypesPropertyWithPhpDocExtractor()
1152+
{
1153+
$normalizer = new AbstractObjectNormalizerWithMetadataAndPhpDocExtractor();
1154+
$data = [
1155+
'values1' => [
1156+
'foo' => 'foo',
1157+
'bar' => 222,
1158+
],
1159+
'values2' => [
1160+
'baz' => 'baz',
1161+
'qux' => 333,
1162+
],
1163+
];
1164+
$expected = new UnionCollectionDocBlockDummy($data['values1']);
1165+
$expected->values2 = $data['values2'];
1166+
1167+
$this->assertEquals($expected, $normalizer->denormalize($data, UnionCollectionDocBlockDummy::class));
1168+
}
11501169
}
11511170

11521171
class AbstractObjectNormalizerDummy extends AbstractObjectNormalizer
@@ -1577,6 +1596,22 @@ public function getValues(): ?array
15771596
}
15781597
}
15791598

1599+
class UnionCollectionDocBlockDummy
1600+
{
1601+
/**
1602+
* @param array<string, string|int> $values1
1603+
*/
1604+
public function __construct(
1605+
public array $values1,
1606+
) {
1607+
}
1608+
1609+
/**
1610+
* @var array<string, string|int>
1611+
*/
1612+
public array $values2;
1613+
}
1614+
15801615
class AbstractObjectNormalizerWithMetadataAndPhpDocExtractor extends AbstractObjectNormalizer
15811616
{
15821617
public function __construct()
@@ -1596,6 +1631,9 @@ protected function getAttributeValue(object $object, string $attribute, ?string
15961631

15971632
protected function setAttributeValue(object $object, string $attribute, mixed $value, ?string $format = null, array $context = []): void
15981633
{
1634+
if (property_exists($object, $attribute)) {
1635+
$object->$attribute = $value;
1636+
}
15991637
}
16001638

16011639
public function getSupportedTypes(?string $format): array

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