diff --git a/src/Symfony/Component/Serializer/Normalizer/GetSetMethodNormalizer.php b/src/Symfony/Component/Serializer/Normalizer/GetSetMethodNormalizer.php index 3d7dad3975..e4e3d82291 100644 --- a/src/Symfony/Component/Serializer/Normalizer/GetSetMethodNormalizer.php +++ b/src/Symfony/Component/Serializer/Normalizer/GetSetMethodNormalizer.php @@ -128,7 +128,9 @@ class GetSetMethodNormalizer extends SerializerAwareNormalizer implements Normal $params[] = $data[$paramName]; // don't run set for a parameter passed to the constructor unset($data[$paramName]); - } elseif (!$constructorParameter->isOptional()) { + } elseif ($constructorParameter->isOptional()) { + $params[] = $constructorParameter->getDefaultValue(); + } else { throw new RuntimeException( 'Cannot create an instance of '.$class. ' from serialized data because its constructor requires '. diff --git a/src/Symfony/Component/Serializer/Tests/Normalizer/GetSetMethodNormalizerTest.php b/src/Symfony/Component/Serializer/Tests/Normalizer/GetSetMethodNormalizerTest.php index 7ec4814d71..e4f1b5c656 100644 --- a/src/Symfony/Component/Serializer/Tests/Normalizer/GetSetMethodNormalizerTest.php +++ b/src/Symfony/Component/Serializer/Tests/Normalizer/GetSetMethodNormalizerTest.php @@ -86,6 +86,16 @@ class GetSetMethodNormalizerTest extends \PHPUnit_Framework_TestCase $this->assertEquals('bar', $obj->getBar()); } + public function testConstructorDenormalizeWithMissingOptionalArgument() + { + $obj = $this->normalizer->denormalize( + array('foo' => 'test', 'baz' => array(1, 2, 3)), + __NAMESPACE__.'\GetConstructorOptionalArgsDummy', 'any'); + $this->assertEquals('test', $obj->getFoo()); + $this->assertEquals(array(), $obj->getBar()); + $this->assertEquals(array(1, 2, 3), $obj->getBaz()); + } + /** * @dataProvider provideCallbacks */ @@ -263,3 +273,37 @@ class GetConstructorDummy throw new \RuntimeException("Dummy::otherMethod() should not be called"); } } + +class GetConstructorOptionalArgsDummy +{ + protected $foo; + private $bar; + private $baz; + + public function __construct($foo, $bar = array(), $baz = array()) + { + $this->foo = $foo; + $this->bar = $bar; + $this->baz = $baz; + } + + public function getFoo() + { + return $this->foo; + } + + public function getBar() + { + return $this->bar; + } + + public function getBaz() + { + return $this->baz; + } + + public function otherMethod() + { + throw new \RuntimeException("Dummy::otherMethod() should not be called"); + } +}