From 528b3cfcd1ff99d7720025655113275c25bcc07d Mon Sep 17 00:00:00 2001 From: Yurun Date: Sun, 18 Feb 2024 17:32:31 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=20HTTP=20=E6=8E=A7=E5=88=B6?= =?UTF-8?q?=E5=99=A8=20string=20=E7=B1=BB=E5=9E=8B=E5=8F=82=E6=95=B0?= =?UTF-8?q?=E5=80=BC=20(#678)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 修复 HTTP 控制器 string 类型参数值 * 更新测试 * 格式化 --- .../grpc/src/Middleware/ActionMiddleware.php | 3 +++ ...numController.php => ParamsController.php} | 14 +++++++++--- .../unit/HttpServer/Tests/RequestTest.php | 22 +++++++++++++++---- .../Http/Middleware/ActionMiddleware.php | 3 +++ tests/unit/Component/.runtime/.gitkeep | 0 5 files changed, 35 insertions(+), 7 deletions(-) rename src/Components/swoole/tests/unit/HttpServer/ApiServer/Controller/{EnumController.php => ParamsController.php} (78%) create mode 100644 tests/unit/Component/.runtime/.gitkeep diff --git a/src/Components/grpc/src/Middleware/ActionMiddleware.php b/src/Components/grpc/src/Middleware/ActionMiddleware.php index 71bd9b4c02..5542dfe130 100644 --- a/src/Components/grpc/src/Middleware/ActionMiddleware.php +++ b/src/Components/grpc/src/Middleware/ActionMiddleware.php @@ -305,6 +305,9 @@ private function prepareActionParams(Request $request, RouteResult $routeResult) case 'bool': $value = (bool) $value; break 2; + case 'string': + $value = (string) $value; + break 2; default: switch ($type['type']) { diff --git a/src/Components/swoole/tests/unit/HttpServer/ApiServer/Controller/EnumController.php b/src/Components/swoole/tests/unit/HttpServer/ApiServer/Controller/ParamsController.php similarity index 78% rename from src/Components/swoole/tests/unit/HttpServer/ApiServer/Controller/EnumController.php rename to src/Components/swoole/tests/unit/HttpServer/ApiServer/Controller/ParamsController.php index 67f013f4e5..19fce5ba4b 100644 --- a/src/Components/swoole/tests/unit/HttpServer/ApiServer/Controller/EnumController.php +++ b/src/Components/swoole/tests/unit/HttpServer/ApiServer/Controller/ParamsController.php @@ -4,7 +4,7 @@ namespace Imi\Swoole\Test\HttpServer\ApiServer\Controller; -if (\PHP_VERSION_ID >= 80100 && !class_exists(EnumController::class, false)) +if (\PHP_VERSION_ID >= 80100 && !class_exists(ParamsController::class, false)) { eval(<<<'PHP' namespace Imi\Swoole\Test\HttpServer\ApiServer\Controller; @@ -15,8 +15,8 @@ use Imi\Test\Component\Enum\TestEnumBeanBacked; use Imi\Test\Component\Enum\TestEnumBeanBackedInt; - #[Controller(prefix: '/enum/')] - class EnumController extends HttpController + #[Controller(prefix: '/params/')] + class ParamsController extends HttpController { #[Action] public function test1(TestEnumBean $enum, TestEnumBeanBacked $enumBacked, TestEnumBeanBackedInt $enumBackedInt): array @@ -37,6 +37,14 @@ public function test2(TestEnumBean|string $enum = '', TestEnumBeanBacked|string 'enumBackedInt' => $enumBackedInt, ]; } + + #[Action] + public function test3(string|int $value): array + { + return [ + 'value' => $value, + ]; + } } PHP); } diff --git a/src/Components/swoole/tests/unit/HttpServer/Tests/RequestTest.php b/src/Components/swoole/tests/unit/HttpServer/Tests/RequestTest.php index 1ca976a7b4..2864bf73aa 100644 --- a/src/Components/swoole/tests/unit/HttpServer/Tests/RequestTest.php +++ b/src/Components/swoole/tests/unit/HttpServer/Tests/RequestTest.php @@ -392,29 +392,43 @@ public function testEnum(): void $this->markTestSkipped(); } $http = new HttpRequest(); - $response = $http->get($this->host . 'enum/test1?enum=A&enumBacked=imi&enumBackedInt=1'); + $response = $http->get($this->host . 'params/test1?enum=A&enumBacked=imi&enumBackedInt=1'); $this->assertEquals([ 'enum' => 'A', 'enumBacked' => 'imi', 'enumBackedInt' => 1, ], $response->json(true)); - $response = $http->get($this->host . 'enum/test2'); + $response = $http->get($this->host . 'params/test2'); $this->assertEquals([ 'enum' => '', 'enumBacked' => '', 'enumBackedInt' => 0, ], $response->json(true)); - $response = $http->get($this->host . 'enum/test2?enum=A&enumBacked=imi&enumBackedInt=1'); + $response = $http->get($this->host . 'params/test2?enum=A&enumBacked=imi&enumBackedInt=1'); $this->assertEquals([ 'enum' => 'A', 'enumBacked' => 'imi', 'enumBackedInt' => 1, ], $response->json(true)); - $response = $http->get($this->host . 'enum/test2?enum=x&enumBacked=x&enumBackedInt=0'); + $response = $http->get($this->host . 'params/test2?enum=x&enumBacked=x&enumBackedInt=0'); $this->assertEquals([ 'enum' => 'x', 'enumBacked' => 'x', 'enumBackedInt' => 0, ], $response->json(true)); + $response = $http->get($this->host . 'params/test3?value=1'); + $this->assertEquals([ + 'value' => '1', + ], $response->json(true)); + $response = $http->post($this->host . 'params/test3', [ + 'value' => 1, + ], 'json'); + $this->assertEquals([ + 'value' => 1, + ], $response->json(true)); + $response = $http->get($this->host . 'params/test3?value=a'); + $this->assertEquals([ + 'value' => 'a', + ], $response->json(true)); } } diff --git a/src/Server/Http/Middleware/ActionMiddleware.php b/src/Server/Http/Middleware/ActionMiddleware.php index fba73cbe73..8f2057a4fd 100644 --- a/src/Server/Http/Middleware/ActionMiddleware.php +++ b/src/Server/Http/Middleware/ActionMiddleware.php @@ -291,6 +291,9 @@ private function prepareActionParams(Request $request, RouteResult $routeResult) case 'bool': $value = (bool) $value; break 2; + case 'string': + $value = (string) $value; + break 2; default: switch ($type['type']) { diff --git a/tests/unit/Component/.runtime/.gitkeep b/tests/unit/Component/.runtime/.gitkeep new file mode 100644 index 0000000000..e69de29bb2