[Serializer] AbstractNormalizer::instantiateObject allow default values when not optional
This commit is contained in:
parent
58efb98c6f
commit
266d53e5e8
@ -328,7 +328,7 @@ abstract class AbstractNormalizer extends SerializerAwareNormalizer implements N
|
|||||||
$params[] = $data[$key];
|
$params[] = $data[$key];
|
||||||
// don't run set for a parameter passed to the constructor
|
// don't run set for a parameter passed to the constructor
|
||||||
unset($data[$key]);
|
unset($data[$key]);
|
||||||
} elseif ($constructorParameter->isOptional()) {
|
} elseif ($constructorParameter->isDefaultValueAvailable()) {
|
||||||
$params[] = $constructorParameter->getDefaultValue();
|
$params[] = $constructorParameter->getDefaultValue();
|
||||||
} else {
|
} else {
|
||||||
throw new RuntimeException(
|
throw new RuntimeException(
|
||||||
|
@ -203,6 +203,15 @@ class GetSetMethodNormalizerTest extends \PHPUnit_Framework_TestCase
|
|||||||
$this->assertEquals(array(1, 2, 3), $obj->getBaz());
|
$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()
|
public function testConstructorWithObjectDenormalize()
|
||||||
{
|
{
|
||||||
$data = new \stdClass();
|
$data = new \stdClass();
|
||||||
@ -650,6 +659,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
|
class GetCamelizedDummy
|
||||||
{
|
{
|
||||||
private $kevinDunglas;
|
private $kevinDunglas;
|
||||||
|
@ -147,6 +147,15 @@ class ObjectNormalizerTest extends \PHPUnit_Framework_TestCase
|
|||||||
$this->assertEquals(array(1, 2, 3), $obj->getBaz());
|
$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()
|
public function testConstructorWithObjectDenormalize()
|
||||||
{
|
{
|
||||||
$data = new \stdClass();
|
$data = new \stdClass();
|
||||||
@ -556,3 +565,30 @@ class ObjectConstructorOptionalArgsDummy
|
|||||||
throw new \RuntimeException('Dummy::otherMethod() should not be called');
|
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