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

Skip to content

Commit 26cee32

Browse files
committed
Fix RequestPayloadValueResolver handling error with no ExpectedTypes
1 parent 39f4f1a commit 26cee32

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

src/Symfony/Component/HttpKernel/Controller/ArgumentResolver/RequestPayloadValueResolver.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,15 @@ public function onKernelControllerArguments(ControllerArgumentsEvent $event): vo
108108
} catch (PartialDenormalizationException $e) {
109109
$trans = $this->translator ? $this->translator->trans(...) : fn ($m, $p) => strtr($m, $p);
110110
foreach ($e->getErrors() as $error) {
111-
$parameters = ['{{ type }}' => implode('|', $error->getExpectedTypes())];
111+
$parameters = [];
112+
$template = 'This value was of an unexpected type.';
113+
if ($expectedTypes = $error->getExpectedTypes()) {
114+
$template = 'This value should be of type {{ type }}.';
115+
$parameters['{{ type }}'] = implode('|', $expectedTypes);
116+
}
112117
if ($error->canUseMessageForUser()) {
113118
$parameters['hint'] = $error->getMessage();
114119
}
115-
$template = 'This value should be of type {{ type }}.';
116120
$message = $trans($template, $parameters, 'validators');
117121
$violations->add(new ConstraintViolation($message, $template, $parameters, null, $error->getPath(), null));
118122
}

src/Symfony/Component/HttpKernel/Tests/Controller/ArgumentResolver/RequestPayloadValueResolverTest.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,12 @@
2323
use Symfony\Component\HttpKernel\HttpKernelInterface;
2424
use Symfony\Component\Serializer\Encoder\JsonEncoder;
2525
use Symfony\Component\Serializer\Encoder\XmlEncoder;
26+
use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
2627
use Symfony\Component\Serializer\Exception\PartialDenormalizationException;
28+
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
2729
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
2830
use Symfony\Component\Serializer\Serializer;
31+
use Symfony\Component\Serializer\SerializerInterface;
2932
use Symfony\Component\Validator\Constraints as Assert;
3033
use Symfony\Component\Validator\ConstraintViolation;
3134
use Symfony\Component\Validator\ConstraintViolationList;
@@ -307,6 +310,39 @@ public function testRequestContentValidationPassed()
307310
$this->assertEquals([$payload], $event->getArguments());
308311
}
309312

313+
/** @dataProvider provideNoTypes */
314+
public function testRequestContentWithUntypedErrors(?array $types)
315+
{
316+
$this->expectException(HttpException::class);
317+
$this->expectExceptionMessage('This value was of an unexpected type.');
318+
$serializer = $this->createMock(SerializerDenormalizer::class);
319+
320+
if (null === $types) {
321+
$exception = new NotNormalizableValueException('Error with no types');
322+
} else {
323+
$exception = NotNormalizableValueException::createForUnexpectedDataType('Error with no types', '', []);
324+
}
325+
$serializer->method('deserialize')->willThrowException(new PartialDenormalizationException([], [$exception]));
326+
327+
$resolver = new RequestPayloadValueResolver($serializer, $this->createMock(ValidatorInterface::class));
328+
$request = Request::create('/', 'POST', server: ['CONTENT_TYPE' => 'application/json'], content: '{"price": 50}');
329+
330+
$arguments = $resolver->resolve($request, new ArgumentMetadata('valid', RequestPayload::class, false, false, null, false, [
331+
MapRequestPayload::class => new MapRequestPayload(),
332+
]));
333+
$event = new ControllerArgumentsEvent($this->createMock(HttpKernelInterface::class), function () {}, $arguments, $request, HttpKernelInterface::MAIN_REQUEST);
334+
335+
$resolver->onKernelControllerArguments($event);
336+
}
337+
338+
public static function provideNoTypes(): array
339+
{
340+
return [
341+
[null],
342+
[[]],
343+
];
344+
}
345+
310346
public function testQueryStringValidationPassed()
311347
{
312348
$payload = new RequestPayload(50);
@@ -612,3 +648,7 @@ public function __construct(public readonly float $price)
612648
{
613649
}
614650
}
651+
652+
interface SerializerDenormalizer extends SerializerInterface, DenormalizerInterface
653+
{
654+
}

0 commit comments

Comments
 (0)