From 05137a513f4c5a5e56ffbcf53847a93284b49f67 Mon Sep 17 00:00:00 2001 From: Maksim Vorozhtsov Date: Thu, 15 Feb 2024 23:22:46 +0300 Subject: [PATCH] [Serializer] Fix unknown types normalization type when know type --- Normalizer/AbstractNormalizer.php | 8 +++++++- Tests/SerializerTest.php | 6 +++--- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/Normalizer/AbstractNormalizer.php b/Normalizer/AbstractNormalizer.php index 8643aa98..2192f8ac 100644 --- a/Normalizer/AbstractNormalizer.php +++ b/Normalizer/AbstractNormalizer.php @@ -410,10 +410,16 @@ protected function instantiateObject(array &$data, string $class, array &$contex continue; } + $constructorParameterType = 'unknown'; + $reflectionType = $constructorParameter->getType(); + if ($reflectionType instanceof \ReflectionNamedType) { + $constructorParameterType = $reflectionType->getName(); + } + $exception = NotNormalizableValueException::createForUnexpectedDataType( sprintf('Failed to create object because the class misses the "%s" property.', $constructorParameter->name), $data, - ['unknown'], + [$constructorParameterType], $context['deserialization_path'], true ); diff --git a/Tests/SerializerTest.php b/Tests/SerializerTest.php index 921d3fd0..1fa29968 100644 --- a/Tests/SerializerTest.php +++ b/Tests/SerializerTest.php @@ -1308,7 +1308,7 @@ public function testCollectDenormalizationErrorsWithConstructor(?ClassMetadataFa [ 'currentType' => 'array', 'expectedTypes' => [ - 'unknown', + 'string', ], 'path' => 'string', 'useMessageForUser' => true, @@ -1317,7 +1317,7 @@ public function testCollectDenormalizationErrorsWithConstructor(?ClassMetadataFa [ 'currentType' => 'array', 'expectedTypes' => [ - 'unknown', + 'int', ], 'path' => 'int', 'useMessageForUser' => true, @@ -1548,7 +1548,7 @@ public function testPartialDenormalizationWithMissingConstructorTypes() [ 'currentType' => 'array', 'expectedTypes' => [ - 'unknown', + 'string', ], 'path' => 'two', 'useMessageForUser' => true,