diff --git a/src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php b/src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php index 2e11136ba0b87..7192186525830 100644 --- a/src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php +++ b/src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php @@ -403,7 +403,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'], - $context['deserialization_path'] ?? null, + $attributeContext['deserialization_path'] ?? null, 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 0ba9272a2b289..c7f0dde0e13e4 100644 --- a/src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php +++ b/src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php @@ -399,7 +399,7 @@ public function denormalize(mixed $data, string $type, string $format = null, ar 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 1d2b6ed6362c8..a9448118069cb 100644 --- a/src/Symfony/Component/Serializer/Tests/SerializerTest.php +++ b/src/Symfony/Component/Serializer/Tests/SerializerTest.php @@ -1066,7 +1066,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.', ], @@ -1199,6 +1199,70 @@ public function testCollectDenormalizationErrors2(?ClassMetadataFactory $classMe $this->assertSame($expected, $exceptionsAsArray); } + 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(fn (NotNormalizableValueException $e): array => [ + '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 */ @@ -1250,7 +1314,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.', ], @@ -1259,7 +1323,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.', ], diff --git a/src/Symfony/Component/Serializer/composer.json b/src/Symfony/Component/Serializer/composer.json index 5efabab21dc45..44c5a28aabdd3 100644 --- a/src/Symfony/Component/Serializer/composer.json +++ b/src/Symfony/Component/Serializer/composer.json @@ -33,7 +33,7 @@ "symfony/http-foundation": "^5.4|^6.0", "symfony/http-kernel": "^5.4|^6.0", "symfony/mime": "^5.4|^6.0", - "symfony/property-access": "^5.4|^6.0", + "symfony/property-access": "^5.4.4|^6.0.4", "symfony/property-info": "^5.4.24|^6.2.11", "symfony/uid": "^5.4|^6.0", "symfony/validator": "^5.4|^6.0",