Skip to content

Commit 50e7be2

Browse files
bug #61381 [HttpKernel] #[MapUploadedFile] throws http exception on empty files array if argument not nullable nor has default value (hwawshy)
This PR was squashed before being merged into the 7.3 branch. Discussion ---------- [HttpKernel] #[MapUploadedFile] throws http exception on empty files array if argument not nullable nor has default value | Q | A | ------------- | --- | Branch? | 7.3 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | Fix #61376 | License | MIT Fixes #61376 Commits ------- 67eaf1f3191 [HttpKernel] #[MapUploadedFile] throws http exception on empty files array if argument not nullable nor has default value
2 parents 665a003 + e364bfd commit 50e7be2

File tree

2 files changed

+61
-2
lines changed

2 files changed

+61
-2
lines changed

Controller/ArgumentResolver/RequestPayloadValueResolver.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,10 +232,10 @@ private function mapRequestPayload(Request $request, ArgumentMetadata $argument,
232232

233233
private function mapUploadedFile(Request $request, ArgumentMetadata $argument, MapUploadedFile $attribute): UploadedFile|array|null
234234
{
235-
if (!($files = $request->files->get($attribute->name ?? $argument->getName(), [])) && ($argument->isNullable() || $argument->hasDefaultValue())) {
235+
if (!($files = $request->files->get($attribute->name ?? $argument->getName())) && ($argument->isNullable() || $argument->hasDefaultValue())) {
236236
return null;
237237
}
238238

239-
return $files;
239+
return $files ?? ('array' === $argument->getType() ? [] : null);
240240
}
241241
}

Tests/Controller/ArgumentResolver/UploadedFileValueResolverTest.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,35 @@ static function () {},
8282
$request,
8383
HttpKernelInterface::MAIN_REQUEST
8484
);
85+
86+
$this->expectException(HttpException::class);
87+
88+
$resolver->onKernelControllerArguments($event);
89+
}
90+
91+
/**
92+
* @dataProvider provideContext
93+
*/
94+
public function testEmptyArrayArgument(RequestPayloadValueResolver $resolver, Request $request)
95+
{
96+
$attribute = new MapUploadedFile();
97+
$argument = new ArgumentMetadata(
98+
'qux',
99+
'array',
100+
false,
101+
false,
102+
null,
103+
false,
104+
[$attribute::class => $attribute]
105+
);
106+
$event = new ControllerArgumentsEvent(
107+
$this->createMock(HttpKernelInterface::class),
108+
static function () {},
109+
$resolver->resolve($request, $argument),
110+
$request,
111+
HttpKernelInterface::MAIN_REQUEST
112+
);
113+
85114
$resolver->onKernelControllerArguments($event);
86115
$data = $event->getArguments()[0];
87116

@@ -337,6 +366,36 @@ static function () {},
337366
$this->assertNull($data);
338367
}
339368

369+
/**
370+
* @dataProvider provideContext
371+
*/
372+
public function testShouldAllowEmptyWhenNullableArray(RequestPayloadValueResolver $resolver, Request $request)
373+
{
374+
$attribute = new MapUploadedFile();
375+
$argument = new ArgumentMetadata(
376+
'qux',
377+
'array',
378+
false,
379+
false,
380+
null,
381+
true,
382+
[$attribute::class => $attribute]
383+
);
384+
/** @var HttpKernelInterface&MockObject $httpKernel */
385+
$httpKernel = $this->createMock(HttpKernelInterface::class);
386+
$event = new ControllerArgumentsEvent(
387+
$httpKernel,
388+
static function () {},
389+
$resolver->resolve($request, $argument),
390+
$request,
391+
HttpKernelInterface::MAIN_REQUEST
392+
);
393+
$resolver->onKernelControllerArguments($event);
394+
$data = $event->getArguments()[0];
395+
396+
$this->assertNull($data);
397+
}
398+
340399
/**
341400
* @dataProvider provideContext
342401
*/

0 commit comments

Comments
 (0)