Skip to content

Commit 621c02e

Browse files
Merge branch '2.8' into 3.0
* 2.8: [Yaml] Fix tests on PHP 7.0.2 [FrameworkBundle] Don't log twice with the error handler [2.7] Workaround https://bugs.php.net/63206 [2.3] Workaround https://bugs.php.net/63206 Add closing parenthesis [Serializer] Unset object_to_populate after using it [Serializer] Allow to use proxies in object_to_populate Conflicts: src/Symfony/Component/Debug/ErrorHandler.php src/Symfony/Component/HttpFoundation/JsonResponse.php
2 parents 01ef10f + 60d6ea5 commit 621c02e

File tree

4 files changed

+71
-3
lines changed

4 files changed

+71
-3
lines changed

Normalizer/AbstractNormalizer.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,9 @@ protected function prepareForDenormalization($data)
224224
* Instantiates an object using constructor parameters when needed.
225225
*
226226
* This method also allows to denormalize data into an existing object if
227-
* it is present in the context with the object_to_populate key.
227+
* it is present in the context with the object_to_populate. This object
228+
* is removed from the context before being returned to avoid side effects
229+
* when recursively normalizing an object graph.
228230
*
229231
* @param array $data
230232
* @param string $class
@@ -241,9 +243,12 @@ protected function instantiateObject(array &$data, $class, array &$context, \Ref
241243
if (
242244
isset($context['object_to_populate']) &&
243245
is_object($context['object_to_populate']) &&
244-
$class === get_class($context['object_to_populate'])
246+
$context['object_to_populate'] instanceof $class
245247
) {
246-
return $context['object_to_populate'];
248+
$object = $context['object_to_populate'];
249+
unset($context['object_to_populate']);
250+
251+
return $object;
247252
}
248253

249254
$constructor = $reflectionClass->getConstructor();

Tests/Fixtures/ProxyDummy.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Serializer\Tests\Fixtures;
13+
14+
/**
15+
* @author Kévin Dunglas <dunglas@gmail.com>
16+
*/
17+
class ProxyDummy extends ToBeProxyfiedDummy
18+
{
19+
}

Tests/Fixtures/ToBeProxyfiedDummy.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Serializer\Tests\Fixtures;
13+
14+
/**
15+
* @author Kévin Dunglas <dunglas@gmail.com>
16+
*/
17+
class ToBeProxyfiedDummy
18+
{
19+
private $foo;
20+
21+
public function setFoo($foo)
22+
{
23+
$this->foo = $foo;
24+
}
25+
26+
public function getFoo()
27+
{
28+
return $this->foo;
29+
}
30+
}

Tests/Normalizer/AbstractNormalizerTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
use Symfony\Component\Serializer\Mapping\AttributeMetadata;
66
use Symfony\Component\Serializer\Mapping\ClassMetadata;
77
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
8+
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
89
use Symfony\Component\Serializer\Tests\Fixtures\AbstractNormalizerDummy;
10+
use Symfony\Component\Serializer\Tests\Fixtures\ProxyDummy;
911

1012
/**
1113
* Provides a dummy Normalizer which extends the AbstractNormalizer.
@@ -88,4 +90,16 @@ public function testGetAllowedAttributesAsObjects()
8890
$result = $this->normalizer->getAllowedAttributes('c', array('groups' => array('other')), false);
8991
$this->assertEquals(array($a3, $a4), $result);
9092
}
93+
94+
public function testObjectToPopulateWithProxy()
95+
{
96+
$proxyDummy = new ProxyDummy();
97+
98+
$context = array('object_to_populate' => $proxyDummy);
99+
100+
$normalizer = new ObjectNormalizer();
101+
$normalizer->denormalize(array('foo' => 'bar'), 'Symfony\Component\Serializer\Tests\Fixtures\ToBeProxyfiedDummy', null, $context);
102+
103+
$this->assertSame('bar', $proxyDummy->getFoo());
104+
}
91105
}

0 commit comments

Comments
 (0)