diff --git a/src/Serializer/AbstractItemNormalizer.php b/src/Serializer/AbstractItemNormalizer.php index 68257a77db4..ea8f0229799 100644 --- a/src/Serializer/AbstractItemNormalizer.php +++ b/src/Serializer/AbstractItemNormalizer.php @@ -336,6 +336,10 @@ private function createAttributeValue($attribute, $value, $format = null, array return $this->denormalizeRelation($attribute, $propertyMetadata, $className, $value, $format, $this->createChildContext($context, $attribute)); } + if ($context[static::DISABLE_TYPE_ENFORCEMENT] ?? false) { + return $value; + } + $this->validateType($attribute, $type, $value, $format); return $value; diff --git a/tests/Serializer/AbstractItemNormalizerTest.php b/tests/Serializer/AbstractItemNormalizerTest.php index b73a01b6788..7895edcbcdb 100644 --- a/tests/Serializer/AbstractItemNormalizerTest.php +++ b/tests/Serializer/AbstractItemNormalizerTest.php @@ -626,6 +626,45 @@ public function testBadType() $normalizer->denormalize(['foo' => 42], Dummy::class); } + public function testTypeChecksCanBeDisabled() + { + $propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class); + $propertyNameCollectionFactoryProphecy->create(Dummy::class, [])->willReturn( + new PropertyNameCollection(['foo']) + )->shouldBeCalled(); + + $propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class); + $propertyMetadataFactoryProphecy->create(Dummy::class, 'foo', [])->willReturn( + new PropertyMetadata(new Type(Type::BUILTIN_TYPE_FLOAT), '', 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(), + null, + null, + null, + false, + [], + [], + null, + true, + ]); + $normalizer->setSerializer($serializerProphecy->reveal()); + + $normalizer->denormalize(['foo' => 42], Dummy::class, null, ['disable_type_enforcement' => true]); + } + public function testJsonAllowIntAsFloat() { $propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class);