From d498de7fc2b7de1fda56675d719de4756694e733 Mon Sep 17 00:00:00 2001 From: HypeMC Date: Sun, 17 Dec 2023 21:48:18 +0100 Subject: [PATCH] [Serializer] Fix using deserialization path --- .../Normalizer/AbstractNormalizer.php | 2 +- .../Normalizer/AbstractObjectNormalizer.php | 2 +- .../Serializer/Tests/SerializerTest.php | 81 +++++++++++++++++-- .../Component/Serializer/composer.json | 2 +- 4 files changed, 78 insertions(+), 9 deletions(-) diff --git a/src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php b/src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php index 0d0181ae84da9..1a7c314b84f48 100644 --- a/src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php +++ b/src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php @@ -414,7 +414,7 @@ protected function instantiateObject(array &$data, string $class, array &$contex sprintf('Failed to create object because the class misses the "%s" property.', $constructorParameter->name), $data, ['unknown'], - $objectDeserializationPath, + $context['deserialization_path'], true ); $context['not_normalizable_value_exceptions'][] = $exception; diff --git a/src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php b/src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php index cd24de5840f5b..a51b69503eb71 100644 --- a/src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php +++ b/src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php @@ -437,7 +437,7 @@ public function denormalize($data, string $type, string $format = null, array $c sprintf('Failed to denormalize attribute "%s" value for class "%s": '.$e->getMessage(), $attribute, $type), $data, ['unknown'], - $context['deserialization_path'] ?? null, + $attributeContext['deserialization_path'] ?? null, false, $e->getCode(), $e diff --git a/src/Symfony/Component/Serializer/Tests/SerializerTest.php b/src/Symfony/Component/Serializer/Tests/SerializerTest.php index 447e0f882a8c5..921d3fd010ef8 100644 --- a/src/Symfony/Component/Serializer/Tests/SerializerTest.php +++ b/src/Symfony/Component/Serializer/Tests/SerializerTest.php @@ -1049,7 +1049,7 @@ public function testCollectDenormalizationErrors(?ClassMetadataFactory $classMet 'expectedTypes' => [ 'unknown', ], - 'path' => 'php74FullWithConstructor', + 'path' => 'php74FullWithConstructor.constructorArgument', 'useMessageForUser' => true, 'message' => 'Failed to create object because the class misses the "constructorArgument" property.', ], @@ -1186,6 +1186,75 @@ public function testCollectDenormalizationErrors2(?ClassMetadataFactory $classMe $this->assertSame($expected, $exceptionsAsArray); } + /** + * @requires PHP 7.4 + */ + public function testCollectDenormalizationErrorsWithoutTypeExtractor() + { + $json = ' + { + "string": [], + "int": [], + "float": [] + }'; + + $serializer = new Serializer([new ObjectNormalizer()], ['json' => new JsonEncoder()]); + + try { + $serializer->deserialize($json, Php74Full::class, 'json', [ + DenormalizerInterface::COLLECT_DENORMALIZATION_ERRORS => true, + ]); + + $this->fail(); + } catch (\Throwable $th) { + $this->assertInstanceOf(PartialDenormalizationException::class, $th); + } + + $this->assertInstanceOf(Php74Full::class, $th->getData()); + + $exceptionsAsArray = array_map(function (NotNormalizableValueException $e): array { + return [ + 'currentType' => $e->getCurrentType(), + 'expectedTypes' => $e->getExpectedTypes(), + 'path' => $e->getPath(), + 'useMessageForUser' => $e->canUseMessageForUser(), + 'message' => $e->getMessage(), + ]; + }, $th->getErrors()); + + $expected = [ + [ + 'currentType' => 'array', + 'expectedTypes' => [ + 'unknown', + ], + 'path' => 'string', + 'useMessageForUser' => false, + 'message' => 'Failed to denormalize attribute "string" value for class "Symfony\\Component\\Serializer\\Tests\\Fixtures\\Php74Full": Expected argument of type "string", "array" given at property path "string".', + ], + [ + 'currentType' => 'array', + 'expectedTypes' => [ + 'unknown', + ], + 'path' => 'int', + 'useMessageForUser' => false, + 'message' => 'Failed to denormalize attribute "int" value for class "Symfony\\Component\\Serializer\\Tests\\Fixtures\\Php74Full": Expected argument of type "int", "array" given at property path "int".', + ], + [ + 'currentType' => 'array', + 'expectedTypes' => [ + 'unknown', + ], + 'path' => 'float', + 'useMessageForUser' => false, + 'message' => 'Failed to denormalize attribute "float" value for class "Symfony\\Component\\Serializer\\Tests\\Fixtures\\Php74Full": Expected argument of type "float", "array" given at property path "float".', + ], + ]; + + $this->assertSame($expected, $exceptionsAsArray); + } + /** * @dataProvider provideCollectDenormalizationErrors * @@ -1241,7 +1310,7 @@ public function testCollectDenormalizationErrorsWithConstructor(?ClassMetadataFa 'expectedTypes' => [ 'unknown', ], - 'path' => null, + 'path' => 'string', 'useMessageForUser' => true, 'message' => 'Failed to create object because the class misses the "string" property.', ], @@ -1250,7 +1319,7 @@ public function testCollectDenormalizationErrorsWithConstructor(?ClassMetadataFa 'expectedTypes' => [ 'unknown', ], - 'path' => null, + 'path' => 'int', 'useMessageForUser' => true, 'message' => 'Failed to create object because the class misses the "int" property.', ], @@ -1300,7 +1369,7 @@ public function testCollectDenormalizationErrorsWithInvalidConstructorTypes() [ 'currentType' => 'string', 'expectedTypes' => [ - 0 => 'bool', + 'bool', ], 'path' => 'bool', 'useMessageForUser' => false, @@ -1309,7 +1378,7 @@ public function testCollectDenormalizationErrorsWithInvalidConstructorTypes() [ 'currentType' => 'bool', 'expectedTypes' => [ - 0 => 'int', + 'int', ], 'path' => 'int', 'useMessageForUser' => false, @@ -1481,7 +1550,7 @@ public function testPartialDenormalizationWithMissingConstructorTypes() 'expectedTypes' => [ 'unknown', ], - 'path' => null, + 'path' => 'two', 'useMessageForUser' => true, 'message' => 'Failed to create object because the class misses the "two" property.', ], diff --git a/src/Symfony/Component/Serializer/composer.json b/src/Symfony/Component/Serializer/composer.json index 3ec14ae0fc313..ec5e37a3ff1d0 100644 --- a/src/Symfony/Component/Serializer/composer.json +++ b/src/Symfony/Component/Serializer/composer.json @@ -33,7 +33,7 @@ "symfony/http-foundation": "^4.4|^5.0|^6.0", "symfony/http-kernel": "^4.4|^5.0|^6.0", "symfony/mime": "^4.4|^5.0|^6.0", - "symfony/property-access": "^5.4|^6.0", + "symfony/property-access": "^5.4.26|^6.3", "symfony/property-info": "^5.4.24|^6.2.11", "symfony/uid": "^5.3|^6.0", "symfony/validator": "^4.4|^5.0|^6.0",