diff --git a/src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php b/src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php index 84d6db2e18ceb..8457b5eef6c3a 100644 --- a/src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php +++ b/src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php @@ -501,6 +501,8 @@ protected function instantiateObject(array &$data, $class, array &$context, \Ref $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 0000000000000..45b65adbc3199 --- /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 85aacb97431a6..7b3c0a0b04ddd 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\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 @@ public function testObjectWithStaticConstructor() } 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 @@ public function testObjectWithNullableConstructorArgument() $this->assertNull($dummy->getFoo()); } + public function testObjectWithNullableNonOptionalConstructorArgumentWithoutInput() + { + $normalizer = new ObjectNormalizer(); + $dummy = $normalizer->denormalize([], NullableConstructorArgumentDummy::class); + + $this->assertNull($dummy->getFoo()); + } + /** * @dataProvider getNormalizer */ pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy