Skip to content

Commit cd98a3f

Browse files
committed
bug symfony#52713 [Serializer] Fix deserialization_path missing using contructor (mtarld)
This PR was merged into the 5.4 branch. Discussion ---------- [Serializer] Fix deserialization_path missing using contructor | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | Fix symfony#44925 symfony#52683 | License | MIT While trying to fix symfony#44925, I used a wrong approach (symfony#52683), and therefore introduced a bug. This PR fixes it and solve the previous issue in a better way. Commits ------- 8a33f53 [Serializer] Fix deserialization_path missing using contructor
2 parents a560853 + 8a33f53 commit cd98a3f

File tree

2 files changed

+7
-12
lines changed

2 files changed

+7
-12
lines changed

src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php

+7-3
Original file line numberDiff line numberDiff line change
@@ -351,10 +351,14 @@ protected function instantiateObject(array &$data, string $class, array &$contex
351351
$missingConstructorArguments = [];
352352
$params = [];
353353
$unsetKeys = [];
354+
$objectDeserializationPath = $context['deserialization_path'] ?? null;
355+
354356
foreach ($constructorParameters as $constructorParameter) {
355357
$paramName = $constructorParameter->name;
356358
$key = $this->nameConverter ? $this->nameConverter->normalize($paramName, $class, $format, $context) : $paramName;
357359

360+
$context['deserialization_path'] = $objectDeserializationPath ? $objectDeserializationPath.'.'.$paramName : $paramName;
361+
358362
$allowed = false === $allowedAttributes || \in_array($paramName, $allowedAttributes);
359363
$ignored = !$this->isAllowedAttribute($class, $paramName, $format, $context);
360364
if ($constructorParameter->isVariadic()) {
@@ -410,13 +414,15 @@ protected function instantiateObject(array &$data, string $class, array &$contex
410414
sprintf('Failed to create object because the class misses the "%s" property.', $constructorParameter->name),
411415
$data,
412416
['unknown'],
413-
$context['deserialization_path'] ?? null,
417+
$objectDeserializationPath,
414418
true
415419
);
416420
$context['not_normalizable_value_exceptions'][] = $exception;
417421
}
418422
}
419423

424+
$context['deserialization_path'] = $objectDeserializationPath;
425+
420426
if ($missingConstructorArguments) {
421427
throw new MissingConstructorArgumentsException(sprintf('Cannot create an instance of "%s" from serialized data because its constructor requires the following parameters to be present : "$%s".', $class, implode('", "$', $missingConstructorArguments)), 0, null, $missingConstructorArguments);
422428
}
@@ -489,8 +495,6 @@ protected function denormalizeParameter(\ReflectionClass $class, \ReflectionPara
489495
*/
490496
protected function createChildContext(array $parentContext, string $attribute, ?string $format): array
491497
{
492-
$parentContext['deserialization_path'] = ($parentContext['deserialization_path'] ?? false) ? $parentContext['deserialization_path'].'.'.$attribute : $attribute;
493-
494498
if (isset($parentContext[self::ATTRIBUTES][$attribute])) {
495499
$parentContext[self::ATTRIBUTES] = $parentContext[self::ATTRIBUTES][$attribute];
496500
} else {

src/Symfony/Component/Serializer/Tests/SerializerTest.php

-9
Original file line numberDiff line numberDiff line change
@@ -1024,15 +1024,6 @@ public function testCollectDenormalizationErrors(?ClassMetadataFactory $classMet
10241024
'useMessageForUser' => true,
10251025
'message' => 'Failed to create object because the class misses the "constructorArgument" property.',
10261026
],
1027-
[
1028-
'currentType' => 'string',
1029-
'expectedTypes' => [
1030-
'float',
1031-
],
1032-
'path' => 'php74FullWithTypedConstructor',
1033-
'useMessageForUser' => false,
1034-
'message' => 'The type of the "something" attribute for class "Symfony\Component\Serializer\Tests\Fixtures\Php74FullWithTypedConstructor" must be one of "float" ("string" given).',
1035-
],
10361027
[
10371028
'currentType' => 'string',
10381029
'expectedTypes' => [

0 commit comments

Comments
 (0)