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

Skip to content

Commit 258ae0f

Browse files
committed
[Serializer] Fix collecting only first missing constructor argument
1 parent 2f34df3 commit 258ae0f

File tree

4 files changed

+41
-4
lines changed

4 files changed

+41
-4
lines changed

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -413,8 +413,6 @@ protected function instantiateObject(array &$data, string $class, array &$contex
413413
true
414414
);
415415
$context['not_normalizable_value_exceptions'][] = $exception;
416-
417-
return $reflectionClass->newInstanceWithoutConstructor();
418416
}
419417
}
420418

src/Symfony/Component/Serializer/Tests/Fixtures/Php80WithPromotedTypedConstructor.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@
1313

1414
final class Php80WithPromotedTypedConstructor
1515
{
16-
public function __construct(public bool $bool)
17-
{
16+
public function __construct(
17+
public bool $bool,
18+
public string $string,
19+
public int $int,
20+
) {
1821
}
1922
}

src/Symfony/Component/Serializer/Tests/Normalizer/Features/ConstructorArgumentsTestTrait.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,22 @@ public function testConstructorWithMissingData()
7070
self::assertSame(['bar', 'baz'], $e->getMissingConstructorArguments());
7171
}
7272
}
73+
74+
public function testExceptionsAreCollectedForConstructorWithMissingData()
75+
{
76+
$data = [
77+
'foo' => 10,
78+
];
79+
80+
$exceptions = [];
81+
82+
$normalizer = $this->getDenormalizerForConstructArguments();
83+
$normalizer->denormalize($data, ConstructorArgumentsObject::class, null, [
84+
'not_normalizable_value_exceptions' => &$exceptions,
85+
]);
86+
87+
self::assertCount(2, $exceptions);
88+
self::assertSame('Failed to create object because the class misses the "bar" property.', $exceptions[0]->getMessage());
89+
self::assertSame('Failed to create object because the class misses the "baz" property.', $exceptions[1]->getMessage());
90+
}
7391
}

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1196,6 +1196,24 @@ public function testCollectDenormalizationErrorsWithConstructor(?ClassMetadataFa
11961196
'useMessageForUser' => false,
11971197
'message' => 'The type of the "bool" attribute for class "Symfony\\Component\\Serializer\\Tests\\Fixtures\\Php80WithPromotedTypedConstructor" must be one of "bool" ("string" given).',
11981198
],
1199+
[
1200+
'currentType' => 'array',
1201+
'expectedTypes' => [
1202+
'unknown',
1203+
],
1204+
'path' => null,
1205+
'useMessageForUser' => true,
1206+
'message' => 'Failed to create object because the class misses the "string" property.',
1207+
],
1208+
[
1209+
'currentType' => 'array',
1210+
'expectedTypes' => [
1211+
'unknown',
1212+
],
1213+
'path' => null,
1214+
'useMessageForUser' => true,
1215+
'message' => 'Failed to create object because the class misses the "int" property.',
1216+
],
11991217
];
12001218

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

0 commit comments

Comments
 (0)