Skip to content

"Bad request" for array parameter with union type #20351

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
chriscpty opened this issue Apr 9, 2025 · 0 comments
Open

"Bad request" for array parameter with union type #20351

chriscpty opened this issue Apr 9, 2025 · 0 comments

Comments

@chriscpty
Copy link
Contributor

Take a yii\web\Controller with an example action like this:

    public function actionFoo(int|array $bar) {
        return $this->asJson(['foo' => $bar]);
    }

Expected result: calling this action with a single query parameter (like /foo?bar=1) and calling it with an array query parameter (like foo?bar[]=1&bar[]=2) both work.

Actual result: Calling it with an integer parameter works correctly, calling it with an array parameter returns a 400 Bad Request.

I'm pretty sure the problem lies with this portion of yii\web\Controller::bindActionParams (line 136 and onwards):

$isArray = ($type = $param->getType()) instanceof \ReflectionNamedType && $type->getName() === 'array';
if ($isArray) {
 $params[$name] = (array)$params[$name];
} elseif (is_array($params[$name])) {
 $isValid = false;
} elseif (
  // ...

$isArray does not check for whether $type is a ReflectionUnionType that includes array, hence the array parameter is wrongly rejected.

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

No branches or pull requests

1 participant