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

Skip to content

Commit b668cd6

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

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ 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 = ['{{ type }}' => implode('|', $error->getExpectedTypes() ?? [])];
112112
if ($error->canUseMessageForUser()) {
113113
$parameters['hint'] = $error->getMessage();
114114
}

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

Lines changed: 25 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,25 @@ public function testRequestContentValidationPassed()
307310
$this->assertEquals([$payload], $event->getArguments());
308311
}
309312

313+
public function testRequestContentWithUntypedErrors()
314+
{
315+
$this->expectException(HttpException::class);
316+
$serializer = $this->createMock(SerializerDenormalizer::class);
317+
$serializer->method('deserialize')->willThrowException(new PartialDenormalizationException([], [new NotNormalizableValueException('Error with no types')]));
318+
319+
$resolver = new RequestPayloadValueResolver($serializer, $this->createMock(ValidatorInterface::class));
320+
$argument = new ArgumentMetadata('valid', RequestPayload::class, false, false, null, false, [
321+
MapRequestPayload::class => new MapRequestPayload(),
322+
]);
323+
$request = Request::create('/', 'POST', server: ['CONTENT_TYPE' => 'application/json'], content: '{"price": 50}');
324+
325+
$kernel = $this->createMock(HttpKernelInterface::class);
326+
$arguments = $resolver->resolve($request, $argument);
327+
$event = new ControllerArgumentsEvent($kernel, function () {}, $arguments, $request, HttpKernelInterface::MAIN_REQUEST);
328+
329+
$resolver->onKernelControllerArguments($event);
330+
}
331+
310332
public function testQueryStringValidationPassed()
311333
{
312334
$payload = new RequestPayload(50);
@@ -612,3 +634,6 @@ public function __construct(public readonly float $price)
612634
{
613635
}
614636
}
637+
638+
interface SerializerDenormalizer extends SerializerInterface, DenormalizerInterface
639+
{}

0 commit comments

Comments
 (0)