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

Appearance settings

Commit 410490c

Browse filesBrowse files
bug #52804 [Serializer] Fix support of plain object types denormalization (andersonamuller)
This PR was merged into the 5.4 branch. Discussion ---------- [Serializer] Fix support of plain object types denormalization | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | | License | MIT When denormalizing properties with a plain `object` type without a class the `ObjectNormalizer` would fail with a `TypeError`. Commits ------- 4a728f7 Fix support to denormalize plain object types
2 parents ac47f07 + 4a728f7 commit 410490c
Copy full SHA for 410490c

File tree

2 files changed

+18
-1
lines changed
Filter options

2 files changed

+18
-1
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php
+1-1Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Tests/Normalizer/AbstractObjectNormalizerTest.php
+17Lines 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)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.