Skip to content

Commit 56ac1a7

Browse files
IckleChrissoyuka
authored andcommitted
Updates the abstract item normalizer to honour the ObjectNormalizer::DISABLE_TYPE_ENFORCEMENT flag during denormalization
1 parent 8f88c9e commit 56ac1a7

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

src/Serializer/AbstractItemNormalizer.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,9 @@ private function createAttributeValue($attribute, $value, $format = null, array
336336
return $this->denormalizeRelation($attribute, $propertyMetadata, $className, $value, $format, $this->createChildContext($context, $attribute));
337337
}
338338

339-
$this->validateType($attribute, $type, $value, $format);
339+
if (empty($context[static::DISABLE_TYPE_ENFORCEMENT])) {
340+
$this->validateType($attribute, $type, $value, $format);
341+
}
340342

341343
return $value;
342344
}

tests/Serializer/AbstractItemNormalizerTest.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,45 @@ public function testBadType()
626626
$normalizer->denormalize(['foo' => 42], Dummy::class);
627627
}
628628

629+
public function testTypeChecksCanBeDisabled()
630+
{
631+
$propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
632+
$propertyNameCollectionFactoryProphecy->create(Dummy::class, [])->willReturn(
633+
new PropertyNameCollection(['foo'])
634+
)->shouldBeCalled();
635+
636+
$propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
637+
$propertyMetadataFactoryProphecy->create(Dummy::class, 'foo', [])->willReturn(
638+
new PropertyMetadata(new Type(Type::BUILTIN_TYPE_FLOAT), '', false, true, false, false)
639+
)->shouldBeCalled();
640+
641+
$iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
642+
$propertyAccessorProphecy = $this->prophesize(PropertyAccessorInterface::class);
643+
$resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
644+
645+
$serializerProphecy = $this->prophesize(SerializerInterface::class);
646+
$serializerProphecy->willImplement(DenormalizerInterface::class);
647+
648+
$normalizer = $this->getMockForAbstractClass(AbstractItemNormalizer::class, [
649+
$propertyNameCollectionFactoryProphecy->reveal(),
650+
$propertyMetadataFactoryProphecy->reveal(),
651+
$iriConverterProphecy->reveal(),
652+
$resourceClassResolverProphecy->reveal(),
653+
$propertyAccessorProphecy->reveal(),
654+
null,
655+
null,
656+
null,
657+
false,
658+
[],
659+
[],
660+
null,
661+
true,
662+
]);
663+
$normalizer->setSerializer($serializerProphecy->reveal());
664+
665+
$normalizer->denormalize(['foo' => 42], Dummy::class, null, ['disable_type_enforcement' => true]);
666+
}
667+
629668
public function testJsonAllowIntAsFloat()
630669
{
631670
$propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class);

0 commit comments

Comments
 (0)