Skip to content
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

Fix string to int action parameter coercion failing for '0' #16658

Merged
merged 1 commit into from Jul 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Controller/ControllerFactory.php
Expand Up @@ -268,7 +268,7 @@ protected function coerceStringToType(string $argument, ReflectionNamedType $typ
case 'float':
return is_numeric($argument) ? (float)$argument : null;
case 'int':
return filter_var($argument, FILTER_VALIDATE_INT) ? (int)$argument : null;
return filter_var($argument, FILTER_VALIDATE_INT, FILTER_NULL_ON_FAILURE);
Copy link
Member

Choose a reason for hiding this comment

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

Oof, I totally forgot about 0 failing the ternary here 😢

case 'bool':
return $argument === '0' ? false : ($argument === '1' ? true : null);
case 'array':
Expand Down
4 changes: 2 additions & 2 deletions tests/TestCase/Controller/ControllerFactoryTest.php
Expand Up @@ -748,14 +748,14 @@ public function testInvokePassedParametersCoercion(): void
'plugin' => null,
'controller' => 'Dependencies',
'action' => 'requiredTyped',
'pass' => ['1.0', '2', '0', ''],
'pass' => ['1.0', '0', '0', ''],
],
]);
$controller = $this->factory->create($request);

$result = $this->factory->invoke($controller);
$data = json_decode((string)$result->getBody(), true);
$this->assertSame(['one' => 1.0, 'two' => 2, 'three' => false, 'four' => []], $data);
$this->assertSame(['one' => 1.0, 'two' => 0, 'three' => false, 'four' => []], $data);

$request = new ServerRequest([
'url' => 'test_plugin_three/dependencies/requiredTyped',
Expand Down