Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitdf2e7cd

Browse files
committed
[Serializer] Fix denormalization of object with typed constructor arg (not castable) and with COLLECT_DENORMALIZATION_ERRORS
1 parent547e876 commitdf2e7cd

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,15 @@ protected function instantiateObject(array &$data, string $class, array &$contex
417417
}
418418

419419
if ($constructor->isConstructor()) {
420-
return$reflectionClass->newInstanceArgs($params);
420+
try {
421+
return$reflectionClass->newInstanceArgs($params);
422+
}catch (\TypeError$th) {
423+
if (!isset($context['not_normalizable_value_exceptions'])) {
424+
throw$th;
425+
}
426+
427+
return$reflectionClass->newInstanceWithoutConstructor();
428+
}
421429
}else {
422430
return$constructor->invokeArgs(null,$params);
423431
}

‎src/Symfony/Component/Serializer/Tests/Fixtures/Php74Full.php‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ final class Php74Full
2828
/** @var Php74Full[] */
2929
publicarray$collection;
3030
publicPhp74FullWithConstructor$php74FullWithConstructor;
31+
publicPhp74FullWithTypedConstructor$php74FullWithTypedConstructor;
3132
publicDummyMessageInterface$dummyMessage;
3233
/** @var TestFoo[] $nestedArray */
3334
publicTestFoo$nestedObject;
@@ -43,6 +44,13 @@ public function __construct($constructorArgument)
4344
}
4445
}
4546

47+
finalclass Php74FullWithTypedConstructor
48+
{
49+
publicfunction__construct(float$something)
50+
{
51+
}
52+
}
53+
4654
finalclass TestFoo
4755
{
4856
publicint$int;

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -852,6 +852,9 @@ public function testCollectDenormalizationErrors(?ClassMetadataFactory $classMet
852852
}
853853
],
854854
"php74FullWithConstructor": {},
855+
"php74FullWithTypedConstructor": {
856+
"something": "not a float"
857+
},
855858
"dummyMessage": {
856859
},
857860
"nestedObject": {
@@ -1005,6 +1008,15 @@ public function testCollectDenormalizationErrors(?ClassMetadataFactory $classMet
10051008
'useMessageForUser' =>true,
10061009
'message' =>'Failed to create object because the class misses the "constructorArgument" property.',
10071010
],
1011+
[
1012+
'currentType' =>'string',
1013+
'expectedTypes' => [
1014+
'float',
1015+
],
1016+
'path' =>'php74FullWithTypedConstructor',
1017+
'useMessageForUser' =>false,
1018+
'message' =>'The type of the "something" attribute for class "Symfony\Component\Serializer\Tests\Fixtures\Php74FullWithTypedConstructor" must be one of "float" ("string" given).',
1019+
],
10081020
$classMetadataFactory ?
10091021
[
10101022
'currentType' =>'null',

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp