-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
Cannot use #[MapUploadedFile]
with optional file
#58590
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
Comments
@jeremyhalin can you dump |
When I type |
The more I test, the more I don't see how |
Can you create a simple repository on your github, with symfony installed and your use case? It will be easier for us to test. |
The two code examples are all I have to share. If you want to test, you can install Symfony demo and just copy/paste my code. |
I'm having a similar problem.
This is the code: public function uploadFile(
#[MapUploadedFile(
[
new Assert\File(mimeTypes: ['image/png', 'image/jpeg', 'image/webp', 'image/jpg']),
new Assert\Image(maxWidth: 3840, maxHeight: 2160),
],
'picture',
)] UploadedFile $picture // <- $picture is an empty array
): JsonResponse {
return $this->json([]);
} This is my request: curl --request PUT \
--url http://localhost:8000/api/file/ \
--header 'Content-Type: multipart/form-data' \
--header 'User-Agent: insomnia/10.0.0' \
--form picture=@/run/user/1000/doc/f67e3a20/3868846673715350409_n.jpg To reproduce this issue you can go to my repository and run the application: cd public
symfony local:server:start --no-tls |
Hello @cizordj It seems you are having the problem even if you pass the file, which is different from my case when I try to call the route without passing a file with an optional |
Seems related to #54871 |
This part of the RequestPayloadValueResolver (line 235) private function mapUploadedFile(Request $request, ArgumentMetadata $argument, MapUploadedFile $attribute): UploadedFile|array|null
{
return $request->files->get($attribute->name ?? $argument->getName(), []);
} assumes that files are present, even though the ArgumentMetaData is right there to check if we have set the parameter as null. Not sure though if this kind of logic should be implemented here. On the other hand, the empty array that is given as parameter to |
Is |
Symfony version(s) affected
7.1.x
Description
I'm trying to use
#[MapUploadedFile]
for a POST route withmultipart/form-data
.When I pass a file it's working as expected, but when I try to make it optional, the test fails because it receives an array while waiting for
null | UploadedFile
.Is it expected? Maybe I'm just wrongly using the browser client to make a request without files?
How to reproduce
Controller
Test
Error
Possible Solution
No response
Additional Context
No response
The text was updated successfully, but these errors were encountered: