diff --git a/src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php b/src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php index 84d6db2e18..8457b5eef6 100644 --- a/src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php +++ b/src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php @@ -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)); } diff --git a/src/Symfony/Component/Serializer/Tests/Fixtures/NullableOptionalConstructorArgumentDummy.php b/src/Symfony/Component/Serializer/Tests/Fixtures/NullableOptionalConstructorArgumentDummy.php new file mode 100644 index 0000000000..45b65adbc3 --- /dev/null +++ b/src/Symfony/Component/Serializer/Tests/Fixtures/NullableOptionalConstructorArgumentDummy.php @@ -0,0 +1,32 @@ + + * + * 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; + } +} diff --git a/src/Symfony/Component/Serializer/Tests/Normalizer/AbstractNormalizerTest.php b/src/Symfony/Component/Serializer/Tests/Normalizer/AbstractNormalizerTest.php index 85aacb9743..7b3c0a0b04 100644 --- a/src/Symfony/Component/Serializer/Tests/Normalizer/AbstractNormalizerTest.php +++ b/src/Symfony/Component/Serializer/Tests/Normalizer/AbstractNormalizerTest.php @@ -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 */