-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
Description
Symfony version(s) affected
7.2 and 7.3 (possibly also 7.1)
Description
According to the Symfony documentation, the associated argument resolver of #[MapUploadedFile]
should throw an HttpException
when no file is submitted and the argument is neither nullable nor has a default value. Currently, however, an empty array is returned which causes a PHP error when the action argument is of type UploadedFile
.
How to reproduce
Create a controller action which accepts an uploaded file:
#[Route('/upload', name: 'file_upload', methods: ['POST'])]
public function upload(
#[MapUploadedFile] UploadedFile $file
): JsonResponse {
return new JsonResponse(['status' => 'ok']);
}
then upload a file with a different name:
curl -i -F 'different_name=@/path/to/file' http://localhost/upload
Symfony then produces the error App\Controller\TestController::upload(): Argument #1 ($file) must be of type Symfony\Component\HttpFoundation\File\UploadedFile, array given, called in /app/vendor/symfony/http-kernel/HttpKernel.php on line 183
and handles it accordingly.
Possible Solution
Instead of an empty array, RequestPayloadValueResolver::mapUploadedFile
should return null
when no file is submitted and the argument is neither nullable nor has a default value, and subsequently an HttpException
gets thrown in RequestPayloadValueResolver::onKernelControllerArguments
.
diff --git a/src/Symfony/Component/HttpKernel/Controller/ArgumentResolver/RequestPayloadValueResolver.php b/src/Symfony/Component/HttpKernel/Controller/ArgumentResolver/RequestPayloadValueResolver.php
index 0ca965db4b..11a16b4de5 100644
--- a/src/Symfony/Component/HttpKernel/Controller/ArgumentResolver/RequestPayloadValueResolver.php
+++ b/src/Symfony/Component/HttpKernel/Controller/ArgumentResolver/RequestPayloadValueResolver.php
@@ -232,7 +232,7 @@ class RequestPayloadValueResolver implements ValueResolverInterface, EventSubscr
private function mapUploadedFile(Request $request, ArgumentMetadata $argument, MapUploadedFile $attribute): UploadedFile|array|null
{
- if (!($files = $request->files->get($attribute->name ?? $argument->getName(), [])) && ($argument->isNullable() || $argument->hasDefaultValue())) {
+ if (!($files = $request->files->get($attribute->name ?? $argument->getName())) && !($argument->isNullable() || $argument->hasDefaultValue())) {
return null;
}
Additional Context
No response