8000 bug #53108 [Serializer] Fix using deserialization path 5.4 (HypeMC) · symfony/symfony@d70b158 · GitHub
[go: up one dir, main page]

Skip to content

Commit d70b158

Browse files
bug #53108 [Serializer] Fix using deserialization path 5.4 (HypeMC)
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
2 parents 02ff4c6 + d498de7 commit d70b158

File tree

4 files changed

+78
-9
lines changed

4 files changed

+78
-9
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ protected function instantiateObject(array &$data, string $class, array &$contex
414414
sprintf('Failed to create object because the class misses the "%s" property.', $constructorParameter->name),
415415
$data,
416416
['unknown'],
417-
$objectDeserializationPath,
417+
$context['deserialization_path'],
418418
true
419419
);
420420
$context['not_normalizable_value_exceptions'][] = $exception;

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@ public function denormalize($data, string $type, string $format = null, array $c
437437
sprintf('Failed to denormalize attribute "%s" value for class "%s": '.$e->getMessage(), $attribute, $type),
438438
$data,
439439
['unknown'],
440-
$context['deserialization_path'] ?? null,
440+
$attributeContext['deserialization_path'] ?? null,
441441
false,
442442
$e->getCode(),
443443
$e

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

+75-6
Original file line numberDiff line numberDiff line change
@@ -1049,7 +1049,7 @@ public function testCollectDenormalizationErrors(?ClassMetadataFactory $classMet
10491049
'expectedTypes' => [
10501050
'unknown',
10511051
],
1052-
'path' => 'php74FullWithConstructor',
1052+
'path' => 'php74FullWithConstructor.constructorArgument',
10531053
'useMessageForUser' => true,
10541054
'message' => 'Failed to create object because the class misses the "constructorArgument" property.',
10551055
],
@@ -1186,6 +1186,75 @@ public function testCollectDenormalizationErrors2(?ClassMetadataFactory $classMe
11861186
$this->assertSame($expected, $exceptionsAsArray);
11871187
}
11881188

1189+
/**
1190+
* @requires PHP 7.4
1191+
*/
1192+
public function testCollectDenormalizationErrorsWithoutTypeExtractor()
1193+
{
1194+
$json = '
1195+
{
1196+
"string": [],
1197+
"int": [],
1198+
"float": []
1199+
}';
1200+
1201+
$serializer = new Serializer([new ObjectNormalizer()], ['json' => new JsonEncoder()]);
1202+
1203+
try {
1204+
$serializer->deserialize($json, Php74Full::class, 'json', [
1205+
DenormalizerInterface::COLLECT_DENORMALIZATION_ERRORS => true,
1206+
]);
1207+
1208+
$this->fail();
1209+
} catch (\Throwable $th) {
1210+
$this->assertInstanceOf(PartialDenormalizationException::class, $th);
1211+
}
1212+
1213+
$this->assertInstanceOf(Php74Full::class, $th->getData());
1214+
1215+
$exceptionsAsArray = array_map(function (NotNormalizableValueException $e): array {
1216+
return [
1217+
'currentType' => $e->getCurrentType(),
1218+
'expectedTypes' => $e->getExpectedTypes(),
1219+
'path' => $e->getPath(),
1220+
'useMessageForUser' => $e->canUseMessageForUser(),
1221+
'message' => $e->getMessage(),
1222+
];
1223+
}, $th->getErrors());
1224+
1225+
$expected = [
1226+
[
1227+
'currentType' => 'array',
1228+
'expectedTypes' => [
1229+
'unknown',
1230+
],
1231+
'path' => 'string',
1232+
'useMessageForUser' => false,
1233+
'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".',
1234+
],
1235+
[
1236+
'currentType' => 'array',
1237+
'expectedTypes' => [
1238+
'unknown',
1239+
],
1240+
'path' => 'int',
1241+
'useMessageForUser' => false,
1242+
'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".',
1243+
],
1244+
[
1245+
'currentType' => 'array',
1246+
'expectedTypes' => [
1247+
'unknown',
1248+
],
1249+
'path' => 'float',
1250+
'useMessageForUser' => false,
1251+
'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".',
1252+
],
1253+
];
1254+
1255+
$this->assertSame($expected, $exceptionsAsArray);
1256+
}
1257+
11891258
/**
11901259
* @dataProvider provideCollectDenormalizationErrors
11911260
*
@@ -1241,7 +1310,7 @@ public function testCollectDenormalizationErrorsWithConstructor(?ClassMetadataFa
12411310
'expectedTypes' => [
12421311
'unknown',
12431312
],
1244-
'path' => null,
1313+
'path' => 'string',
12451314
'useMessageForUser' => true,
12461315
'message' => 'Failed to create object because the class misses the "string" property.',
12471316
],
@@ -1250,7 +1319,7 @@ public function testCollectDenormalizationErrorsWithConstructor(?ClassMetadataFa
12501319
'expectedTypes' => [
12511320
'unknown',
12521321
],
1253-
'path' => null,
1322+
'path' => 'int',
12541323
'useMessageForUser' => true,
12551324
'message' => 'Failed to create object because the class misses the "int" property.',
12561325
],
@@ -1300,7 +1369,7 @@ public function testCollectDenormalizationErrorsWithInvalidConstructorTypes()
13001369
[
13011370
'currentType' => 'string',
13021371
'expectedTypes' => [
1303-
0 => 'bool',
1372+
'bool',
13041373
],
13051374
'path' => 'bool',
13061375
'useMessageForUser' => false,
@@ -1309,7 +1378,7 @@ public function testCollectDenormalizationErrorsWithInvalidConstructorTypes()
13091378
[
13101379
'currentType' => 'bool',
13111380
'expectedTypes' => [
1312-
0 => 'int',
1381+
'int',
13131382
],
13141383
'path' => 'int',
13151384
'useMessageForUser' => false,
@@ -1481,7 +1550,7 @@ public function testPartialDenormalizationWithMissingConstructorTypes()
14811550
'expectedTypes' => [
14821551
'unknown',
14831552
],
1484-
'path' => null,
1553+
'path' => 'two',
14851554
'useMessageForUser' => true,
14861555
'message' => 'Failed to create object because the class misses the "two" property.',
14871556
],

src/Symfony/Component/Serializer/composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
"symfony/http-foundation": "^4.4|^5.0|^6.0",
3434
"symfony/http-kernel": "^4.4|^5.0|^6.0",
3535
"symfony/mime": "^4.4|^5.0|^6.0",
36-
"symfony/property-access": "^5.4|^6.0",
36+
"symfony/property-access": "^5.4.26|^6.3",
3737
"symfony/property-info": "^5.4.24|^6.2.11",
3838
"symfony/uid": "^5.3|^6.0",
3939
"symfony/validator": "^4.4|^5.0|^6.0",

0 commit comments

Comments
 (0)
0