Browse Source

Fix passedParams with non string scalar values.

ControllerFactory no longer tries to coerce type for non string
passed arguments.

Fixes #16307
ADmad 4 years ago
parent
commit
94de0857f3

+ 1 - 1
src/Controller/ControllerFactory.php

@@ -208,7 +208,7 @@ class ControllerFactory implements ControllerFactoryInterface, RequestHandlerInt
             // Use any passed params as positional arguments
             if ($passedParams) {
                 $argument = array_shift($passedParams);
-                if ($type instanceof ReflectionNamedType) {
+                if (is_string($argument) && $type instanceof ReflectionNamedType) {
                     $typedArgument = $this->coerceStringToType($argument, $type);
 
                     if ($typedArgument === null) {

+ 19 - 0
tests/TestCase/Controller/ControllerFactoryTest.php

@@ -490,6 +490,25 @@ class ControllerFactoryTest extends TestCase
         $this->assertEquals($data->dep->id, $inject->id);
     }
 
+    public function testCreateWithNonStringScalarRouteParam(): void
+    {
+        $request = new ServerRequest([
+            'url' => 'test_plugin_three/dependencies/required_typed',
+            'params' => [
+                'plugin' => null,
+                'controller' => 'Dependencies',
+                'action' => 'requiredTyped',
+                'pass' => [1.1, 2, true, ['foo' => 'bar']],
+            ],
+        ]);
+        $controller = $this->factory->create($request);
+        $response = $this->factory->invoke($controller);
+
+        $expected = ['one' => 1.1, 'two' => 2, 'three' => true, 'four' => ['foo' => 'bar']];
+        $data = json_decode((string)$response->getBody(), true);
+        $this->assertSame($expected, $data);
+    }
+
     /**
      * Ensure that a controllers startup process can emit a response
      */