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

Skip to content

Commit 0af85fe

Browse files
Jeroenynicolas-grekas
authored andcommitted
Fix RequestPayloadValueResolver handling error with no ExpectedTypes
1 parent abdb713 commit 0af85fe

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

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

+6-2
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

+35
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\ConstraintViolationList;
3134
use Symfony\Component\Validator\Exception\ValidationFailedException;
@@ -332,6 +335,34 @@ public function testRequestContentValidationPassed()
332335
$this->assertEquals([$payload], $event->getArguments());
333336
}
334337

338+
/**
339+
* @testWith [null]
340+
* [[]]
341+
*/
342+
public function testRequestContentWithUntypedErrors(?array $types)
343+
{
344+
$this->expectException(HttpException::class);
345+
$this->expectExceptionMessage('This value was of an unexpected type.');
346+
$serializer = $this->createMock(SerializerDenormalizer::class);
347+
348+
if (null === $types) {
349+
$exception = new NotNormalizableValueException('Error with no types');
350+
} else {
351+
$exception = NotNormalizableValueException::createForUnexpectedDataType('Error with no types', '', []);
352+
}
353+
$serializer->method('deserialize')->willThrowException(new PartialDenormalizationException([], [$exception]));
354+
355+
$resolver = new RequestPayloadValueResolver($serializer, $this->createMock(ValidatorInterface::class));
356+
$request = Request::create('/', 'POST', server: ['CONTENT_TYPE' => 'application/json'], content: '{"price": 50}');
357+
358+
$arguments = $resolver->resolve($request, new ArgumentMetadata('valid', RequestPayload::class, false, false, null, false, [
359+
MapRequestPayload::class => new MapRequestPayload(),
360+
]));
361+
$event = new ControllerArgumentsEvent($this->createMock(HttpKernelInterface::class), function () {}, $arguments, $request, HttpKernelInterface::MAIN_REQUEST);
362+
363+
$resolver->onKernelControllerArguments($event);
364+
}
365+
335366
public function testQueryStringValidationPassed()
336367
{
337368
$payload = new RequestPayload(50);
@@ -638,6 +669,10 @@ public function __construct(public readonly float $price)
638669
}
639670
}
640671

672+
interface SerializerDenormalizer extends SerializerInterface, DenormalizerInterface
673+
{
674+
}
675+
641676
class User
642677
{
643678
public function __construct(

0 commit comments

Comments
 (0)