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

Commit 6ac6f2f

Browse filesBrowse files
committed
Add argument $mapEmpty to MapQueryString and MapRequestPayload for always attempting denormalization with empty query and request payload
1 parent dc23c8e commit 6ac6f2f
Copy full SHA for 6ac6f2f

File tree

5 files changed

+70
-4
lines changed
Filter options

5 files changed

+70
-4
lines changed

‎src/Symfony/Component/HttpKernel/Attribute/MapQueryString.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpKernel/Attribute/MapQueryString.php
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public function __construct(
3131
public readonly string|GroupSequence|array|null $validationGroups = null,
3232
string $resolver = RequestPayloadValueResolver::class,
3333
public readonly int $validationFailedStatusCode = Response::HTTP_NOT_FOUND,
34+
public bool $mapEmpty = false,
3435
) {
3536
parent::__construct($resolver);
3637
}

‎src/Symfony/Component/HttpKernel/Attribute/MapRequestPayload.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpKernel/Attribute/MapRequestPayload.php
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public function __construct(
3232
public readonly string|GroupSequence|array|null $validationGroups = null,
3333
string $resolver = RequestPayloadValueResolver::class,
3434
public readonly int $validationFailedStatusCode = Response::HTTP_UNPROCESSABLE_ENTITY,
35+
public bool $mapEmpty = false,
3536
) {
3637
parent::__construct($resolver);
3738
}

‎src/Symfony/Component/HttpKernel/CHANGELOG.md

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpKernel/CHANGELOG.md
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ CHANGELOG
1717
* Deprecate `FileLinkFormatter`, use `FileLinkFormatter` from the ErrorHandler component instead
1818
* Add argument `$buildDir` to `WarmableInterface`
1919
* Add argument `$filter` to `Profiler::find()` and `FileProfilerStorage::find()`
20+
* Add argument `$mapEmpty` to `MapQueryString` and `MapRequestPayload` for always attempting denormalization with empty query and request payload
2021

2122
6.3
2223
---

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpKernel/Controller/ArgumentResolver/RequestPayloadValueResolver.php
+4-4Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ public static function getSubscribedEvents(): array
157157

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

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

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

181-
if ('' === $data = $request->getContent()) {
181+
if ('' === $content ??= $request->getContent()) {
182182
return null;
183183
}
184184

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

189189
try {
190-
return $this->serializer->deserialize($data, $type, $format, self::CONTEXT_DESERIALIZE + $attribute->serializationContext);
190+
return $this->serializer->deserialize($content, $type, $format, self::CONTEXT_DESERIALIZE + $attribute->serializationContext);
191191
} catch (UnsupportedFormatException $e) {
192192
throw new HttpException(Response::HTTP_UNSUPPORTED_MEDIA_TYPE, sprintf('Unsupported format: "%s".', $format), $e);
193193
} catch (NotEncodableValueException $e) {

‎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
+63Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
use Symfony\Component\Serializer\Encoder\JsonEncoder;
2525
use Symfony\Component\Serializer\Encoder\XmlEncoder;
2626
use Symfony\Component\Serializer\Exception\PartialDenormalizationException;
27+
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
2728
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
2829
use Symfony\Component\Serializer\Serializer;
2930
use Symfony\Component\Validator\Constraints as Assert;
@@ -149,6 +150,46 @@ public function testQueryNullableValueArgument()
149150
$this->assertSame([null], $event->getArguments());
150151
}
151152

153+
public function testMapQueryStringEmpty()
154+
{
155+
$payload = new RequestPayload(50);
156+
$denormalizer = new RequestPayloadDenormalizer($payload);
157+
$serializer = new Serializer([$denormalizer]);
158+
$resolver = new RequestPayloadValueResolver($serializer);
159+
$argument = new ArgumentMetadata('valid', RequestPayload::class, false, false, null, false, [
160+
MapQueryString::class => new MapQueryString(mapEmpty: true),
161+
]);
162+
$request = Request::create('/', 'GET');
163+
164+
$kernel = $this->createMock(HttpKernelInterface::class);
165+
$arguments = $resolver->resolve($request, $argument);
166+
$event = new ControllerArgumentsEvent($kernel, fn () => null, $arguments, $request, HttpKernelInterface::MAIN_REQUEST);
167+
168+
$resolver->onKernelControllerArguments($event);
169+
170+
$this->assertSame([$payload], $event->getArguments());
171+
}
172+
173+
public function testMapRequestPayloadEmpty()
174+
{
175+
$payload = new RequestPayload(50);
176+
$denormalizer = new RequestPayloadDenormalizer($payload);
177+
$serializer = new Serializer([$denormalizer]);
178+
$resolver = new RequestPayloadValueResolver($serializer);
179+
$argument = new ArgumentMetadata('valid', RequestPayload::class, false, false, null, false, [
180+
MapRequestPayload::class => new MapRequestPayload(mapEmpty: true),
181+
]);
182+
$request = Request::create('/', 'POST');
183+
184+
$kernel = $this->createMock(HttpKernelInterface::class);
185+
$arguments = $resolver->resolve($request, $argument);
186+
$event = new ControllerArgumentsEvent($kernel, fn () => null, $arguments, $request, HttpKernelInterface::MAIN_REQUEST);
187+
188+
$resolver->onKernelControllerArguments($event);
189+
190+
$this->assertSame([$payload], $event->getArguments());
191+
}
192+
152193
public function testNullPayloadAndNotDefaultOrNullableArgument()
153194
{
154195
$validator = $this->createMock(ValidatorInterface::class);
@@ -687,3 +728,25 @@ public function __construct(public readonly float $page)
687728
{
688729
}
689730
}
731+
732+
class RequestPayloadDenormalizer implements DenormalizerInterface {
733+
734+
public function __construct(private RequestPayload $payload)
735+
{
736+
}
737+
738+
public function denormalize(mixed $data, string $type, string $format = null, array $context = []): mixed
739+
{
740+
return $this->payload;
741+
}
742+
743+
public function supportsDenormalization(mixed $data, string $type, string $format = null, array $context = []): bool
744+
{
745+
return $type === RequestPayload::class;
746+
}
747+
748+
public function getSupportedTypes(?string $format = null): array
749+
{
750+
return [RequestPayload::class => true];
751+
}
752+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.