Skip to content
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

[HttpKernel] Introduce #[MapUploadedFile] controller argument attribute #49978

Merged
merged 1 commit into from
Apr 18, 2024

Conversation

renedelima
Copy link
Contributor

@renedelima renedelima commented Apr 8, 2023

Q A
Branch? 7.1
Bug fix? no
New feature? yes
Deprecations? no
Tickets #52678, #49138
License MIT
Doc PR -

Usage Example

# src/Controller/UserPictureController.php

namespace App\Controller;

use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Attribute\AsController;
use Symfony\Component\HttpKernel\Attribute\MapUploadedFile;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Validator\Constraints as Assert;

#[AsController]
final class UserPictureController
{
    #[Route('/user/picture', methods: ['PUT'])]
    public function __invoke(
        #[MapUploadedFile(
            new Assert\File(mimeTypes: ['image/png', 'image/jpeg']),
        )] ?UploadedFile $picture,
    ): Response {
        return new Response('Your picture was updated');
    }
}
# src/Controller/UserDocumentsController.php

namespace App\Controller;

use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Attribute\AsController;
use Symfony\Component\HttpKernel\Attribute\MapUploadedFile;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Validator\Constraints as Assert;

final class UserDocumentsController
{
    #[Route('/user/documents', methods: ['PUT'])]
    public function __invoke(
        #[MapUploadedFile(
            new Assert\File(mimeTypes: ['application/pdf'])
        )] array $documents
    ): Response {
        return new Response('Thanks for sharing your documents');
    }
}
# src/Controller/UserDocumentsController.php

namespace App\Controller;

use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Attribute\AsController;
use Symfony\Component\HttpKernel\Attribute\MapUploadedFile;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Validator\Constraints as Assert;

final class UserDocumentsController
{
    #[Route('/user/documents', methods: ['PUT'])]
    public function __invoke(
        #[MapUploadedFile(
            new Assert\File(mimeTypes: ['application/pdf'])
        )] UploadedFile ...$documents
    ): Response {
        return new Response('Thanks for sharing your documents');
    }
}

@carsonbot
Copy link

Hey!

I see that this is your first PR. That is great! Welcome!

Symfony has a contribution guide which I suggest you to read.

In short:

  • Always add tests
  • Keep backward compatibility (see https://symfony.com/bc).
  • Bug fixes must be submitted against the lowest maintained branch where they apply (see https://symfony.com/releases)
  • Features and deprecations must be submitted against the 6.3 branch.

Review the GitHub status checks of your pull request and try to solve the reported issues. If some tests are failing, try to see if they are failing because of this change.

When two Symfony core team members approve this change, it will be merged and you will become an official Symfony contributor!
If this PR is merged in a lower version branch, it will be merged up to all maintained branches within a few days.

I am going to sit back now and wait for the reviews.

Cheers!

Carsonbot

@renedelima renedelima force-pushed the map-uploaded-file branch 3 times, most recently from 66e8f09 to f366d35 Compare April 8, 2023 19:08
@Koc
Copy link
Contributor

Koc commented Apr 11, 2023

Wow, I like idea of reusing infrastructure provided by RequestPayloadValueResolver 👏

Can you add support of the variadic arguments? + squash your commits for easier review until my PR hasn't merged.

@renedelima
Copy link
Contributor Author

Can you add support of the variadic arguments? + squash your commits for easier review until my PR hasn't merged.

I Just added support to variadic arguments and the commits were squashed. @Koc

A missing file is like a missing form field in the payload. The code should be 422 too.

Status code changed to 422. @GromNaN

@Koc
Copy link
Contributor

Koc commented Apr 14, 2023

@renedelima you can rebase now 🎉

@renedelima renedelima force-pushed the map-uploaded-file branch 8 times, most recently from 75aad23 to 9eb9b53 Compare April 18, 2023 16:15
@renedelima
Copy link
Contributor Author

@renedelima you can rebase now 🎉

Rebased. For now, it seems there are no more suggestions to consider. Let's see what people have to say.

Copy link
Contributor

@renanbr renanbr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It could also be great to have unit tests in RequestPayloadValueResolverTest.

@renedelima renedelima force-pushed the map-uploaded-file branch 2 times, most recently from 6312aee to 0a29c3a Compare April 20, 2023 17:32
@renedelima renedelima force-pushed the map-uploaded-file branch 2 times, most recently from b3a06fb to 9701c13 Compare March 24, 2024 18:45
@nicolas-grekas
Copy link
Member

Anyone @symfony/mergers?
(small rebase needed BTW)

Copy link
Member

@yceruto yceruto left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

@OskarStark
Copy link
Contributor

It looks like your commiter email is not associated with your GitHub account, maybe you want to change that?

@renedelima renedelima force-pushed the map-uploaded-file branch 4 times, most recently from e96d7ee to e3f43a0 Compare April 12, 2024 14:48
@OskarStark OskarStark changed the title [HttpKernel] Introduce MapUploadedFile controller argument attribute [HttpKernel] Introduce #[MapUploadedFile] controller argument attribute Apr 13, 2024
Signed-off-by: Rene Lima <renedelima@gmail.com>
@chalasr
Copy link
Member

chalasr commented Apr 18, 2024

1 year later. Thanks for your work and your endurance @renedelima!

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

Successfully merging this pull request may close these issues.

None yet

9 participants