Skip to content

Commit b30f57e

Browse files
committed
bug symfony#30977 [serializer] prevent mixup in normalizer of the object to populate (dbu)
This PR was merged into the 3.4 branch. Discussion ---------- [serializer] prevent mixup in normalizer of the object to populate EUFOSSA | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - OBJECT_TO_POPULATE is meant to specify the top level object. The implementation left the option in the context and it would be used whenever we have the first element that matches the class. symfony#30607 (to master) introduces the feature to also keep the instances of attributes to deeply populate an existing object tree. In both cases, we do not want the mix up to happen with what the current OBJECT_TO_POPULATE is. Commits ------- fdb668e prevent mixup of the object to populate
2 parents 48bb7c9 + fdb668e commit b30f57e

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,8 @@ protected function instantiateObject(array &$data, $class, array &$context, \Ref
330330

331331
return $object;
332332
}
333+
// clean up even if no match
334+
unset($context[static::OBJECT_TO_POPULATE]);
333335

334336
$constructor = $this->getConstructor($data, $class, $context, $reflectionClass, $allowedAttributes);
335337
if ($constructor) {

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,30 @@ public function testGroupsDenormalizeWithNameConverter()
315315
);
316316
}
317317

318+
public function testObjectToPopulateNoMatch()
319+
{
320+
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
321+
$this->normalizer = new ObjectNormalizer($classMetadataFactory, null, null, new PhpDocExtractor());
322+
new Serializer([$this->normalizer]);
323+
324+
$objectToPopulate = new ObjectInner();
325+
$objectToPopulate->foo = 'foo';
326+
327+
$outer = $this->normalizer->denormalize([
328+
'foo' => 'foo',
329+
'inner' => [
330+
'bar' => 'bar',
331+
],
332+
], ObjectOuter::class, null, [ObjectNormalizer::OBJECT_TO_POPULATE => $objectToPopulate]);
333+
334+
$this->assertInstanceOf(ObjectOuter::class, $outer);
335+
$inner = $outer->getInner();
336+
$this->assertInstanceOf(ObjectInner::class, $inner);
337+
$this->assertNotSame($objectToPopulate, $inner);
338+
$this->assertSame('bar', $inner->bar);
339+
$this->assertNull($inner->foo);
340+
}
341+
318342
/**
319343
* @dataProvider provideCallbacks
320344
*/
@@ -936,6 +960,9 @@ class ObjectOuter
936960
{
937961
public $foo;
938962
public $bar;
963+
/**
964+
* @var ObjectInner
965+
*/
939966
private $inner;
940967
private $date;
941968

0 commit comments

Comments
 (0)