Skip to content

Commit dfffe58

Browse files
bug #57928 [Serializer] fix denormalizing mixed collection values (rynhndrcksn)
This PR was merged into the 7.1 branch. Discussion ---------- [Serializer] fix denormalizing mixed collection values | Q | A | ------------- | --- | Branch? | 7.1 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | Fix #57914 | License | MIT Commits ------- f42ff39 fix denormalizing mixed collection values
2 parents ba10748 + f42ff39 commit dfffe58

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -767,6 +767,12 @@ private function validateAndDenormalize(Type $type, string $currentClass, string
767767
$class = null;
768768
}
769769
}
770+
} elseif ($t instanceof ObjectType) {
771+
$typeIdentifier = TypeIdentifier::OBJECT;
772+
$class = $t->getClassName();
773+
} else {
774+
$typeIdentifier = $t->getTypeIdentifier();
775+
$class = null;
770776
}
771777
} else {
772778
if ($t instanceof ObjectType) {

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1224,6 +1224,29 @@ public function provideDenormalizeWithFilterBoolData(): array
12241224
[['foo' => 'something'], null],
12251225
];
12261226
}
1227+
1228+
public function testDenormalizeArrayObject()
1229+
{
1230+
$normalizer = new class() extends AbstractObjectNormalizerDummy {
1231+
public function __construct()
1232+
{
1233+
parent::__construct(null, null, new PhpDocExtractor());
1234+
}
1235+
1236+
protected function isAllowedAttribute($classOrObject, string $attribute, ?string $format = null, array $context = []): bool
1237+
{
1238+
return true;
1239+
}
1240+
};
1241+
$serializer = new Serializer([$normalizer]);
1242+
$normalizer->setSerializer($serializer);
1243+
1244+
$actual = $normalizer->denormalize(['foo' => ['array' => ['key' => 'value']]], DummyWithArrayObject::class);
1245+
1246+
$this->assertInstanceOf(DummyWithArrayObject::class, $actual);
1247+
$this->assertInstanceOf(\ArrayObject::class, $actual->foo);
1248+
$this->assertSame(1, $actual->foo->count());
1249+
}
12271250
}
12281251

12291252
class AbstractObjectNormalizerDummy extends AbstractObjectNormalizer
@@ -1515,6 +1538,12 @@ class BoolPropertyDummy
15151538
public $foo;
15161539
}
15171540

1541+
class DummyWithArrayObject
1542+
{
1543+
/** @var \ArrayObject<string, mixed> */
1544+
public $foo;
1545+
}
1546+
15181547
class SerializerCollectionDummy implements SerializerInterface, DenormalizerInterface
15191548
{
15201549
private array $normalizers;

0 commit comments

Comments
 (0)