Skip to content

Commit

Permalink
[Serializer] Fix serializer tries to denormalize null values on nulla…
Browse files Browse the repository at this point in the history
…ble properties
  • Loading branch information
ogizanagi committed May 26, 2018
1 parent 6fc7fdb commit ca31488
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
Expand Up @@ -349,6 +349,12 @@ protected function instantiateObject(array &$data, $class, array &$context, \Ref
}
} elseif ($allowed && !$ignored && (isset($data[$key]) || array_key_exists($key, $data))) {
$parameterData = $data[$key];
if (null === $parameterData && $constructorParameter->allowsNull()) {
$params[] = null;
// Don't run set for a parameter passed to the constructor
unset($data[$key]);
continue;
}
try {
if (null !== $constructorParameter->getClass()) {
if (!$this->serializer instanceof DenormalizerInterface) {
Expand Down
@@ -0,0 +1,32 @@
<?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;

class NullableConstructorArgumentDummy
{
private $foo;

public function __construct(?\stdClass $foo)
{
$this->foo = $foo;
}

public function setFoo($foo)
{
$this->foo = 'this setter should not be called when using the constructor argument';
}

public function getFoo()
{
return $this->foo;
}
}
Expand Up @@ -9,6 +9,7 @@
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\Tests\Fixtures\AbstractNormalizerDummy;
use Symfony\Component\Serializer\Tests\Fixtures\NullableConstructorArgumentDummy;
use Symfony\Component\Serializer\Tests\Fixtures\ProxyDummy;
use Symfony\Component\Serializer\Tests\Fixtures\StaticConstructorDummy;
use Symfony\Component\Serializer\Tests\Fixtures\StaticConstructorNormalizer;
Expand Down Expand Up @@ -116,4 +117,15 @@ public function testObjectWithStaticConstructor()
$this->assertEquals('baz', $dummy->quz);
$this->assertNull($dummy->foo);
}

/**
* @requires PHP 7.1
*/
public function testObjectWithNullableConstructorArgument()
{
$normalizer = new ObjectNormalizer();
$dummy = $normalizer->denormalize(array('foo' => null), NullableConstructorArgumentDummy::class);

$this->assertNull($dummy->getFoo());
}
}

0 comments on commit ca31488

Please sign in to comment.