Skip to content

Commit

Permalink
bug #11220 [Components][Serializer] optional constructor arguments ca…
Browse files Browse the repository at this point in the history
…n 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
  • Loading branch information
fabpot committed Jun 25, 2014
2 parents eeeae94 + 5bb2345 commit 803b06b
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
Expand Up @@ -128,7 +128,9 @@ public function denormalize($data, $class, $format = null, array $context = arra
$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 '.
Expand Down
Expand Up @@ -86,6 +86,16 @@ public function testConstructorDenormalize()
$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
*/
Expand Down Expand Up @@ -263,3 +273,37 @@ public function otherMethod()
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");
}
}

0 comments on commit 803b06b

Please sign in to comment.