8000 [Serializer] Fix denormalization of object with typed constructor arg (not castable) and with COLLECT_DENORMALIZATION_ERRORS by lyrixx · Pull Request #50024 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Serializer] Fix denormalization of object with typed constructor arg (not castable) and with COLLECT_DENORMALIZATION_ERRORS #50024

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 15, 2023
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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