Skip to content

Commit

Permalink
bug #52804 [Serializer] Fix support of plain object types denormaliza…
Browse files Browse the repository at this point in the history
…tion (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
  • Loading branch information
nicolas-grekas committed Dec 1, 2023
2 parents ac47f07 + 4a728f7 commit 410490c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
Expand Up @@ -584,7 +584,7 @@ private function validateAndDenormalize(array $types, string $currentClass, stri

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

if (Type::BUILTIN_TYPE_OBJECT === $builtinType) {
if (Type::BUILTIN_TYPE_OBJECT === $builtinType && null !== $class) {
if (!$this->serializer instanceof DenormalizerInterface) {
throw new LogicException(sprintf('Cannot denormalize attribute "%s" for class "%s" because injected serializer is not a denormalizer.', $attribute, $class));
}
Expand Down
Expand Up @@ -113,6 +113,17 @@ public function testDenormalizeWithExtraAttributesAndNoGroupsWithMetadataFactory
);
}

public function testDenormalizePlainObject()
{
$extractor = new PhpDocExtractor();
$normalizer = new ObjectNormalizer(null, null, null, $extractor);
$dummy = $normalizer->denormalize(['plainObject' => (object) ['foo' => 'bar']], DummyWithPlainObject::class);

$this->assertInstanceOf(DummyWithPlainObject::class, $dummy);
$this->assertInstanceOf(\stdClass::class, $dummy->plainObject);
$this->assertSame('bar', $dummy->plainObject->foo);
}

public function testDenormalizeCollectionDecodedFromXmlWithOneChild()
{
$denormalizer = $this->getDenormalizerForDummyCollection();
Expand Down Expand Up @@ -598,6 +609,12 @@ protected function setAttributeValue(object $object, string $attribute, $value,
}
}

class DummyWithPlainObject
{
/** @var object */
public $plainObject;
}

class ObjectWithBasicProperties
{
/** @var bool */
Expand Down

0 comments on commit 410490c

Please sign in to comment.