Skip to content

Navigation Menu

Sign in
Appearance settings

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

[HttpKernel] Denormalize request data using the csv format when using "#[MapQueryString]" or "#[MapRequestPayload]" (except for content data) #59134

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
Dec 9, 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 @@ -40,11 +40,9 @@
class RequestPayloadValueResolver implements ValueResolverInterface, EventSubscriberInterface
{
/**
* @see \Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer::DISABLE_TYPE_ENFORCEMENT
* @see DenormalizerInterface::COLLECT_DENORMALIZATION_ERRORS
*/
private const CONTEXT_DENORMALIZE = [
'disable_type_enforcement' => true,
'collect_denormalization_errors' => true,
];

Expand Down Expand Up @@ -161,7 +159,7 @@ private function mapQueryString(Request $request, string $type, MapQueryString $
return null;
}

return $this->serializer->denormalize($data, $type, null, $attribute->serializationContext + self::CONTEXT_DENORMALIZE);
return $this->serializer->denormalize($data, $type, 'csv', $attribute->serializationContext + self::CONTEXT_DENORMALIZE);
}

private function mapRequestPayload(Request $request, string $type, MapRequestPayload $attribute): ?object
Expand All @@ -175,7 +173,7 @@ private function mapRequestPayload(Request $request, string $type, MapRequestPay
}

if ($data = $request->request->all()) {
return $this->serializer->denormalize($data, $type, null, $attribute->serializationContext + self::CONTEXT_DENORMALIZE);
return $this->serializer->denormalize($data, $type, 'csv', $attribute->serializationContext + self::CONTEXT_DENORMALIZE);
}

if ('' === $data = $request->getContent()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\PropertyAccess\Exception\InvalidTypeException;
use Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Encoder\XmlEncoder;
use Symfony\Component\Serializer\Exception\PartialDenormalizationException;
Expand Down Expand Up @@ -363,6 +364,38 @@ public function testQueryStringValidationPassed()
$this->assertEquals([$payload], $event->getArguments());
}

public function testQueryStringParameterTypeMismatch()
{
$query = ['price' => 'not a float'];

$normalizer = new ObjectNormalizer(null, null, null, new ReflectionExtractor());
$serializer = new Serializer([$normalizer], ['json' => new JsonEncoder()]);

$validator = $this->createMock(ValidatorInterface::class);
$validator->expects($this->never())->method('validate');

$resolver = new RequestPayloadValueResolver($serializer, $validator);

$argument = new ArgumentMetadata('invalid', RequestPayload::class, false, false, null, false, [
MapQueryString::class => new MapQueryString(),
]);

$request = Request::create('/', 'GET', $query);

$kernel = $this->createMock(HttpKernelInterface::class);
$arguments = $resolver->resolve($request, $argument);
$event = new ControllerArgumentsEvent($kernel, function () {}, $arguments, $request, HttpKernelInterface::MAIN_REQUEST);

try {
$resolver->onKernelControllerArguments($event);
$this->fail(sprintf('Expected "%s" to be thrown.', HttpException::class));
} catch (HttpException $e) {
$validationFailedException = $e->getPrevious();
$this->assertInstanceOf(ValidationFailedException::class, $validationFailedException);
$this->assertSame('This value should be of type float.', $validationFailedException->getViolations()[0]->getMessage());
}
}

public function testRequestInputValidationPassed()
{
$input = ['price' => '50'];
Expand Down Expand Up @@ -391,6 +424,38 @@ public function testRequestInputValidationPassed()
$this->assertEquals([$payload], $event->getArguments());
}

public function testRequestInputTypeMismatch()
{
$input = ['price' => 'not a float'];

$normalizer = new ObjectNormalizer(null, null, null, new ReflectionExtractor());
$serializer = new Serializer([$normalizer], ['json' => new JsonEncoder()]);

$validator = $this->createMock(ValidatorInterface::class);
$validator->expects($this->never())->method('validate');

$resolver = new RequestPayloadValueResolver($serializer, $validator);

$argument = new ArgumentMetadata('invalid', RequestPayload::class, false, false, null, false, [
MapRequestPayload::class => new MapRequestPayload(),
]);

$request = Request::create('/', 'POST', $input);

$kernel = $this->createMock(HttpKernelInterface::class);
$arguments = $resolver->resolve($request, $argument);
$event = new ControllerArgumentsEvent($kernel, function () {}, $arguments, $request, HttpKernelInterface::MAIN_REQUEST);

try {
$resolver->onKernelControllerArguments($event);
$this->fail(sprintf('Expected "%s" to be thrown.', HttpException::class));
} catch (HttpException $e) {
$validationFailedException = $e->getPrevious();
$this->assertInstanceOf(ValidationFailedException::class, $validationFailedException);
$this->assertSame('This value should be of type float.', $validationFailedException->getViolations()[0]->getMessage());
}
}

public function testItThrowsOnVariadicArgument()
{
$serializer = new Serializer();
Expand Down
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.