Skip to content

Commit

Permalink
Fix nullable property on denormalization
Browse files Browse the repository at this point in the history
  • Loading branch information
abluchet committed Aug 25, 2016
1 parent 3c49700 commit f39ae90
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/Serializer/AbstractItemNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,12 @@ protected function setAttributeValue($object, $attribute, $value, $format = null
return;
}

if ($type->isNullable() && null === $value) {
$this->setValue($object, $attribute, $value);

return;
}

$builtinType = $type->getBuiltinType();
if (!call_user_func('is_'.$builtinType, $value)) {
throw new InvalidArgumentException(sprintf(
Expand Down
31 changes: 31 additions & 0 deletions tests/Serializer/AbstractItemNormalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -521,4 +521,35 @@ public function testDenormalizeBadKeyType()
'relatedDummies' => ['a' => ['bar' => 'baz']],
], Dummy::class);
}

public function testNullable()
{
$propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
$propertyNameCollectionFactoryProphecy->create(Dummy::class, [])->willReturn(
new PropertyNameCollection(['name'])
)->shouldBeCalled();

$propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
$propertyMetadataFactoryProphecy->create(Dummy::class, 'name', [])->willReturn(
new PropertyMetadata(new Type(Type::BUILTIN_TYPE_STRING, true), '', false, true, false, false)
)->shouldBeCalled();

$iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
$propertyAccessorProphecy = $this->prophesize(PropertyAccessorInterface::class);
$resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);

$serializerProphecy = $this->prophesize(SerializerInterface::class);
$serializerProphecy->willImplement(DenormalizerInterface::class);

$normalizer = $this->getMockForAbstractClass(AbstractItemNormalizer::class, [
$propertyNameCollectionFactoryProphecy->reveal(),
$propertyMetadataFactoryProphecy->reveal(),
$iriConverterProphecy->reveal(),
$resourceClassResolverProphecy->reveal(),
$propertyAccessorProphecy->reveal(),
]);
$normalizer->setSerializer($serializerProphecy->reveal());

$normalizer->denormalize(['name' => null], Dummy::class);
}
}

0 comments on commit f39ae90

Please sign in to comment.