Skip to content

Commit df91d86

Browse files
boekkooifabpot
authored andcommitted
[Serializer] AbstractNormalizer::instantiateObject avoid null rejection
1 parent 1f29e1a commit df91d86

File tree

4 files changed

+34
-3
lines changed

4 files changed

+34
-3
lines changed

Normalizer/AbstractNormalizer.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ abstract class AbstractNormalizer extends SerializerAwareNormalizer implements N
5959
/**
6060
* Sets the {@link ClassMetadataFactoryInterface} to use.
6161
*
62-
* @param ClassMetadataFactoryInterface|null $classMetadataFactory
63-
* @param NameConverterInterface|null $nameConverter
62+
* @param ClassMetadataFactoryInterface|null $classMetadataFactory
63+
* @param NameConverterInterface|null $nameConverter
6464
*/
6565
public function __construct(ClassMetadataFactoryInterface $classMetadataFactory = null, NameConverterInterface $nameConverter = null)
6666
{
@@ -324,7 +324,7 @@ protected function instantiateObject(array $data, $class, array &$context, \Refl
324324

325325
$allowed = $allowedAttributes === false || in_array($paramName, $allowedAttributes);
326326
$ignored = in_array($paramName, $this->ignoredAttributes);
327-
if ($allowed && !$ignored && isset($data[$key])) {
327+
if ($allowed && !$ignored && array_key_exists($key, $data)) {
328328
$params[] = $data[$key];
329329
// don't run set for a parameter passed to the constructor
330330
unset($data[$key]);

Tests/Normalizer/GetSetMethodNormalizerTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,16 @@ public function testConstructorDenormalize()
193193
$this->assertTrue($obj->isBaz());
194194
}
195195

196+
public function testConstructorDenormalizeWithNullArgument()
197+
{
198+
$obj = $this->normalizer->denormalize(
199+
array('foo' => 'foo', 'bar' => null, 'baz' => true),
200+
__NAMESPACE__.'\GetConstructorDummy', 'any');
201+
$this->assertEquals('foo', $obj->getFoo());
202+
$this->assertNull($obj->getBar());
203+
$this->assertTrue($obj->isBaz());
204+
}
205+
196206
public function testConstructorDenormalizeWithMissingOptionalArgument()
197207
{
198208
$obj = $this->normalizer->denormalize(

Tests/Normalizer/ObjectNormalizerTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,16 @@ public function testConstructorDenormalize()
137137
$this->assertTrue($obj->isBaz());
138138
}
139139

140+
public function testConstructorDenormalizeWithNullArgument()
141+
{
142+
$obj = $this->normalizer->denormalize(
143+
array('foo' => 'foo', 'bar' => null, 'baz' => true),
144+
__NAMESPACE__.'\ObjectConstructorDummy', 'any');
145+
$this->assertEquals('foo', $obj->getFoo());
146+
$this->assertNull($obj->bar);
147+
$this->assertTrue($obj->isBaz());
148+
}
149+
140150
public function testConstructorDenormalizeWithMissingOptionalArgument()
141151
{
142152
$obj = $this->normalizer->denormalize(

Tests/Normalizer/PropertyNormalizerTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,17 @@ public function testConstructorDenormalize()
151151
$this->assertEquals('bar', $obj->getBar());
152152
}
153153

154+
public function testConstructorDenormalizeWithNullArgument()
155+
{
156+
$obj = $this->normalizer->denormalize(
157+
array('foo' => null, 'bar' => 'bar'),
158+
__NAMESPACE__.'\PropertyConstructorDummy', '
159+
any'
160+
);
161+
$this->assertNull($obj->getFoo());
162+
$this->assertEquals('bar', $obj->getBar());
163+
}
164+
154165
/**
155166
* @dataProvider provideCallbacks
156167
*/

0 commit comments

Comments
 (0)