Skip to content

Commit

Permalink
[Serializer] Fix denormalization of object with typed constructor arg…
Browse files Browse the repository at this point in the history
… (not castable) and with COLLECT_DENORMALIZATION_ERRORS

There is already of test with it worked "by chance".
The test is testCollectDenormalizationErrorsWithConstructor.
It worked because `$reflectionClass->newInstanceArgs($params)` works if
once give a `string` value that must fit in a `bool` value. PHP casts
automatically the value for us. (see: https://3v4l.org/GYr0T)

Now we ensure to not throw an exception if the instantiation cannot be
done. The missmatch is already collected few lines above, so there
nothing to do more
  • Loading branch information
lyrixx committed Apr 15, 2023
1 parent 547e876 commit 4f238a7
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
Expand Up @@ -417,7 +417,15 @@ protected function instantiateObject(array &$data, string $class, array &$contex
}

if ($constructor->isConstructor()) {
return $reflectionClass->newInstanceArgs($params);
try {
return $reflectionClass->newInstanceArgs($params);
} catch (\TypeError $th) {
if (!isset($context['not_normalizable_value_exceptions'])) {
throw $th;
}

return $reflectionClass->newInstanceWithoutConstructor();
}
} else {
return $constructor->invokeArgs(null, $params);
}
Expand Down
8 changes: 8 additions & 0 deletions src/Symfony/Component/Serializer/Tests/Fixtures/Php74Full.php
Expand Up @@ -28,6 +28,7 @@ final class Php74Full
/** @var Php74Full[] */
public array $collection;
public Php74FullWithConstructor $php74FullWithConstructor;
public Php74FullWithTypedConstructor $php74FullWithTypedConstructor;
public DummyMessageInterface $dummyMessage;
/** @var TestFoo[] $nestedArray */
public TestFoo $nestedObject;
Expand All @@ -43,6 +44,13 @@ public function __construct($constructorArgument)
}
}

final class Php74FullWithTypedConstructor
{
public function __construct(float $something)
{
}
}

final class TestFoo
{
public int $int;
Expand Down
12 changes: 12 additions & 0 deletions src/Symfony/Component/Serializer/Tests/SerializerTest.php
Expand Up @@ -852,6 +852,9 @@ public function testCollectDenormalizationErrors(?ClassMetadataFactory $classMet
}
],
"php74FullWithConstructor": {},
"php74FullWithTypedConstructor": {
"something": "not a float"
},
"dummyMessage": {
},
"nestedObject": {
Expand Down Expand Up @@ -1005,6 +1008,15 @@ public function testCollectDenormalizationErrors(?ClassMetadataFactory $classMet
'useMessageForUser' => true,
'message' => 'Failed to create object because the class misses the "constructorArgument" property.',
],
[
'currentType' => 'string',
'expectedTypes' => [
'float',
],
'path' => 'php74FullWithTypedConstructor',
'useMessageForUser' => false,
'message' => 'The type of the "something" attribute for class "Symfony\Component\Serializer\Tests\Fixtures\Php74FullWithTypedConstructor" must be one of "float" ("string" given).',
],
$classMetadataFactory ?
[
'currentType' => 'null',
Expand Down

0 comments on commit 4f238a7

Please sign in to comment.