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 4ed438e
Show file tree
Hide file tree
Showing 2 changed files with 31 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 @@ -16,6 +16,8 @@
use Symfony\Component\PropertyInfo\Extractor\PhpDocExtractor;
use Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor;
use Symfony\Component\PropertyInfo\PropertyInfoExtractor;
use Symfony\Component\Serializer\Mapping\AttributeMetadata;
use Symfony\Component\Serializer\Mapping\ClassMetadata;
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;
use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader;
use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter;
Expand Down Expand Up @@ -315,6 +317,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 +962,9 @@ class ObjectOuter
{
public $foo;
public $bar;
/**
* @var ObjectInner
*/
private $inner;
private $date;

Expand Down

0 comments on commit 4ed438e

Please sign in to comment.