Skip to content

Fix behavior for controller actions with union types #20352

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

chriscpty
Copy link
Contributor

This fixes the behaviour for yii\web\Controller::bindActionParams when a function parameter has a union type.

In effect, this fixes two issues:

  • For a union type that can be a string or a numeric value (such as string|int), if the parameter value is a string containing a numeric value (such as '1'), the result will be the numeric value ((int)1 here) rather than the string.
  • For a union type that can be either an array or a different type (such as int|array), if the parameter value is an array, the controller no longer throws a BadRequestHttpException, fixing "Bad request" for array parameter with union type #20351.
Q A
Is bugfix? ✔️
New feature?
Breaks BC?
Fixed issues #20351

! fix yii\web\Controller::bindActionParams behaviour for union types
o some refactoring in the process
o cleanup, changelog
Copy link

codecov bot commented Apr 10, 2025

Codecov Report

Attention: Patch coverage is 39.13043% with 42 lines in your changes missing coverage. Please review.

Project coverage is 63.38%. Comparing base (2fad37b) to head (d29998c).

Files with missing lines Patch % Lines
framework/web/Controller.php 39.13% 42 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##             master   #20352      +/-   ##
============================================
- Coverage     63.44%   63.38%   -0.06%     
- Complexity    11445    11468      +23     
============================================
  Files           431      431              
  Lines         37207    37251      +44     
============================================
+ Hits          23606    23612       +6     
- Misses        13601    13639      +38     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@chriscpty
Copy link
Contributor Author

Does codecov only check for coverage with PHP < 8.0 (hence the reduced coverage, because a good part of the added code is only touched in PHP >= 8.0 and is covered by tests codecov doesn't seem to see)?

@samdark samdark requested review from a team April 13, 2025 07:38
if ($type instanceof \ReflectionNamedType) {
[$result, $isValid] = $this->filterSingleTypeActionParam($params[$name], $type);
$params[$name] = $result;
} elseif (class_exists('\ReflectionUnionType') && $type instanceof \ReflectionUnionType) {
Copy link
Member

Choose a reason for hiding this comment

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

I don't think class_exists is needed here.

@samdark
Copy link
Member

samdark commented Apr 13, 2025

Would you please measure performance impact w/ phpbench if possible?

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

Successfully merging this pull request may close these issues.

2 participants