-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
#[MapUploadedFile] Missing or Invalid 'name' Parameter Causes Type Error #54871
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
Can you create a small example application that allows to reproduce your issue? |
I had already mentioned in the description how to reproduce, but let me explain it again. If I understand the feature correctly, it converts the uploaded file to Symfony\Component\HttpFoundation\File\UploadedFile internally. However, the 'name' parameter is currently optional when it should be mandatory. If you omit the 'name' parameter, it will return an empty array instead of an UploadedFile, resulting in a type error [500]. #[Route(path:'/user/picture',name:'upload_route', methods: ['POST'])]
public function changeUserPicture(
#[MapUploadedFile(
)] ?UploadedFile $picture
): Response
{
// type error
} #[Route(path:'/user/picture',name:'upload_route', methods: ['POST'])]
public function changeUserPicture(
#[MapUploadedFile(
name: 'file'
)] ?UploadedFile $picture
): Response
{
dd($picture); // it works
} <form action="{{ path('upload_route') }}" method="post" enctype="multipart/form-data">
<input type="file" name="file" id="file">
<button type="submit">Upload</button>
</form> |
What @xabbuh means is that you create a new symfony project to reproduce the issue and then post the repo url here. |
https://github.com/rcsofttech85/symfony-test symfony server:start you should see form upload option. |
Thanks for sharing it @rcsofttech85. When you omit the attribute parameter Alternatively, you can also change the argument to |
@renedelima hi! Am I right, that if the form (or curl, or, in my case, tests) do not have 'picture' file inside the request, we will get 500 error in the result because the resolver returns an empty array by default? I assume getting 500 as a result of malformed request isn't right. 400 or 422 would be much better. |
Symfony version(s) affected
7.1
Description
In the changeUserPicture() function, if the 'name' parameter in the MapUploadedFile attribute is missing or invalid, it results in a type error being thrown. This behavior could lead to unexpected exceptions and is not clearly handled.
How to reproduce
Possible Solution
Additional Context
The text was updated successfully, but these errors were encountered: