8000 Handle type error constructing input · symfony/symfony@3e32f65 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3e32f65

Browse files
committed
Handle type error constructing input
1 parent 68f27ef commit 3e32f65

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,28 @@ protected function instantiateObject(array &$data, string $class, array &$contex
425425
throw $e;
426426
}
427427

428+
$message = $e->getMessage();
429+
430+
if (preg_match('/#\d+ \(\$([^)]+)\) must be of type ([^,]+), ([^,]+) given/', $message, $match)) {
431+
$message = sprintf(
432+
'The type of the "%s" parameter for class "%s" must be of type "%s" ("%s" given).',
433+
$match[1],
434+
$class,
435+
$match[2],
436+
$match[3],
437+
);
438+
}
439+
440+
$exception = NotNormalizableValueException::createForUnexpectedDataType(
441+
$message,
442+
$data,
443+
[$class],
444+
$context['deserialization_path'] ?? null,
445+
true,
446+
previous: $e,
447+
);
448+
$context['not_normalizable_value_exceptions'][] = $exception;
449+
428450
return $reflectionClass->newInstanceWithoutConstructor();
429451
}
430452
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1021,6 +1021,15 @@ public function testCollectDenormalizationErrors(?ClassMetadataFactory $classMet
10211021
'useMessageForUser' => false,
10221022
'message' => 'The type of the "something" attribute for class "Symfony\Component\Serializer\Tests\Fixtures\Php74FullWithTypedConstructor" must be one of "float" ("string" given).',
10231023
],
1024+
[
1025+
'currentType' => 'array',
1026+
'expectedTypes' => [
1027+
'Symfony\Component\Serializer\Tests\Fixtures\Php74FullWithTypedConstructor',
1028+
],
1029+
'path' => 'php74FullWithTypedConstructor',
1030+
'useMessageForUser' => true,
1031+
'message' => 'The type of the "something" parameter for class "Symfony\Component\Serializer\Tests\Fixtures\Php74FullWithTypedConstructor" must be of type "float" ("string" given).',
1032+
],
10241033
$classMetadataFactory ?
10251034
[
10261035
'currentType' => 'null',

0 commit comments

Comments
 (0)
0