Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[HttpKernel] Fix variadic TypeError for #[MapUploadedFile] #54901

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
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,11 @@ public function onKernelControllerArguments(ControllerArgumentsEvent $event): vo
};
}

$arguments[$i] = $payload;
if ($argument->metadata->isVariadic() && \is_array($payload)) {
Copy link
Member

Choose a reason for hiding this comment

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

if the payload is not an array, we should throw a 422 instead, don't you think?
we should also use array_is_list so that we don't map to named arguments by mistake

array_splice($arguments, $i, 1, $payload);
Copy link
Member

Choose a reason for hiding this comment

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

could we use array_merge instead? that might be more readable

Copy link
Contributor

@melya melya Jul 29, 2024

Choose a reason for hiding this comment

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

Seems like the PR is somewhat stale 😞

Unfortunately, array_merge won't work here because the current logic processes the existing $arguments and replaces them one by one. The variadic variable can only be at the end of the function signature, so we need to extend that array further.
Therefore, array_splice is appropriate here.

I have added a bit updated fix to my PR, where I tried to fix another related issue with #[MapUploadedFile]

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@melya I will close this PR then. Thank you :)

} else {
$arguments[$i] = $payload;
}
}

$event->setArguments($arguments);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ static function () {},
$resolver->onKernelControllerArguments($event);

/** @var UploadedFile[] $data */
$data = $event->getArguments()[0];
$data = $event->getArguments();

$this->assertCount(2, $data);
$this->assertSame('file-small.txt', $data[0]->getFilename());
Expand Down