Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[HttpKernel] Fatal Error when using#[MapUploadedFile] with non-array/non-variadic argument#57824

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
melya wants to merge3 commits intosymfony:7.1
base:7.1
Choose a base branch
Loading
frommelya:fix-map-uploaded-file-in-request-payload-resolver2

Conversation

melya
Copy link
Contributor

@melyamelya commentedJul 25, 2024
edited
Loading

QA
Branch?7.1
Bug fix?yes
New feature?no
Deprecations?no
LicenseMIT

Having a simple controller withMapUploadedFile as non-array.
When thefile is omitted in request, symfony throwsValueError because resolver returns an empty array and it doesn't match withUploadedFile $file.

In my opinion, the HttpException should be thrown in this case.
For array argument the behaviour stays the same.

#[AsController]#[Route(path:"/upload", methods: ["post"])]finalclass Upload{publicfunction__invoke(        #[MapUploadedFile()]UploadedFile$file,    ):Response {...    }}

Example with omittedfile -ValueError occurs here

POST /upload HTTP/1.1Content-Type: multipart/form-data; charset=utf-8; boundary=__X_PAW_BOUNDARY__

Error example

App\Controller\Upload::__invoke(): Argument #2 ($file) must be of type Symfony\Component\HttpFoundation\File\UploadedFile, array given, called in /app/vendor/symfony/http-kernel/HttpKernel.php on line 183 (500 Internal Server Error)

stetodd reacted with thumbs up emoji
@melya
Copy link
ContributorAuthor

#[MapUploadedFile] was introduced with#49978.

@renedelima Thanks for this feature 💪 .
Could you please check my fix to ensure I haven't missed any initial ideas?

@melyamelya changed the title[HttpKernel] Fix resolving of MapUploadedFile for non-array/non-variadic arguments[HttpKernel] Fix resolving ofMapUploadedFile for non-array/non-variadic argumentsJul 29, 2024
@melyamelya changed the title[HttpKernel] Fix resolving ofMapUploadedFile for non-array/non-variadic arguments[HttpKernel] Fix resolving of#[MapUploadedFile] for non-array/non-variadic argumentsJul 29, 2024
@melyamelya changed the title[HttpKernel] Fix resolving of#[MapUploadedFile] for non-array/non-variadic arguments[HttpKernel] Fatal Error when using#[MapUploadedFile] as non-array/non-variadic argumentJul 29, 2024
@melyamelya changed the title[HttpKernel] Fatal Error when using#[MapUploadedFile] as non-array/non-variadic argument[HttpKernel] Fatal Error when using#[MapUploadedFile] with non-array/non-variadic argumentJul 29, 2024
Copy link
Member

@nicolas-grekasnicolas-grekas left a comment

Choose a reason for hiding this comment

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

LGTM, I just have minor comments.

if (!(is_array($payload) && array_is_list($payload))) {
throw HttpException::fromStatusCode(422);
}
array_splice($arguments, $i, count($payload), $payload);

Choose a reason for hiding this comment

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

wouldn't array_merge be more appropriate?

Suggested change
array_splice($arguments,$i,count($payload),$payload);
array_splice($arguments,$i,\count($payload),$payload);

Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

I wish it work, here I put more details#54901 (comment)

@@ -68,7 +68,7 @@ public function testEmpty(RequestPayloadValueResolver $resolver, Request $reques
$attribute = new MapUploadedFile();
$argument = new ArgumentMetadata(
'qux',
UploadedFile::class,
"array",

Choose a reason for hiding this comment

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

Suggested change
"array",
'array',

Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't this be spotted by fabbot? 🤔

}
array_splice($arguments, $i, count($payload), $payload);
} else {
$arguments[$i] = $payload;

Choose a reason for hiding this comment

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

What about the situation when the argument isUploadedFile and the$payload is an array of files? I think this would still result in a500?

Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

Interesting 🤔
Is it possible to haveUploadedFile as part of#[MapRequestPayload] $payload? (#[MapQuery] we can exclude, I guess)
Could you please share an example of what you mean, so I can elaborate more on this ?

@@ -170,7 +170,14 @@ public function onKernelControllerArguments(ControllerArgumentsEvent $event): vo
};
}

$arguments[$i] = $payload;
if ($argument->metadata->isVariadic()) {

Choose a reason for hiding this comment

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

Should we check if it is variadic as well as if the type is an array here, similar to the check in themapUploadedFile method?

Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

Basically$arguments here are original arguments of an action.
In example below$arguments = [$request, $payload, $files];

publicfunctionaction(Request$request, #[MapRequestPayload]Payload$payload, #[MapUploadedFile]UploadedFile ...$files]) {...}

And the magic here is to expand variadic$files into several items of$arguments.
I hope PHP won't change mechanics of how variadic works and it will be always only 1 possible variadic argument in function signature 🤞

Also, there is already an ability to use#[MapUploadedFile] as bellow. And it works and covered byUploadedFileValueResolverTest::testMultipleFilesArray test case.

publicfunctionaction(Request$request, #[MapRequestPayload]Payload$payload,/** @var UploadedFile[] */ #[MapUploadedFile]array$files]) {...}

Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

I have tested variant

finalclass TestPayloadRequest{publicUploadedFile$file;}finalclass IndexControllerextends AbstractController{    #[Route(path:"/test-upload", methods:"POST")]publicfunctiontestUpload(        #[MapRequestPayload]TestPayloadRequest$file,        #[MapUploadedFile]UploadedFile ...$files,    ):Response {returnnewResponse();    }}

with request like

POST /test-upload HTTP/1.1Cookie: XDEBUG_SESSION=PHPSTORM;Content-Type: multipart/form-data; charset=utf-8; boundary=__X_PAW_BOUNDARY__--__X_PAW_BOUNDARY__Content-Disposition: form-data; name="files[]"; filename="test.yaml"Content-Type: application/x-yamlinfo: test--__X_PAW_BOUNDARY__Content-Disposition: form-data; name="file"; filename="test.yaml"Content-Type: application/x-yamlinfo: test--__X_PAW_BOUNDARY__Content-Disposition: form-data; name="files[]"; filename="test.yaml"Content-Type: application/x-yamlinfo: test

Shortly: current implementation is not designed to work like this, because data for#[MapRequestPayload]
is fetched like this :

https://github.com/melya/symfony/blob/fix-map-uploaded-file-in-request-payload-resolver2/src/Symfony/Component/HttpKernel/Controller/ArgumentResolver/RequestPayloadValueResolver.php?plain=1#L212-L232

@melya
Copy link
ContributorAuthor

Seems like failed tests are unrelated to my changes

@melya
Copy link
ContributorAuthor

@nicolas-grekas@stetodd friendly reminder ;)

@melyamelyaforce-pushed thefix-map-uploaded-file-in-request-payload-resolver2 branch fromfaa10b6 to93d4190CompareSeptember 24, 2024 14:22
@melya
Copy link
ContributorAuthor

any updates?

@nicolas-grekasnicolas-grekas modified the milestones:7.1,7.2Feb 4, 2025
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@OskarStarkOskarStarkOskarStark left review comments

@stetoddstetoddAwaiting requested review from stetodd

@nicolas-grekasnicolas-grekasAwaiting requested review from nicolas-grekas

Assignees
No one assigned
Projects
None yet
Milestone
7.2
Development

Successfully merging this pull request may close these issues.

5 participants
@melya@nicolas-grekas@OskarStark@stetodd@carsonbot

[8]ページ先頭

©2009-2025 Movatter.jp