Skip to content

Commit ea5bc7c

Browse files
authored
Merge pull request #1957 from IckleChris/fix/ignoring-type-checks
Fixes ignoring type checks during denormalization
2 parents 8f88c9e + d99c3a8 commit ea5bc7c

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

src/Serializer/AbstractItemNormalizer.php

+4
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,10 @@ private function createAttributeValue($attribute, $value, $format = null, array
336336
return $this->denormalizeRelation($attribute, $propertyMetadata, $className, $value, $format, $this->createChildContext($context, $attribute));
337337
}
338338

339+
if ($context[static::DISABLE_TYPE_ENFORCEMENT] ?? false) {
340+
return $value;
341+
}
342+
339343
$this->validateType($attribute, $type, $value, $format);
340344

341345
return $value;

tests/Serializer/AbstractItemNormalizerTest.php

+39
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)