From ca314889e7fb7a8b5c1f58ca489b9a22b7e71293 Mon Sep 17 00:00:00 2001 From: Maxime Steinhausser Date: Sat, 26 May 2018 11:34:32 +0200 Subject: [PATCH] [Serializer] Fix serializer tries to denormalize null values on nullable properties --- .../Normalizer/AbstractNormalizer.php | 6 ++++ .../NullableConstructorArgumentDummy.php | 32 +++++++++++++++++++ .../Normalizer/AbstractNormalizerTest.php | 12 +++++++ 3 files changed, 50 insertions(+) create mode 100644 src/Symfony/Component/Serializer/Tests/Fixtures/NullableConstructorArgumentDummy.php diff --git a/src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php b/src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php index 74a133214f55b..cad6205dfb826 100644 --- a/src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php +++ b/src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php @@ -349,6 +349,12 @@ protected function instantiateObject(array &$data, $class, array &$context, \Ref } } elseif ($allowed && !$ignored && (isset($data[$key]) || array_key_exists($key, $data))) { $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 { if (null !== $constructorParameter->getClass()) { if (!$this->serializer instanceof DenormalizerInterface) { diff --git a/src/Symfony/Component/Serializer/Tests/Fixtures/NullableConstructorArgumentDummy.php b/src/Symfony/Component/Serializer/Tests/Fixtures/NullableConstructorArgumentDummy.php new file mode 100644 index 0000000000000..616fab4ae8df0 --- /dev/null +++ b/src/Symfony/Component/Serializer/Tests/Fixtures/NullableConstructorArgumentDummy.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 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; + } +} diff --git a/src/Symfony/Component/Serializer/Tests/Normalizer/AbstractNormalizerTest.php b/src/Symfony/Component/Serializer/Tests/Normalizer/AbstractNormalizerTest.php index e07fb56bf5b7f..28edc05872710 100644 --- a/src/Symfony/Component/Serializer/Tests/Normalizer/AbstractNormalizerTest.php +++ b/src/Symfony/Component/Serializer/Tests/Normalizer/AbstractNormalizerTest.php @@ -9,6 +9,7 @@ use Symfony\Component\Serializer\Normalizer\AbstractNormalizer; use Symfony\Component\Serializer\Normalizer\ObjectNormalizer; 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\StaticConstructorDummy; use Symfony\Component\Serializer\Tests\Fixtures\StaticConstructorNormalizer; @@ -116,4 +117,15 @@ public function testObjectWithStaticConstructor() $this->assertEquals('baz', $dummy->quz); $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()); + } } 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