10000 Fix support to denormalize plain object types · symfonyaml/symfony@4a728f7 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4a728f7

Browse files
Fix support to denormalize plain object types
When denormalizing properties with a plain `object` type without a class the `ObjectNormalizer` would fail with a `TypeError`.
1 parent 92513d3 commit 4a728f7

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,7 @@ private function validateAndDenormalize(array $types, string $currentClass, stri
584584

585585
$expectedTypes[Type::BUILTIN_TYPE_OBJECT === $builtinType && $class ? $class : $builtinType] = true;
586586

587-
if (Type::BUILTIN_TYPE_OBJECT === $builtinType) {
587+
if (Type::BUILTIN_TYPE_OBJECT === $builtinType && null !== $class) {
588588
if (!$this->serializer instanceof DenormalizerInterface) {
589589
throw new LogicException(sprintf('Cannot denormalize attribute "%s" for class "%s" because injected serializer is not a denormalizer.', $attribute, $class));
590590
}

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,17 @@ public function testDenormalizeWithExtraAttributesAndNoGroupsWithMetadataFactory
113113
);
114114
}
115115

116+
public function testDenormalizePlainObject()
117+
{
118+
$extractor = new PhpDocExtractor();
119+
$normalizer = new ObjectNormalizer(null, null, null, $extractor);
120+
$dummy = $normalizer->denormalize(['plainObject' => (object) ['foo' => 'bar']], DummyWithPlainObject::class);
121+
122+
$this->assertInstanceOf(DummyWithPlainObject::class, $dummy);
123+
$this->assertInstanceOf(\stdClass::class, $dummy->plainObject);
124+
$this->assertSame('bar', $dummy->plainObject->foo);
125+
}
126+
116127
public function testDenormalizeCollectionDecodedFromXmlWithOneChild()
117128
{
118129
$denormalizer = $this->getDenormalizerForDummyCollection();
@@ -598,6 +609,12 @@ protected function setAttributeValue(object $object, string $attribute, $value,
598609
}
599610
}
600611

612+
class DummyWithPlainObject
613+
{
614+
/** @var object */
615+
public $plainObject;
616+
}
617+
601618
class ObjectWithBasicProperties
602619
{
603620
/** @var bool */

0 commit comments

Comments
 (0)
0