Skip to content

Commit

Permalink
prevent mixup of the object to populate
Browse files Browse the repository at this point in the history
  • Loading branch information
dbu committed Apr 7, 2019
1 parent c82e2df commit fdb668e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
Expand Up @@ -330,6 +330,8 @@ protected function instantiateObject(array &$data, $class, array &$context, \Ref

return $object;
}
// clean up even if no match
unset($context[static::OBJECT_TO_POPULATE]);

$constructor = $this->getConstructor($data, $class, $context, $reflectionClass, $allowedAttributes);
if ($constructor) {
Expand Down
Expand Up @@ -315,6 +315,30 @@ public function testGroupsDenormalizeWithNameConverter()
);
}

public function testObjectToPopulateNoMatch()
{
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
$this->normalizer = new ObjectNormalizer($classMetadataFactory, null, null, new PhpDocExtractor());
new Serializer([$this->normalizer]);

$objectToPopulate = new ObjectInner();
$objectToPopulate->foo = 'foo';

$outer = $this->normalizer->denormalize([
'foo' => 'foo',
'inner' => [
'bar' => 'bar',
],
], ObjectOuter::class, null, [ObjectNormalizer::OBJECT_TO_POPULATE => $objectToPopulate]);

$this->assertInstanceOf(ObjectOuter::class, $outer);
$inner = $outer->getInner();
$this->assertInstanceOf(ObjectInner::class, $inner);
$this->assertNotSame($objectToPopulate, $inner);
$this->assertSame('bar', $inner->bar);
$this->assertNull($inner->foo);
}

/**
* @dataProvider provideCallbacks
*/
Expand Down Expand Up @@ -936,6 +960,9 @@ class ObjectOuter
{
public $foo;
public $bar;
/**
* @var ObjectInner
*/
private $inner;
private $date;

Expand Down

0 comments on commit fdb668e

Please sign in to comment.