From 6a06e967933b692123553a183f3b4ba8e0f4b577 Mon Sep 17 00:00:00 2001 From: ADmad Date: Mon, 9 Oct 2023 19:42:59 +0530 Subject: [PATCH 1/2] Don't throw an exception for unsupoorted types. If the ControllerFactory encounters a method argument type like union type which it cannot use for conversion, it no longer throws an exception and passes the value as is. --- src/Controller/ControllerFactory.php | 11 ----------- tests/TestCase/Controller/ControllerFactoryTest.php | 9 +++++---- 2 files changed, 5 insertions(+), 15 deletions(-) diff --git a/src/Controller/ControllerFactory.php b/src/Controller/ControllerFactory.php index b68c66af018..ebeb2328d62 100644 --- a/src/Controller/ControllerFactory.php +++ b/src/Controller/ControllerFactory.php @@ -159,17 +159,6 @@ protected function getActionArgs(Closure $action, array $passedParams): array $function = new ReflectionFunction($action); foreach ($function->getParameters() as $parameter) { $type = $parameter->getType(); - if ($type && !$type instanceof ReflectionNamedType) { - // Only single types are supported - throw new InvalidParameterException([ - 'template' => 'unsupported_type', - 'parameter' => $parameter->getName(), - 'controller' => $this->controller->getName(), - 'action' => $this->controller->getRequest()->getParam('action'), - 'prefix' => $this->controller->getRequest()->getParam('prefix'), - 'plugin' => $this->controller->getRequest()->getParam('plugin'), - ]); - } // Check for dependency injection for classes if ($type instanceof ReflectionNamedType && !$type->isBuiltin()) { diff --git a/tests/TestCase/Controller/ControllerFactoryTest.php b/tests/TestCase/Controller/ControllerFactoryTest.php index f23187fc5aa..7d44dc311ad 100644 --- a/tests/TestCase/Controller/ControllerFactoryTest.php +++ b/tests/TestCase/Controller/ControllerFactoryTest.php @@ -888,14 +888,15 @@ public function testInvokePassedParamUnsupportedReflectionType(): void 'plugin' => null, 'controller' => 'Dependencies', 'action' => 'unsupportedTypedUnion', - 'pass' => ['test'], + 'pass' => ['1'], ], ]); $controller = $this->factory->create($request); - $this->expectException(InvalidParameterException::class); - $this->expectExceptionMessage('Type declaration for `one` in action `Dependencies::unsupportedTypedUnion()` is unsupported.'); - $this->factory->invoke($controller); + $result = $this->factory->invoke($controller); + $data = json_decode((string)$result->getBody(), true); + + $this->assertSame(['one' => '1'], $data); } public function testMiddleware(): void From 55795335a2c3c083e1fbbda48c6d0ed3c2fca533 Mon Sep 17 00:00:00 2001 From: ADmad Date: Wed, 11 Oct 2023 13:28:21 +0530 Subject: [PATCH 2/2] Rename test class method --- tests/TestCase/Controller/ControllerFactoryTest.php | 2 +- tests/test_app/TestApp/Controller/DependenciesController.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/TestCase/Controller/ControllerFactoryTest.php b/tests/TestCase/Controller/ControllerFactoryTest.php index 7d44dc311ad..13b36fc3fa9 100644 --- a/tests/TestCase/Controller/ControllerFactoryTest.php +++ b/tests/TestCase/Controller/ControllerFactoryTest.php @@ -887,7 +887,7 @@ public function testInvokePassedParamUnsupportedReflectionType(): void 'params' => [ 'plugin' => null, 'controller' => 'Dependencies', - 'action' => 'unsupportedTypedUnion', + 'action' => 'typedUnion', 'pass' => ['1'], ], ]); diff --git a/tests/test_app/TestApp/Controller/DependenciesController.php b/tests/test_app/TestApp/Controller/DependenciesController.php index 47f27276bd8..32af6f6d43a 100644 --- a/tests/test_app/TestApp/Controller/DependenciesController.php +++ b/tests/test_app/TestApp/Controller/DependenciesController.php @@ -60,7 +60,7 @@ public function unsupportedTyped(iterable $one) return $this->response->withStringBody(json_encode(compact('one'))); } - public function unsupportedTypedUnion(string|int $one) + public function typedUnion(string|int $one) { return $this->response->withStringBody(json_encode(compact('one'))); }