Skip to content

[HttpKernel] #[MapUploadedFile] throws http exception on empty files array if argument not nullable nor has default value #61381

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

Merged
merged 1 commit into from
Aug 19, 2025

Conversation

hwawshy
Copy link
Contributor

@hwawshy hwawshy commented Aug 10, 2025

Q A
Branch? 7.3
Bug fix? yes
New feature? no
Deprecations? no
Issues Fix #61376
License MIT

Fixes #61376

Copy link
Member

@fabpot fabpot left a comment

Choose a reason for hiding this comment

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

I understand that this is a bug fix, but also a behavior change, so I'm not sure we should do it in 7.3.
@symfony/mergers Thoughts?

@symfony symfony deleted a comment from carsonbot Aug 19, 2025
@Spomky
Copy link
Contributor

Spomky commented Aug 19, 2025

Indeed. Previously it returned an empty array or null (if the argument was nullable). Now it always returns null and may cause errors for non-nullable array arguments. An argument like #[MapUploadedFile] array $files should return [] and not null.

}

return $files;
return $request->files->get($attribute->name ?? $argument->getName());
Copy link
Member

@nicolas-grekas nicolas-grekas Aug 19, 2025

Choose a reason for hiding this comment

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

What looks strange to me in the previous code is defaulting to an array without caring about the accepted type. To reduce the behavioral diff, we should continue to default to an array when arrays are accepted - and when not, then we should fix the behavior.

This would need a new test case for the array-case.

Suggested change
return $request->files->get($attribute->name ?? $argument->getName());
if (!($files = $request->files->get($attribute->name ?? $argument->getName())) && ($argument->isNullable() || $argument->hasDefaultValue())) {
return null;
}
return $files ?? ('array' === $argument->getType() ? [] : null);

Copy link
Contributor

Choose a reason for hiding this comment

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

I see at least 4 test cases that should be verified when no file is provided:

  • #[MapUploadedFile] array $files => should return an empty array
  • #[MapUploadedFile] ?array $files => should return null
  • #[MapUploadedFile] UploadedFile $file => should return null and an exception will be thrown
  • #[MapUploadedFile] UploadedFile ?$file => should return null

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I understand returning an empty array for the sake of BC, but IMO an absence of value should be denoted by null and not an empty array. The user indicates the value is optional by making the argument nullable (that's also what the docs currently say), otherwise an HttpException should be thrown.

Copy link
Member

Choose a reason for hiding this comment

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

That's not how this works for arrays for the other value resolvers. We have to be consistent.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Alright, I applied your suggestion and added the missing test cases. I am not sure if the failed pipelines are related to my changes.

…array if argument not nullable nor has default value
@nicolas-grekas
Copy link
Member

Thank you @hwawshy.

@nicolas-grekas nicolas-grekas merged commit 4102229 into symfony:7.3 Aug 19, 2025
5 of 11 checks passed
@hwawshy hwawshy deleted the fix_61376 branch August 19, 2025 12:45
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.

7 participants