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

修复 HTTP 控制器 string 类型参数值 #678

Merged
merged 3 commits into from
Feb 18, 2024
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
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.