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

[HttpKernel] Add option to map empty data with MapQueryString and MapRequestPayload #52134

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

Open
wants to merge 1 commit into
base: 7.3
Choose a base branch
Loading
from
Open
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 @@ -31,6 +31,7 @@ public function __construct(
public readonly string|GroupSequence|array|null $validationGroups = null,
string $resolver = RequestPayloadValueResolver::class,
public readonly int $validationFailedStatusCode = Response::HTTP_NOT_FOUND,
public bool $mapEmpty = false,
) {
parent::__construct($resolver);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public function __construct(
public readonly string|GroupSequence|array|null $validationGroups = null,
string $resolver = RequestPayloadValueResolver::class,
public readonly int $validationFailedStatusCode = Response::HTTP_UNPROCESSABLE_ENTITY,
public bool $mapEmpty = false,
) {
parent::__construct($resolver);
}
Expand Down
1 change: 1 addition & 0 deletions 1 src/Symfony/Component/HttpKernel/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ CHANGELOG
* Deprecate `FileLinkFormatter`, use `FileLinkFormatter` from the ErrorHandler component instead
* Add argument `$buildDir` to `WarmableInterface`
* Add argument `$filter` to `Profiler::find()` and `FileProfilerStorage::find()`
* Add argument `$mapEmpty` to `MapQueryString` and `MapRequestPayload` for always attempting denormalization with empty query and request payload

6.3
---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ public static function getSubscribedEvents(): array

private function mapQueryString(Request $request, string $type, MapQueryString $attribute): ?object
{
if (!$data = $request->query->all()) {
if ((!$data = $request->query->all()) && !$attribute->mapEmpty) {
return null;
}

Expand All @@ -174,11 +174,11 @@ private function mapRequestPayload(Request $request, string $type, MapRequestPay
throw new HttpException(Response::HTTP_UNSUPPORTED_MEDIA_TYPE, sprintf('Unsupported format, expects "%s", but "%s" given.', implode('", "', (array) $attribute->acceptFormat), $format));
}

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

if ('' === $data = $request->getContent()) {
if ('' === $content ??= $request->getContent()) {
return null;
}

Expand All @@ -187,7 +187,7 @@ private function mapRequestPayload(Request $request, string $type, MapRequestPay
}

try {
return $this->serializer->deserialize($data, $type, $format, self::CONTEXT_DESERIALIZE + $attribute->serializationContext);
return $this->serializer->deserialize($content, $type, $format, self::CONTEXT_DESERIALIZE + $attribute->serializationContext);
} catch (UnsupportedFormatException $e) {
throw new HttpException(Response::HTTP_UNSUPPORTED_MEDIA_TYPE, sprintf('Unsupported format: "%s".', $format), $e);
} catch (NotEncodableValueException $e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Encoder\XmlEncoder;
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\Validator\Constraints as Assert;
Expand Down Expand Up @@ -149,6 +150,46 @@ public function testQueryNullableValueArgument()
$this->assertSame([null], $event->getArguments());
}

public function testMapQueryStringEmpty()
{
$payload = new RequestPayload(50);
$denormalizer = new RequestPayloadDenormalizer($payload);
$serializer = new Serializer([$denormalizer]);
$resolver = new RequestPayloadValueResolver($serializer);
$argument = new ArgumentMetadata('valid', RequestPayload::class, false, false, null, false, [
MapQueryString::class => new MapQueryString(mapEmpty: true),
]);
$request = Request::create('/', 'GET');

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

$resolver->onKernelControllerArguments($event);

$this->assertSame([$payload], $event->getArguments());
}

public function testMapRequestPayloadEmpty()
{
$payload = new RequestPayload(50);
$denormalizer = new RequestPayloadDenormalizer($payload);
$serializer = new Serializer([$denormalizer]);
$resolver = new RequestPayloadValueResolver($serializer);
$argument = new ArgumentMetadata('valid', RequestPayload::class, false, false, null, false, [
MapRequestPayload::class => new MapRequestPayload(mapEmpty: true),
]);
$request = Request::create('/', 'POST');

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

$resolver->onKernelControllerArguments($event);

$this->assertSame([$payload], $event->getArguments());
}

public function testNullPayloadAndNotDefaultOrNullableArgument()
{
$validator = $this->createMock(ValidatorInterface::class);
Expand Down Expand Up @@ -687,3 +728,25 @@ public function __construct(public readonly float $page)
{
}
}

class RequestPayloadDenormalizer implements DenormalizerInterface
{
public function __construct(private RequestPayload $payload)
{
}

public function denormalize(mixed $data, string $type, string $format = null, array $context = []): mixed
{
return $this->payload;
}

public function supportsDenormalization(mixed $data, string $type, string $format = null, array $context = []): bool
{
return RequestPayload::class === $type;
}

public function getSupportedTypes(string $format = null): array
{
return [RequestPayload::class => true];
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.