From 45a61da998fa345f5b62762863ab85609ba1f020 Mon Sep 17 00:00:00 2001 From: Jakub Kisielewski Date: Sun, 11 Feb 2018 15:26:58 +0100 Subject: [PATCH 1/4] deserialize as a null when inner object cannot be created and type hint allows null --- .../Normalizer/AbstractNormalizer.php | 5 +++ .../Tests/Normalizer/ObjectNormalizerTest.php | 39 +++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php b/src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php index 6442e98a1473f..41207756a2f94 100644 --- a/src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php +++ b/src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php @@ -351,6 +351,11 @@ protected function instantiateObject(array &$data, $class, array &$context, \Ref } } catch (\ReflectionException $e) { throw new RuntimeException(sprintf('Could not determine the class of the parameter "%s".', $key), 0, $e); + } catch(RuntimeException $e) { + if(!$constructorParameter->getType()->allowsNull()) + throw $e; + + $parameterData = null; } // Don't run set for a parameter passed to the constructor diff --git a/src/Symfony/Component/Serializer/Tests/Normalizer/ObjectNormalizerTest.php b/src/Symfony/Component/Serializer/Tests/Normalizer/ObjectNormalizerTest.php index 68f062cd5dcc6..8afccca92909d 100644 --- a/src/Symfony/Component/Serializer/Tests/Normalizer/ObjectNormalizerTest.php +++ b/src/Symfony/Component/Serializer/Tests/Normalizer/ObjectNormalizerTest.php @@ -182,6 +182,23 @@ public function testConstructorWithObjectTypeHintDenormalize() $this->assertEquals('rab', $obj->getInner()->bar); } + public function testConstructorWithUnconstructableNullableObjectTypeHintDenormalize() + { + $data = array( + 'id' => 10, + 'inner' => null + ); + + $normalizer = new ObjectNormalizer(); + $serializer = new Serializer(array($normalizer)); + $normalizer->setSerializer($serializer); + + $obj = $normalizer->denormalize($data, DummyWithNullableConstructorObject::class); + $this->assertInstanceOf(DummyWithNullableConstructorObject::class, $obj); + $this->assertEquals(10, $obj->getId()); + $this->assertNull($obj->getInner()); + } + /** * @expectedException \Symfony\Component\Serializer\Exception\RuntimeException * @expectedExceptionMessage Could not determine the class of the parameter "unknown". @@ -1109,3 +1126,25 @@ public function getFoo() return $this->Foo; } } + +class DummyWithNullableConstructorObject +{ + private $id; + private $inner; + + public function __construct($id, ?ObjectConstructorDummy $inner) + { + $this->id = $id; + $this->inner = $inner; + } + + public function getId() + { + return $this->id; + } + + public function getInner() + { + return $this->inner; + } +} \ No newline at end of file From af11454ea1110ae923655a65b7a1833a8f072057 Mon Sep 17 00:00:00 2001 From: Jakub Kisielewski Date: Sun, 11 Feb 2018 15:34:40 +0100 Subject: [PATCH 2/4] catch MissingConstructorArgumentsException instead of RuntimeException --- .../Component/Serializer/Normalizer/AbstractNormalizer.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php b/src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php index 41207756a2f94..18cf1f2ef9f2b 100644 --- a/src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php +++ b/src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php @@ -351,7 +351,7 @@ protected function instantiateObject(array &$data, $class, array &$context, \Ref } } catch (\ReflectionException $e) { throw new RuntimeException(sprintf('Could not determine the class of the parameter "%s".', $key), 0, $e); - } catch(RuntimeException $e) { + } catch(MissingConstructorArgumentsException $e) { if(!$constructorParameter->getType()->allowsNull()) throw $e; From 9ba64165454acbd0b644331b262e731efbf8fc14 Mon Sep 17 00:00:00 2001 From: Jakub Kisielewski Date: Sun, 11 Feb 2018 15:45:34 +0100 Subject: [PATCH 3/4] fix coding standards --- .../Component/Serializer/Normalizer/AbstractNormalizer.php | 6 +++--- .../Serializer/Tests/Normalizer/ObjectNormalizerTest.php | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php b/src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php index 18cf1f2ef9f2b..8fe19b902b35b 100644 --- a/src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php +++ b/src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php @@ -351,10 +351,10 @@ protected function instantiateObject(array &$data, $class, array &$context, \Ref } } catch (\ReflectionException $e) { throw new RuntimeException(sprintf('Could not determine the class of the parameter "%s".', $key), 0, $e); - } catch(MissingConstructorArgumentsException $e) { - if(!$constructorParameter->getType()->allowsNull()) + } catch (MissingConstructorArgumentsException $e) { + if(!$constructorParameter->getType()->allowsNull()) { throw $e; - + } $parameterData = null; } diff --git a/src/Symfony/Component/Serializer/Tests/Normalizer/ObjectNormalizerTest.php b/src/Symfony/Component/Serializer/Tests/Normalizer/ObjectNormalizerTest.php index 8afccca92909d..441fe55eb0c4f 100644 --- a/src/Symfony/Component/Serializer/Tests/Normalizer/ObjectNormalizerTest.php +++ b/src/Symfony/Component/Serializer/Tests/Normalizer/ObjectNormalizerTest.php @@ -1147,4 +1147,4 @@ public function getInner() { return $this->inner; } -} \ No newline at end of file +} From a16ed6f9fdae4df45e8900d2acf2678a2446d18c Mon Sep 17 00:00:00 2001 From: Jakub Kisielewski Date: Sun, 11 Feb 2018 15:46:54 +0100 Subject: [PATCH 4/4] fix coding standards --- .../Component/Serializer/Normalizer/AbstractNormalizer.php | 2 +- .../Serializer/Tests/Normalizer/ObjectNormalizerTest.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php b/src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php index 8fe19b902b35b..aa72e9d49e129 100644 --- a/src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php +++ b/src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php @@ -352,7 +352,7 @@ protected function instantiateObject(array &$data, $class, array &$context, \Ref } catch (\ReflectionException $e) { throw new RuntimeException(sprintf('Could not determine the class of the parameter "%s".', $key), 0, $e); } catch (MissingConstructorArgumentsException $e) { - if(!$constructorParameter->getType()->allowsNull()) { + if (!$constructorParameter->getType()->allowsNull()) { throw $e; } $parameterData = null; diff --git a/src/Symfony/Component/Serializer/Tests/Normalizer/ObjectNormalizerTest.php b/src/Symfony/Component/Serializer/Tests/Normalizer/ObjectNormalizerTest.php index 441fe55eb0c4f..333f48779dc23 100644 --- a/src/Symfony/Component/Serializer/Tests/Normalizer/ObjectNormalizerTest.php +++ b/src/Symfony/Component/Serializer/Tests/Normalizer/ObjectNormalizerTest.php @@ -186,7 +186,7 @@ public function testConstructorWithUnconstructableNullableObjectTypeHintDenormal { $data = array( 'id' => 10, - 'inner' => null + 'inner' => null, ); $normalizer = new ObjectNormalizer();