Skip to content

Commit

Permalink
bug #52713 [Serializer] Fix deserialization_path missing using contru…
Browse files Browse the repository at this point in the history
…ctor (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 #44925 #52683
| License       | MIT

While trying to fix #44925, I used a wrong approach (#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
  • Loading branch information
xabbuh committed Nov 24, 2023
2 parents a560853 + 8a33f53 commit cd98a3f
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 12 deletions.
Expand Up @@ -351,10 +351,14 @@ protected function instantiateObject(array &$data, string $class, array &$contex
$missingConstructorArguments = [];
$params = [];
$unsetKeys = [];
$objectDeserializationPath = $context['deserialization_path'] ?? null;

foreach ($constructorParameters as $constructorParameter) {
$paramName = $constructorParameter->name;
$key = $this->nameConverter ? $this->nameConverter->normalize($paramName, $class, $format, $context) : $paramName;

$context['deserialization_path'] = $objectDeserializationPath ? $objectDeserializationPath.'.'.$paramName : $paramName;

$allowed = false === $allowedAttributes || \in_array($paramName, $allowedAttributes);
$ignored = !$this->isAllowedAttribute($class, $paramName, $format, $context);
if ($constructorParameter->isVariadic()) {
Expand Down Expand Up @@ -410,13 +414,15 @@ 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,
$objectDeserializationPath,
true
);
$context['not_normalizable_value_exceptions'][] = $exception;
}
}

$context['deserialization_path'] = $objectDeserializationPath;

if ($missingConstructorArguments) {
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);
}
Expand Down Expand Up @@ -489,8 +495,6 @@ protected function denormalizeParameter(\ReflectionClass $class, \ReflectionPara
*/
protected function createChildContext(array $parentContext, string $attribute, ?string $format): array
{
$parentContext['deserialization_path'] = ($parentContext['deserialization_path'] ?? false) ? $parentContext['deserialization_path'].'.'.$attribute : $attribute;

if (isset($parentContext[self::ATTRIBUTES][$attribute])) {
$parentContext[self::ATTRIBUTES] = $parentContext[self::ATTRIBUTES][$attribute];
} else {
Expand Down
9 changes: 0 additions & 9 deletions src/Symfony/Component/Serializer/Tests/SerializerTest.php
Expand Up @@ -1024,15 +1024,6 @@ public function testCollectDenormalizationErrors(?ClassMetadataFactory $classMet
'useMessageForUser' => true,
'message' => 'Failed to create object because the class misses the "constructorArgument" property.',
],
[
'currentType' => 'string',
'expectedTypes' => [
'float',
],
'path' => 'php74FullWithTypedConstructor',
'useMessageForUser' => false,
'message' => 'The type of the "something" attribute for class "Symfony\Component\Serializer\Tests\Fixtures\Php74FullWithTypedConstructor" must be one of "float" ("string" given).',
],
[
'currentType' => 'string',
'expectedTypes' => [
Expand Down

0 comments on commit cd98a3f

Please sign in to comment.