Skip to content

#[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

Closed
rcsofttech85 opened this issue May 9, 2024 · 6 comments
Closed

Comments

@rcsofttech85
Copy link

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

    public function changeUserPicture(
        #[MapUploadedFile(
            constraints: new Assert\File(mimeTypes: ['image/png', 'image/jpeg']),

        )] ?UploadedFile $picture
    ): Response
    {
        //
    }

Possible Solution

public function changeUserPicture(
     #[MapUploadedFile(
         constraints:
         new Assert\File(mimeTypes: ['image/png', 'image/jpeg']),
         name:
         'file'
     )] ?UploadedFile $picture
 ): Response
 {
    //
 }

Additional Context

Screenshot from 2024-05-09 18-38-44

@xabbuh
Copy link
Member

xabbuh commented May 9, 2024

Can you create a small example application that allows to reproduce your issue?

@rcsofttech85
Copy link
Author

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>

@asispts
Copy link
Contributor

asispts commented May 10, 2024

What @xabbuh means is that you create a new symfony project to reproduce the issue and then post the repo url here.

@rcsofttech85
Copy link
Author

rcsofttech85 commented May 10, 2024

https://github.com/rcsofttech85/symfony-test

symfony server:start
http://localhost:8000/home

you should see form upload option.

@renedelima
Copy link
Contributor

    #[Route(path:'/user/picture',name:'upload_route', methods: ['POST'])]
    public function changeUserPicture(
        #[MapUploadedFile(
        )] ?UploadedFile $picture
    ): Response
    {
       // type error
    }
<form action="{{ path('upload_route') }}" method="post" enctype="multipart/form-data">
    <input type="file" name="file" id="file">
    <button type="submit">Upload</button>
</form>

Thanks for sharing it @rcsofttech85.

When you omit the attribute parameter name, the value resolver will try to fetch the uploaded file based on the variable name, which is $picture in this case. So the form field should be changed to <input type="file" name="picture" id="file"> to match with the controller argument.

Alternatively, you can also change the argument to #[MapUploadedFile] ?UploadedFile $file to match with with the form field.

@DarthLegiON
Copy link

DarthLegiON commented Jun 8, 2024

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants