[Serializer] Fix serializer tries to denormalize null values on nullable properties
This commit is contained in:
parent
6fc7fdb182
commit
ca314889e7
@ -349,6 +349,12 @@ abstract class AbstractNormalizer extends SerializerAwareNormalizer implements N
|
|||||||
}
|
}
|
||||||
} elseif ($allowed && !$ignored && (isset($data[$key]) || array_key_exists($key, $data))) {
|
} elseif ($allowed && !$ignored && (isset($data[$key]) || array_key_exists($key, $data))) {
|
||||||
$parameterData = $data[$key];
|
$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 {
|
try {
|
||||||
if (null !== $constructorParameter->getClass()) {
|
if (null !== $constructorParameter->getClass()) {
|
||||||
if (!$this->serializer instanceof DenormalizerInterface) {
|
if (!$this->serializer instanceof DenormalizerInterface) {
|
||||||
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
@ -9,6 +9,7 @@ use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
|
|||||||
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
|
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
|
||||||
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
|
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
|
||||||
use Symfony\Component\Serializer\Tests\Fixtures\AbstractNormalizerDummy;
|
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\ProxyDummy;
|
||||||
use Symfony\Component\Serializer\Tests\Fixtures\StaticConstructorDummy;
|
use Symfony\Component\Serializer\Tests\Fixtures\StaticConstructorDummy;
|
||||||
use Symfony\Component\Serializer\Tests\Fixtures\StaticConstructorNormalizer;
|
use Symfony\Component\Serializer\Tests\Fixtures\StaticConstructorNormalizer;
|
||||||
@ -116,4 +117,15 @@ class AbstractNormalizerTest extends TestCase
|
|||||||
$this->assertEquals('baz', $dummy->quz);
|
$this->assertEquals('baz', $dummy->quz);
|
||||||
$this->assertNull($dummy->foo);
|
$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());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user