Skip to content

Commit 7f755cd

Browse files
committed
bug #17328 [Serializer] Allow to use proxies in object_to_populate (dunglas)
This PR was merged into the 2.7 branch. Discussion ---------- [Serializer] Allow to use proxies in object_to_populate | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #15627, api-platform/core#381 | License | MIT | Doc PR | n/a Allows to populate a proxy (or any class having the given type). Commits ------- b16b5b9 [Serializer] Allow to use proxies in object_to_populate
2 parents e6dbd24 + 1cfc7c4 commit 7f755cd

File tree

4 files changed

+64
-1
lines changed

4 files changed

+64
-1
lines changed

Normalizer/AbstractNormalizer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ protected function instantiateObject(array &$data, $class, array &$context, \Ref
298298
if (
299299
isset($context['object_to_populate']) &&
300300
is_object($context['object_to_populate']) &&
301-
$class === get_class($context['object_to_populate'])
301+
$context['object_to_populate'] instanceof $class
302302
) {
303303
$object = $context['object_to_populate'];
304304
unset($context['object_to_populate']);

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)