Thanks to visit codestin.com
Credit goes to github.com

Skip to content
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
[Serializer] Fix using deserialization path
  • Loading branch information
HypeMC committed Dec 31, 2023
commit d498de7fc2b7de1fda56675d719de4756694e733
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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