Skip to content

Commit

Permalink
bug #53108 [Serializer] Fix using deserialization path 5.4 (HypeMC)
Browse files Browse the repository at this point in the history
This PR was merged into the 5.4 branch.

Discussion
----------

[Serializer] Fix using deserialization path 5.4

| Q             | A
| ------------- | ---
| Branch?       | 5.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Issues        | -
| License       | MIT

I've noticed this while working on #53107.

Sometimes, the `path` provided to the `NotNormalizableValueException` is incorrect.

Commits
-------

d498de7 [Serializer] Fix using deserialization path
  • Loading branch information
nicolas-grekas committed Jan 2, 2024
2 parents 02ff4c6 + d498de7 commit d70b158
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 9 deletions.
Expand Up @@ -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;
Expand Down
Expand Up @@ -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
Expand Down
81 changes: 75 additions & 6 deletions src/Symfony/Component/Serializer/Tests/SerializerTest.php
Expand Up @@ -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.',
],
Expand Down Expand Up @@ -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
*
Expand Down Expand Up @@ -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.',
],
Expand All @@ -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.',
],
Expand Down Expand Up @@ -1300,7 +1369,7 @@ public function testCollectDenormalizationErrorsWithInvalidConstructorTypes()
[
'currentType' => 'string',
'expectedTypes' => [
0 => 'bool',
'bool',
],
'path' => 'bool',
'useMessageForUser' => false,
Expand All @@ -1309,7 +1378,7 @@ public function testCollectDenormalizationErrorsWithInvalidConstructorTypes()
[
'currentType' => 'bool',
'expectedTypes' => [
0 => 'int',
'int',
],
'path' => 'int',
'useMessageForUser' => false,
Expand Down Expand Up @@ -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.',
],
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Serializer/composer.json
Expand Up @@ -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",
Expand Down

0 comments on commit d70b158

Please sign in to comment.