bug #11220 [Components][Serializer] optional constructor arguments can be omitted during the denormalization process (xabbuh)
This PR was merged into the 2.3 branch.
Discussion
----------
[Components][Serializer] optional constructor arguments can be omitted during the denormalization process
| Q | A
| ------------- | ---
| Bug fix? | yes
| New feature? | no
| BC breaks? | no
| Deprecations? | no
| Tests pass? | yes
| Fixed tickets | #10499
| License | MIT
| Doc PR |
Commits
-------
5bb2345
[Components][Serializer] optional constructor arguments can be omitted during the denormalization process
This commit is contained in:
commit
803b06b2a4
@ -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 '.
|
||||
|
@ -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");
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user