bug #14711 [Serializer] AbstractNormalizer instantiateObject avoid null rejection (boekkooi)

This PR was submitted for the 2.8 branch but it was merged into the 2.7 branch instead (closes #14711).

Discussion
----------

[Serializer] AbstractNormalizer instantiateObject avoid `null` rejection

| 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 null argument.

Commits
-------

d546080 [Serializer] AbstractNormalizer::instantiateObject avoid `null` rejection
This commit is contained in:
Fabien Potencier 2015-05-25 09:37:02 +02:00
commit c563bbc2e4
4 changed files with 34 additions and 3 deletions

View File

@ -59,8 +59,8 @@ abstract class AbstractNormalizer extends SerializerAwareNormalizer implements N
/**
* Sets the {@link ClassMetadataFactoryInterface} to use.
*
* @param ClassMetadataFactoryInterface|null $classMetadataFactory
* @param NameConverterInterface|null $nameConverter
* @param ClassMetadataFactoryInterface|null $classMetadataFactory
* @param NameConverterInterface|null $nameConverter
*/
public function __construct(ClassMetadataFactoryInterface $classMetadataFactory = null, NameConverterInterface $nameConverter = null)
{
@ -324,7 +324,7 @@ abstract class AbstractNormalizer extends SerializerAwareNormalizer implements N
$allowed = $allowedAttributes === false || in_array($paramName, $allowedAttributes);
$ignored = in_array($paramName, $this->ignoredAttributes);
if ($allowed && !$ignored && isset($data[$key])) {
if ($allowed && !$ignored && array_key_exists($key, $data)) {
$params[] = $data[$key];
// don't run set for a parameter passed to the constructor
unset($data[$key]);

View File

@ -193,6 +193,16 @@ class GetSetMethodNormalizerTest extends \PHPUnit_Framework_TestCase
$this->assertTrue($obj->isBaz());
}
public function testConstructorDenormalizeWithNullArgument()
{
$obj = $this->normalizer->denormalize(
array('foo' => 'foo', 'bar' => null, 'baz' => true),
__NAMESPACE__.'\GetConstructorDummy', 'any');
$this->assertEquals('foo', $obj->getFoo());
$this->assertNull($obj->getBar());
$this->assertTrue($obj->isBaz());
}
public function testConstructorDenormalizeWithMissingOptionalArgument()
{
$obj = $this->normalizer->denormalize(

View File

@ -137,6 +137,16 @@ class ObjectNormalizerTest extends \PHPUnit_Framework_TestCase
$this->assertTrue($obj->isBaz());
}
public function testConstructorDenormalizeWithNullArgument()
{
$obj = $this->normalizer->denormalize(
array('foo' => 'foo', 'bar' => null, 'baz' => true),
__NAMESPACE__.'\ObjectConstructorDummy', 'any');
$this->assertEquals('foo', $obj->getFoo());
$this->assertNull($obj->bar);
$this->assertTrue($obj->isBaz());
}
public function testConstructorDenormalizeWithMissingOptionalArgument()
{
$obj = $this->normalizer->denormalize(

View File

@ -151,6 +151,17 @@ class PropertyNormalizerTest extends \PHPUnit_Framework_TestCase
$this->assertEquals('bar', $obj->getBar());
}
public function testConstructorDenormalizeWithNullArgument()
{
$obj = $this->normalizer->denormalize(
array('foo' => null, 'bar' => 'bar'),
__NAMESPACE__.'\PropertyConstructorDummy', '
any'
);
$this->assertNull($obj->getFoo());
$this->assertEquals('bar', $obj->getBar());
}
/**
* @dataProvider provideCallbacks
*/