[Serializer] Allow to use proxies in object_to_populate

This commit is contained in:
Kévin Dunglas 2016-01-11 10:35:19 +01:00
parent 4d536bd09a
commit b16b5b9d86
4 changed files with 64 additions and 1 deletions

View File

@ -296,7 +296,7 @@ abstract class AbstractNormalizer extends SerializerAwareNormalizer implements N
if (
isset($context['object_to_populate']) &&
is_object($context['object_to_populate']) &&
$class === get_class($context['object_to_populate'])
$context['object_to_populate'] instanceof $class
) {
return $context['object_to_populate'];
}

View File

@ -0,0 +1,19 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Serializer\Tests\Fixtures;
/**
* @author Kévin Dunglas <dunglas@gmail.com>
*/
class ProxyDummy extends ToBeProxyfiedDummy
{
}

View File

@ -0,0 +1,30 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Serializer\Tests\Fixtures;
/**
* @author Kévin Dunglas <dunglas@gmail.com>
*/
class ToBeProxyfiedDummy
{
private $foo;
public function setFoo($foo)
{
$this->foo = $foo;
}
public function getFoo()
{
return $this->foo;
}
}

View File

@ -5,7 +5,9 @@ namespace Symfony\Component\Serializer\Tests\Normalizer;
use Symfony\Component\Serializer\Mapping\AttributeMetadata;
use Symfony\Component\Serializer\Mapping\ClassMetadata;
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\Tests\Fixtures\AbstractNormalizerDummy;
use Symfony\Component\Serializer\Tests\Fixtures\ProxyDummy;
/**
* Provides a dummy Normalizer which extends the AbstractNormalizer.
@ -88,4 +90,16 @@ class AbstractNormalizerTest extends \PHPUnit_Framework_TestCase
$result = $this->normalizer->getAllowedAttributes('c', array('groups' => array('other')), false);
$this->assertEquals(array($a3, $a4), $result);
}
public function testObjectToPopulateWithProxy()
{
$proxyDummy = new ProxyDummy();
$context = array('object_to_populate' => $proxyDummy);
$normalizer = new ObjectNormalizer();
$normalizer->denormalize(array('foo' => 'bar'), 'Symfony\Component\Serializer\Tests\Fixtures\ToBeProxyfiedDummy', null, $context);
$this->assertSame('bar', $proxyDummy->getFoo());
}
}