Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 78f82b9

Browse filesBrowse files
bug #52131 [HttpKernel] Fix RequestPayloadValueResolver handling error with no ExpectedTypes (Jeroeny)
This PR was merged into the 6.3 branch. Discussion ---------- [HttpKernel] Fix `RequestPayloadValueResolver` handling error with no ExpectedTypes | Q | A | ------------- | --- | Branch? | 6.3 | Bug fix? | yes | New feature? | no | Deprecations? | no | License | MIT The `NotNormalizableValueException` errors in `PartialDenormalizationException->getErrors()` have a `->getExpectedTypes()` method that returns `array|null`. The `RequestPayloadValueResolver` assumes this is always an array. When the returned value is `null`, an Exception is thrown with `implode(): Argument #1 ($pieces) must be of type array, string given`. Also the exception message in case of an empty array was: `This value should be of type .`. #SymfonyHackday Commits ------- 0af85fe Fix RequestPayloadValueResolver handling error with no ExpectedTypes
2 parents abdb713 + 0af85fe commit 78f82b9
Copy full SHA for 78f82b9

File tree

2 files changed

+41
-2
lines changed
Filter options

2 files changed

+41
-2
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpKernel/Controller/ArgumentResolver/RequestPayloadValueResolver.php
+6-2Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpKernel/Tests/Controller/ArgumentResolver/RequestPayloadValueResolverTest.php
+35Lines changed: 35 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\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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.