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 fdb668e

Browse filesBrowse files
committed
prevent mixup of the object to populate
1 parent c82e2df commit fdb668e
Copy full SHA for fdb668e

File tree

2 files changed

+29
-0
lines changed
Filter options

2 files changed

+29
-0
lines changed

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

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

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