Skip to content

Commit

Permalink
Merge pull request #16658 from cakephp/fix-int-param
Browse files Browse the repository at this point in the history
Fix string to int action parameter coercion failing for '0'
  • Loading branch information
markstory committed Jul 30, 2022
2 parents c60b422 + 0c32784 commit 923f04a
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 3 deletions.
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);
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

0 comments on commit 923f04a

Please sign in to comment.