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

Improve casting of integer routing parameters #16643

Merged
merged 2 commits into from Jul 25, 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 ctype_digit($argument) ? (int)$argument : null;
return filter_var($argument, FILTER_VALIDATE_INT) ? (int)$argument : null;
case 'bool':
return $argument === '0' ? false : ($argument === '1' ? true : null);
case 'array':
Expand Down
24 changes: 17 additions & 7 deletions tests/TestCase/Controller/ControllerFactoryTest.php
Expand Up @@ -733,15 +733,13 @@ public function testInvokePassedParametersCoercion(): void
'plugin' => null,
'controller' => 'Dependencies',
'action' => 'requiredTyped',
'pass' => ['1.0', '02', '0', '8,9'],
'pass' => ['1.0', '2', '0', '8,9'],
],
]);
$controller = $this->factory->create($request);

$result = $this->factory->invoke($controller);
$data = json_decode((string)$result->getBody(), true);

$this->assertNotNull($data);
$this->assertSame(['one' => 1.0, 'two' => 2, 'three' => false, 'four' => ['8', '9']], $data);

$request = new ServerRequest([
Expand All @@ -750,16 +748,29 @@ public function testInvokePassedParametersCoercion(): void
'plugin' => null,
'controller' => 'Dependencies',
'action' => 'requiredTyped',
'pass' => ['1.0', '02', '0', ''],
'pass' => ['1.0', '2', '0', ''],
],
]);
$controller = $this->factory->create($request);

$result = $this->factory->invoke($controller);
$data = json_decode((string)$result->getBody(), true);

$this->assertNotNull($data);
$this->assertSame(['one' => 1.0, 'two' => 2, 'three' => false, 'four' => []], $data);

$request = new ServerRequest([
'url' => 'test_plugin_three/dependencies/requiredTyped',
'params' => [
'plugin' => null,
'controller' => 'Dependencies',
'action' => 'requiredTyped',
'pass' => ['1.0', '-1', '0', ''],
othercorey marked this conversation as resolved.
Show resolved Hide resolved
],
]);
$controller = $this->factory->create($request);

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

/**
Expand All @@ -781,7 +792,6 @@ public function testInvokeOptionalTypedParam(): void
$result = $this->factory->invoke($controller);
$data = json_decode((string)$result->getBody(), true);

$this->assertNotNull($data);
$this->assertSame(['one' => 1.0, 'two' => 2, 'three' => true], $data);
}

Expand Down