From 266d53e5e806884ba200a3ec964adb2b24eb11af Mon Sep 17 00:00:00 2001 From: Warnar Boekkooi Date: Mon, 25 May 2015 07:36:47 +0800 Subject: [PATCH] [Serializer] AbstractNormalizer::instantiateObject allow default values when not optional --- .../Normalizer/AbstractNormalizer.php | 2 +- .../Normalizer/GetSetMethodNormalizerTest.php | 36 +++++++++++++++++++ .../Tests/Normalizer/ObjectNormalizerTest.php | 36 +++++++++++++++++++ 3 files changed, 73 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php b/src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php index dfd21b4644..c0c622efe9 100644 --- a/src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php +++ b/src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php @@ -328,7 +328,7 @@ abstract class AbstractNormalizer extends SerializerAwareNormalizer implements N $params[] = $data[$key]; // don't run set for a parameter passed to the constructor unset($data[$key]); - } elseif ($constructorParameter->isOptional()) { + } elseif ($constructorParameter->isDefaultValueAvailable()) { $params[] = $constructorParameter->getDefaultValue(); } else { throw new RuntimeException( diff --git a/src/Symfony/Component/Serializer/Tests/Normalizer/GetSetMethodNormalizerTest.php b/src/Symfony/Component/Serializer/Tests/Normalizer/GetSetMethodNormalizerTest.php index b4c82391d7..e9e6a56a0a 100644 --- a/src/Symfony/Component/Serializer/Tests/Normalizer/GetSetMethodNormalizerTest.php +++ b/src/Symfony/Component/Serializer/Tests/Normalizer/GetSetMethodNormalizerTest.php @@ -203,6 +203,15 @@ class GetSetMethodNormalizerTest extends \PHPUnit_Framework_TestCase $this->assertEquals(array(1, 2, 3), $obj->getBaz()); } + public function testConstructorDenormalizeWithOptionalDefaultArgument() + { + $obj = $this->normalizer->denormalize( + array('bar' => 'test'), + __NAMESPACE__.'\GetConstructorArgsWithDefaultValueDummy', 'any'); + $this->assertEquals(array(), $obj->getFoo()); + $this->assertEquals('test', $obj->getBar()); + } + public function testConstructorWithObjectDenormalize() { $data = new \stdClass(); @@ -650,6 +659,33 @@ class GetConstructorOptionalArgsDummy } } +class GetConstructorArgsWithDefaultValueDummy +{ + protected $foo; + protected $bar; + + public function __construct($foo = array(), $bar) + { + $this->foo = $foo; + $this->bar = $bar; + } + + public function getFoo() + { + return $this->foo; + } + + public function getBar() + { + return $this->bar; + } + + public function otherMethod() + { + throw new \RuntimeException('Dummy::otherMethod() should not be called'); + } +} + class GetCamelizedDummy { private $kevinDunglas; diff --git a/src/Symfony/Component/Serializer/Tests/Normalizer/ObjectNormalizerTest.php b/src/Symfony/Component/Serializer/Tests/Normalizer/ObjectNormalizerTest.php index f07e79907f..6e0cffed94 100644 --- a/src/Symfony/Component/Serializer/Tests/Normalizer/ObjectNormalizerTest.php +++ b/src/Symfony/Component/Serializer/Tests/Normalizer/ObjectNormalizerTest.php @@ -147,6 +147,15 @@ class ObjectNormalizerTest extends \PHPUnit_Framework_TestCase $this->assertEquals(array(1, 2, 3), $obj->getBaz()); } + public function testConstructorDenormalizeWithOptionalDefaultArgument() + { + $obj = $this->normalizer->denormalize( + array('bar' => 'test'), + __NAMESPACE__.'\ObjectConstructorArgsWithDefaultValueDummy', 'any'); + $this->assertEquals(array(), $obj->getFoo()); + $this->assertEquals('test', $obj->getBar()); + } + public function testConstructorWithObjectDenormalize() { $data = new \stdClass(); @@ -556,3 +565,30 @@ class ObjectConstructorOptionalArgsDummy throw new \RuntimeException('Dummy::otherMethod() should not be called'); } } + +class ObjectConstructorArgsWithDefaultValueDummy +{ + protected $foo; + protected $bar; + + public function __construct($foo = array(), $bar) + { + $this->foo = $foo; + $this->bar = $bar; + } + + public function getFoo() + { + return $this->foo; + } + + public function getBar() + { + return $this->bar; + } + + public function otherMethod() + { + throw new \RuntimeException('Dummy::otherMethod() should not be called'); + } +}