Skip to content

Commit

Permalink
Fix string to int action parameter coercion failing for '0'
Browse files Browse the repository at this point in the history
  • Loading branch information
othercorey committed Jul 29, 2022
1 parent c60b422 commit 0c32784
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 0c32784

Please sign in to comment.