Skip to content

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

Open
@chriscpty

Description

@chriscpty

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions