[Serializer] AbstractNormalizer force null for non-optional nullable constructor parameter denormalization when not present in input
This commit is contained in:
parent
602b52041a
commit
d29e433ae8
@ -501,6 +501,8 @@ abstract class AbstractNormalizer implements NormalizerInterface, DenormalizerIn
|
|||||||
$params[] = $this->defaultContext[self::DEFAULT_CONSTRUCTOR_ARGUMENTS][$class][$key];
|
$params[] = $this->defaultContext[self::DEFAULT_CONSTRUCTOR_ARGUMENTS][$class][$key];
|
||||||
} elseif ($constructorParameter->isDefaultValueAvailable()) {
|
} elseif ($constructorParameter->isDefaultValueAvailable()) {
|
||||||
$params[] = $constructorParameter->getDefaultValue();
|
$params[] = $constructorParameter->getDefaultValue();
|
||||||
|
} elseif ($constructorParameter->hasType() && $constructorParameter->getType()->allowsNull()) {
|
||||||
|
$params[] = null;
|
||||||
} else {
|
} 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));
|
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));
|
||||||
}
|
}
|
||||||
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
@ -18,6 +18,7 @@ use Symfony\Component\Serializer\Serializer;
|
|||||||
use Symfony\Component\Serializer\Tests\Fixtures\AbstractNormalizerDummy;
|
use Symfony\Component\Serializer\Tests\Fixtures\AbstractNormalizerDummy;
|
||||||
use Symfony\Component\Serializer\Tests\Fixtures\Dummy;
|
use Symfony\Component\Serializer\Tests\Fixtures\Dummy;
|
||||||
use Symfony\Component\Serializer\Tests\Fixtures\NullableConstructorArgumentDummy;
|
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\StaticConstructorDummy;
|
||||||
use Symfony\Component\Serializer\Tests\Fixtures\StaticConstructorNormalizer;
|
use Symfony\Component\Serializer\Tests\Fixtures\StaticConstructorNormalizer;
|
||||||
use Symfony\Component\Serializer\Tests\Fixtures\VariadicConstructorTypedArgsDummy;
|
use Symfony\Component\Serializer\Tests\Fixtures\VariadicConstructorTypedArgsDummy;
|
||||||
@ -118,6 +119,22 @@ class AbstractNormalizerTest extends TestCase
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function testObjectWithNullableConstructorArgument()
|
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();
|
$normalizer = new ObjectNormalizer();
|
||||||
$dummy = $normalizer->denormalize(['foo' => null], NullableConstructorArgumentDummy::class);
|
$dummy = $normalizer->denormalize(['foo' => null], NullableConstructorArgumentDummy::class);
|
||||||
@ -125,6 +142,14 @@ class AbstractNormalizerTest extends TestCase
|
|||||||
$this->assertNull($dummy->getFoo());
|
$this->assertNull($dummy->getFoo());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testObjectWithNullableNonOptionalConstructorArgumentWithoutInput()
|
||||||
|
{
|
||||||
|
$normalizer = new ObjectNormalizer();
|
||||||
|
$dummy = $normalizer->denormalize([], NullableConstructorArgumentDummy::class);
|
||||||
|
|
||||||
|
$this->assertNull($dummy->getFoo());
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @dataProvider getNormalizer
|
* @dataProvider getNormalizer
|
||||||
*/
|
*/
|
||||||
|
Reference in New Issue
Block a user