8000 [Serializer] Handle type error constructing input by kylekatarnls · Pull Request #51003 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Serializer] Handle type error constructing input #51003

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

Open
wants to merge 1 commit into
base: 7.4
Choose a base branch
from
Open
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
Handle type error constructing input
  • Loading branch information
kylekatarnls committed Jul 17, 2023
commit 3e32f657f5aed769d9d4d110b2b59efed9d23703
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,28 @@ protected function instantiateObject(array &$data, string $class, array &$contex
throw $e;
}

$message = $e->getMessage();

if (preg_match('/#\d+ \(\$([^)]+)\) must be of type ([^,]+), ([^,]+) given/', $message, $match)) {
$message = sprintf(
'The type of the "%s" parameter for class "%s" must be of type "%s" ("%s" given).',
$match[1],
$class,
$match[2],
$match[3],
);
}

$exception = NotNormalizableValueException::createForUnexpectedDataType(
$message,
$data,
[$class],
$context['deserialization_path'] ?? null,
true,
previous: $e,
);
Comment on lines +430 to +447
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps it would be better this way

Suggested change
if (preg_match('/#\d+ \(\$([^)]+)\) must be of type ([^,]+), ([^,]+) given/', $message, $match)) {
$message = sprintf(
'The type of the "%s" parameter for class "%s" must be of type "%s" ("%s" given).',
$match[1],
$class,
$match[2],
$match[3],
);
}
$exception = NotNormalizableValueException::createForUnexpectedDataType(
$message,
$data,
[$class],
$context['deserialization_path'] ?? null,
true,
previous: $e,
);
$type = $class;
if (preg_match('/#\d+ \(\$([^)]+)\) must be of type ([^,]+), ([^,]+) given/', $message, $match)) {
$message = sprintf(
'The type of the "%s" parameter for class "%s" must be of type "%s" ("%s" given).',
$match[1],
$class,
$match[2],
$match[3],
);
$type = $match[2];
}
$exception = NotNormalizableValueException::createForUnexpectedDataType(
$message,
$data,
[$type],
$context['deserialization_path'] ?? null,
true,
previous: $e,
);

$context['not_normalizable_value_exceptions'][] = $exception;

return $reflectionClass->newInstanceWithoutConstructor();
}
}
Expand Down
9 changes: 9 additions & 0 deletions src/Symfony/Component/Serializer/Tests/SerializerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1021,6 +1021,15 @@ public function testCollectDenormalizationErrors(?ClassMetadataFactory $classMet
'useMessageForUser' => false,
'message' => 'The type of the "something" attribute for class "Symfony\Component\Serializer\Tests\Fixtures\Php74FullWithTypedConstructor" must be one of "float" ("string" given).',
],
[
'currentType' => 'array',
'expectedTypes' => [
'Symfony\Component\Serializer\Tests\Fixtures\Php74FullWithTypedConstructor',
],
'path' => 'php74FullWithTypedConstructor',
'useMessageForUser' => true,
'message' => 'The type of the "something" parameter for class "Symfony\Component\Serializer\Tests\Fixtures\Php74FullWithTypedConstructor" must be of type "float" ("string" given).',
],
$classMetadataFactory ?
[
'currentType' => 'null',
Expand Down
0