Skip to content

Commit 16ebf43

Browse files
committed
bug #27389 [Serializer] Fix serializer tries to denormalize null values on nullable properties (ogizanagi)
This PR was merged into the 3.4 branch. Discussion ---------- [Serializer] Fix serializer tries to denormalize null values on nullable properties | Q | A | ------------- | --- | Branch? | 3.4 <!-- see below --> | Bug fix? | yes | New feature? | no <!-- don't forget to update src/**/CHANGELOG.md files --> | BC breaks? | no <!-- see https://symfony.com/bc --> | Deprecations? | no <!-- don't forget to update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tests pass? | yes <!-- please add some, will be required by reviewers --> | Fixed tickets | #27384 <!-- #-prefixed issue number(s), if any --> | License | MIT | Doc PR | N/A Commits ------- ca31488 [Serializer] Fix serializer tries to denormalize null values on nullable properties
2 parents 28c8c85 + ca31488 commit 16ebf43

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed

src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,12 @@ protected function instantiateObject(array &$data, $class, array &$context, \Ref
349349
}
350350
} elseif ($allowed && !$ignored && (isset($data[$key]) || array_key_exists($key, $data))) {
351351
$parameterData = $data[$key];
352+
if (null === $parameterData && $constructorParameter->allowsNull()) {
353+
$params[] = null;
354+
// Don't run set for a parameter passed to the constructor
355+
unset($data[$key]);
356+
continue;
357+
}
352358
try {
353359
if (null !== $constructorParameter->getClass()) {
354360
if (!$this->serializer instanceof DenormalizerInterface) {
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
class NullableConstructorArgumentDummy
15+
{
16+
private $foo;
17+
18+
public function __construct(?\stdClass $foo)
19+
{
20+
$this->foo = $foo;
21+
}
22+
23+
public function setFoo($foo)
24+
{
25+
$this->foo = 'this setter should not be called when using the constructor argument';
26+
}
27+
28+
public function getFoo()
29+
{
30+
return $this->foo;
31+
}
32+
}

src/Symfony/Component/Serializer/Tests/Normalizer/AbstractNormalizerTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
1010
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
1111
use Symfony\Component\Serializer\Tests\Fixtures\AbstractNormalizerDummy;
12+
use Symfony\Component\Serializer\Tests\Fixtures\NullableConstructorArgumentDummy;
1213
use Symfony\Component\Serializer\Tests\Fixtures\ProxyDummy;
1314
use Symfony\Component\Serializer\Tests\Fixtures\StaticConstructorDummy;
1415
use Symfony\Component\Serializer\Tests\Fixtures\StaticConstructorNormalizer;
@@ -116,4 +117,15 @@ public function testObjectWithStaticConstructor()
116117
$this->assertEquals('baz', $dummy->quz);
117118
$this->assertNull($dummy->foo);
118119
}
120+
121+
/**
122+
* @requires PHP 7.1
123+
*/
124+
public function testObjectWithNullableConstructorArgument()
125+
{
126+
$normalizer = new ObjectNormalizer();
127+
$dummy = $normalizer->denormalize(array('foo' => null), NullableConstructorArgumentDummy::class);
128+
129+
$this->assertNull($dummy->getFoo());
130+
}
119131
}

0 commit comments

Comments
 (0)