Skip to content

Commit

Permalink
修复 HTTP 控制器 string 类型参数值 (#678)
Browse files Browse the repository at this point in the history
* 修复 HTTP 控制器 string 类型参数值

* 更新测试

* 格式化
  • Loading branch information
Yurunsoft authored Feb 18, 2024
1 parent d3cb325 commit 528b3cf
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 7 deletions.
3 changes: 3 additions & 0 deletions src/Components/grpc/src/Middleware/ActionMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'])
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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);
}
22 changes: 18 additions & 4 deletions src/Components/swoole/tests/unit/HttpServer/Tests/RequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
3 changes: 3 additions & 0 deletions src/Server/Http/Middleware/ActionMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'])
{
Expand Down
Empty file.

0 comments on commit 528b3cf

Please sign in to comment.