Skip to content

[HttpKernel] Fix RequestPayloadValueResolver handling error with no ExpectedTypes #52131

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,15 @@ public function onKernelControllerArguments(ControllerArgumentsEvent $event): vo
} catch (PartialDenormalizationException $e) {
$trans = $this->translator ? $this->translator->trans(...) : fn ($m, $p) => strtr($m, $p);
foreach ($e->getErrors() as $error) {
$parameters = ['{{ type }}' => implode('|', $error->getExpectedTypes())];
$parameters = [];
$template = 'This value was of an unexpected type.';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that this message is not in translation maps. Dunno if it matters. Does it?

if ($expectedTypes = $error->getExpectedTypes()) {
$template = 'This value should be of type {{ type }}.';
$parameters['{{ type }}'] = implode('|', $expectedTypes);
}
if ($error->canUseMessageForUser()) {
$parameters['hint'] = $error->getMessage();
}
$template = 'This value should be of type {{ type }}.';
$message = $trans($template, $parameters, 'validators');
$violations->add(new ConstraintViolation($message, $template, $parameters, null, $error->getPath(), null));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,12 @@
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Encoder\XmlEncoder;
use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
use Symfony\Component\Serializer\Exception\PartialDenormalizationException;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\Serializer;
use Symfony\Component\Serializer\SerializerInterface;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\ConstraintViolationList;
use Symfony\Component\Validator\Exception\ValidationFailedException;
Expand Down Expand Up @@ -332,6 +335,34 @@ public function testRequestContentValidationPassed()
$this->assertEquals([$payload], $event->getArguments());
}

/**
* @testWith [null]
* [[]]
*/
public function testRequestContentWithUntypedErrors(?array $types)
{
$this->expectException(HttpException::class);
$this->expectExceptionMessage('This value was of an unexpected type.');
$serializer = $this->createMock(SerializerDenormalizer::class);

if (null === $types) {
$exception = new NotNormalizableValueException('Error with no types');
} else {
$exception = NotNormalizableValueException::createForUnexpectedDataType('Error with no types', '', []);
}
$serializer->method('deserialize')->willThrowException(new PartialDenormalizationException([], [$exception]));

$resolver = new RequestPayloadValueResolver($serializer, $this->createMock(ValidatorInterface::class));
$request = Request::create('/', 'POST', server: ['CONTENT_TYPE' => 'application/json'], content: '{"price": 50}');

$arguments = $resolver->resolve($request, new ArgumentMetadata('valid', RequestPayload::class, false, false, null, false, [
MapRequestPayload::class => new MapRequestPayload(),
]));
$event = new ControllerArgumentsEvent($this->createMock(HttpKernelInterface::class), function () {}, $arguments, $request, HttpKernelInterface::MAIN_REQUEST);

$resolver->onKernelControllerArguments($event);
}

public function testQueryStringValidationPassed()
{
$payload = new RequestPayload(50);
Expand Down Expand Up @@ -638,6 +669,10 @@ public function __construct(public readonly float $price)
}
}

interface SerializerDenormalizer extends SerializerInterface, DenormalizerInterface
{
}

class User
{
public function __construct(
Expand Down