Skip to content

[Serializer] Fix nested deserialization_path computation when there is no metadata for the attribute #45684

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -239,12 +239,12 @@ private function getAttributeNormalizationContext(object $object, string $attrib
*/
private function getAttributeDenormalizationContext(string $class, string $attribute, array $context): array
{
$context['deserialization_path'] = ($context['deserialization_path'] ?? false) ? $context['deserialization_path'].'.'.$attribute : $attribute;

if (null === $metadata = $this->getAttributeMetadata($class, $attribute)) {
return $context;
}

$context['deserialization_path'] = ($context['deserialization_path'] ?? false) ? $context['deserialization_path'].'.'.$attribute : $attribute;

return array_merge($context, $metadata->getDenormalizationContextForGroups($this->getGroups($context)));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ final class Php74Full
public array $collection;
public Php74FullWithConstructor $php74FullWithConstructor;
public DummyMessageInterface $dummyMessage;
/** @var TestFoo[] $nestedArray */
public TestFoo $nestedObject;
}


Expand All @@ -38,3 +40,8 @@ public function __construct($constructorArgument)
{
}
}

final class TestFoo
{
public int $int;
}
73 changes: 56 additions & 17 deletions src/Symfony/Component/Serializer/Tests/SerializerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -739,8 +739,12 @@ public function testDeserializeAndUnwrap()
);
}

/** @requires PHP 7.4 */
public function testCollectDenormalizationErrors()
/**
* @dataProvider provideCollectDenormalizationErrors
*
* @requires PHP 7.4
*/
public function testCollectDenormalizationErrors(?ClassMetadataFactory $classMetadataFactory)
{
$json = '
{
Expand All @@ -764,10 +768,12 @@ public function testCollectDenormalizationErrors()
],
"php74FullWithConstructor": {},
"dummyMessage": {
},
"nestedObject": {
"int": "string"
}
}';

$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
$extractor = new PropertyInfoExtractor([], [new PhpDocExtractor(), new ReflectionExtractor()]);

$serializer = new Serializer(
Expand All @@ -777,7 +783,7 @@ public function testCollectDenormalizationErrors()
new DateTimeZoneNormalizer(),
new DataUriNormalizer(),
new UidNormalizer(),
new ObjectNormalizer($classMetadataFactory, null, null, $extractor, new ClassDiscriminatorFromClassMetadata($classMetadataFactory)),
new ObjectNormalizer($classMetadataFactory, null, null, $extractor, $classMetadataFactory ? new ClassDiscriminatorFromClassMetadata($classMetadataFactory) : null),
],
['json' => new JsonEncoder()]
);
Expand Down Expand Up @@ -913,22 +919,45 @@ public function testCollectDenormalizationErrors()
'useMessageForUser' => true,
'message' => 'Failed to create object because the object miss the "constructorArgument" property.',
],
$classMetadataFactory ?
[
'currentType' => 'null',
'expectedTypes' => [
'string',
],
'path' => 'dummyMessage.type',
'useMessageForUser' => false,
'message' => 'Type property "type" not found for the abstract object "Symfony\Component\Serializer\Tests\Fixtures\DummyMessageInterface".',
] :
[
'currentType' => 'array',
'expectedTypes' => [
DummyMessageInterface::class,
],
'path' => 'dummyMessage',
'useMessageForUser' => false,
'message' => 'The type of the "dummyMessage" attribute for class "Symfony\Component\Serializer\Tests\Fixtures\Php74Full" must be one of "Symfony\Component\Serializer\Tests\Fixtures\DummyMessageInterface" ("array" given).',
],
[
'currentType' => 'null',
'currentType' => 'string',
'expectedTypes' => [
'string',
'int',
],
'path' => 'dummyMessage.type',
'useMessageForUser' => false,
'message' => 'Type property "type" not found for the abstract object "Symfony\Component\Serializer\Tests\Fixtures\DummyMessageInterface".',
'path' => 'nestedObject[int]',
'useMessageForUser' => true,
'message' => 'The type of the key "int" must be "int" ("string" given).',
],
];

$this->assertSame($expected, $exceptionsAsArray);
}

/** @requires PHP 7.4 */
public function testCollectDenormalizationErrors2()
/**
* @dataProvider provideCollectDenormalizationErrors
*
* @requires PHP 7.4
*/
public function testCollectDenormalizationErrors2(?ClassMetadataFactory $classMetadataFactory)
{
$json = '
[
Expand All @@ -940,13 +969,12 @@ public function testCollectDenormalizationErrors2()
}
]';

$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
$extractor = new PropertyInfoExtractor([], [new PhpDocExtractor(), new ReflectionExtractor()]);

$serializer = new Serializer(
[
new ArrayDenormalizer(),
new ObjectNormalizer($classMetadataFactory, null, null, $extractor, new ClassDiscriminatorFromClassMetadata($classMetadataFactory)),
new ObjectNormalizer($classMetadataFactory, null, null, $extractor, $classMetadataFactory ? new ClassDiscriminatorFromClassMetadata($classMetadataFactory) : null),
],
['json' => new JsonEncoder()]
);
Expand Down Expand Up @@ -999,17 +1027,20 @@ public function testCollectDenormalizationErrors2()
$this->assertSame($expected, $exceptionsAsArray);
}

/** @requires PHP 8.0 */
public function testCollectDenormalizationErrorsWithConstructor()
/**
* @dataProvider provideCollectDenormalizationErrors
*
* @requires PHP 8.0
*/
public function testCollectDenormalizationErrorsWithConstructor(?ClassMetadataFactory $classMetadataFactory)
{
$json = '{"bool": "bool"}';

$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
$extractor = new PropertyInfoExtractor([], [new PhpDocExtractor(), new ReflectionExtractor()]);

$serializer = new Serializer(
[
new ObjectNormalizer($classMetadataFactory, null, null, $extractor, new ClassDiscriminatorFromClassMetadata($classMetadataFactory)),
new ObjectNormalizer($classMetadataFactory, null, null, $extractor, $classMetadataFactory ? new ClassDiscriminatorFromClassMetadata($classMetadataFactory) : null),
],
['json' => new JsonEncoder()]
);
Expand Down Expand Up @@ -1050,6 +1081,14 @@ public function testCollectDenormalizationErrorsWithConstructor()

$this->assertSame($expected, $exceptionsAsArray);
}

public function provideCollectDenormalizationErrors()
{
return [
[null],
[new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()))],
];
}
}

class Model
Expand Down