Skip to content

Commit dd887fa

Browse files
bug #62449 [HttpKernel] Fix using MapRequestPayload on nullable arguments (nicolas-grekas)
This PR was merged into the 6.4 branch. Discussion ---------- [HttpKernel] Fix using MapRequestPayload on nullable arguments | Q | A | ------------- | --- | Branch? | 6.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | - | License | MIT #50029 broke the capacity to use `#[MapRequestPayload]` on a nullable argument of an action accepting for GET/POST verbs. Commits ------- c73142a [HttpKernel] Fix using MapRequestPayload on nullable arguments
2 parents f3cb286 + c73142a commit dd887fa

File tree

2 files changed

+27
-5
lines changed

2 files changed

+27
-5
lines changed

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,10 @@ private function mapQueryString(Request $request, string $type, MapQueryString $
168168

169169
private function mapRequestPayload(Request $request, string $type, MapRequestPayload $attribute): ?object
170170
{
171+
if ('' === $data = $request->request->all() ?: $request->getContent()) {
172+
return null;
173+
}
174+
171175
if (null === $format = $request->getContentTypeFormat()) {
172176
throw new HttpException(Response::HTTP_UNSUPPORTED_MEDIA_TYPE, 'Unsupported format.');
173177
}
@@ -176,14 +180,10 @@ private function mapRequestPayload(Request $request, string $type, MapRequestPay
176180
throw new HttpException(Response::HTTP_UNSUPPORTED_MEDIA_TYPE, \sprintf('Unsupported format, expects "%s", but "%s" given.', implode('", "', (array) $attribute->acceptFormat), $format));
177181
}
178182

179-
if ($data = $request->request->all()) {
183+
if (\is_array($data)) {
180184
return $this->serializer->denormalize($data, $type, 'csv', $attribute->serializationContext + self::CONTEXT_DENORMALIZE);
181185
}
182186

183-
if ('' === $data = $request->getContent()) {
184-
return null;
185-
}
186-
187187
if ('form' === $format) {
188188
throw new HttpException(Response::HTTP_BAD_REQUEST, 'Request payload contains invalid "form" data.');
189189
}

src/Symfony/Component/HttpKernel/Tests/Controller/ArgumentResolver/RequestPayloadValueResolverTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,28 @@ public function testNullPayloadAndNotDefaultOrNullableArgument()
179179
}
180180
}
181181

182+
public function testRequestPayloadWithoutContentTypeOnNullableArgumentReturnsNull()
183+
{
184+
$validator = $this->createMock(ValidatorInterface::class);
185+
$validator->expects($this->never())
186+
->method('validate');
187+
188+
$resolver = new RequestPayloadValueResolver(new Serializer(), $validator);
189+
190+
$argument = new ArgumentMetadata('valid', RequestPayload::class, false, false, null, true, [
191+
MapRequestPayload::class => new MapRequestPayload(),
192+
]);
193+
$request = Request::create('/', 'POST');
194+
195+
$kernel = $this->createMock(HttpKernelInterface::class);
196+
$arguments = $resolver->resolve($request, $argument);
197+
$event = new ControllerArgumentsEvent($kernel, fn () => null, $arguments, $request, HttpKernelInterface::MAIN_REQUEST);
198+
199+
$resolver->onKernelControllerArguments($event);
200+
201+
$this->assertSame([null], $event->getArguments());
202+
}
203+
182204
public function testQueryNullPayloadAndNotDefaultOrNullableArgument()
183205
{
184206
$validator = $this->createMock(ValidatorInterface::class);

0 commit comments

Comments
 (0)