[Serializer] AbstractNormalizer force null for non-optional nullable constructor parameter denormalization when not present in input

This commit is contained in:
Pierre Rineau 2021-03-19 17:42:38 +01:00 committed by Nicolas Grekas
parent 602b52041a
commit d29e433ae8
3 changed files with 59 additions and 0 deletions

View File

@ -501,6 +501,8 @@ abstract class AbstractNormalizer implements NormalizerInterface, DenormalizerIn
$params[] = $this->defaultContext[self::DEFAULT_CONSTRUCTOR_ARGUMENTS][$class][$key];
} elseif ($constructorParameter->isDefaultValueAvailable()) {
$params[] = $constructorParameter->getDefaultValue();
} elseif ($constructorParameter->hasType() && $constructorParameter->getType()->allowsNull()) {
$params[] = null;
} else {
throw new MissingConstructorArgumentsException(sprintf('Cannot create an instance of "%s" from serialized data because its constructor requires parameter "%s" to be present.', $class, $constructorParameter->name));
}

View File

@ -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 NullableOptionalConstructorArgumentDummy
{
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;
}
}

View File

@ -18,6 +18,7 @@ use Symfony\Component\Serializer\Serializer;
use Symfony\Component\Serializer\Tests\Fixtures\AbstractNormalizerDummy;
use Symfony\Component\Serializer\Tests\Fixtures\Dummy;
use Symfony\Component\Serializer\Tests\Fixtures\NullableConstructorArgumentDummy;
use Symfony\Component\Serializer\Tests\Fixtures\NullableOptionalConstructorArgumentDummy;
use Symfony\Component\Serializer\Tests\Fixtures\StaticConstructorDummy;
use Symfony\Component\Serializer\Tests\Fixtures\StaticConstructorNormalizer;
use Symfony\Component\Serializer\Tests\Fixtures\VariadicConstructorTypedArgsDummy;
@ -118,6 +119,22 @@ class AbstractNormalizerTest extends TestCase
}
public function testObjectWithNullableConstructorArgument()
{
$normalizer = new ObjectNormalizer();
$dummy = $normalizer->denormalize(['foo' => null], NullableOptionalConstructorArgumentDummy::class);
$this->assertNull($dummy->getFoo());
}
public function testObjectWithNullableConstructorArgumentWithoutInput()
{
$normalizer = new ObjectNormalizer();
$dummy = $normalizer->denormalize([], NullableOptionalConstructorArgumentDummy::class);
$this->assertNull($dummy->getFoo());
}
public function testObjectWithNullableNonOptionalConstructorArgument()
{
$normalizer = new ObjectNormalizer();
$dummy = $normalizer->denormalize(['foo' => null], NullableConstructorArgumentDummy::class);
@ -125,6 +142,14 @@ class AbstractNormalizerTest extends TestCase
$this->assertNull($dummy->getFoo());
}
public function testObjectWithNullableNonOptionalConstructorArgumentWithoutInput()
{
$normalizer = new ObjectNormalizer();
$dummy = $normalizer->denormalize([], NullableConstructorArgumentDummy::class);
$this->assertNull($dummy->getFoo());
}
/**
* @dataProvider getNormalizer
*/