diff --git a/src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php b/src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php index 5953ca86a2..8e89dd5096 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 23cb497ead..e8156dd10f 100644 --- a/src/Symfony/Component/Serializer/Tests/Normalizer/GetSetMethodNormalizerTest.php +++ b/src/Symfony/Component/Serializer/Tests/Normalizer/GetSetMethodNormalizerTest.php @@ -213,6 +213,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(); @@ -660,6 +669,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 6a41930eb0..14a268f5bc 100644 --- a/src/Symfony/Component/Serializer/Tests/Normalizer/ObjectNormalizerTest.php +++ b/src/Symfony/Component/Serializer/Tests/Normalizer/ObjectNormalizerTest.php @@ -157,6 +157,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(); @@ -566,3 +575,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'); + } +}