bug #14745 [Serializer] AbstractNormalizer::instantiateObject allow default values when not optional (boekkooi)
This PR was merged into the 2.7 branch.
Discussion
----------
[Serializer] AbstractNormalizer::instantiateObject allow default values when not optional
| Q | A
| ------------- | ---
| Bug fix? | yes
| New feature? | no
| BC breaks? | no
| Deprecations? | no
| Tests pass? | yes
| Fixed tickets | -
| License | MIT
| Doc PR | -
This PR fixes a bug in the AbstractNormalizer when denormalizing a array with a missing value but a default value set in the class constructor.
Commits
-------
266d53e
[Serializer] AbstractNormalizer::instantiateObject allow default values when not optional
This commit is contained in:
commit
2aeaa22e31
@ -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(
|
||||
|
@ -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;
|
||||
|
@ -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');
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user