Skip to content

Commit 528b3cf

Browse files
authored
修复 HTTP 控制器 string 类型参数值 (#678)
* 修复 HTTP 控制器 string 类型参数值 * 更新测试 * 格式化
1 parent d3cb325 commit 528b3cf

File tree

5 files changed

+35
-7
lines changed

5 files changed

+35
-7
lines changed

src/Components/grpc/src/Middleware/ActionMiddleware.php

+3
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,9 @@ private function prepareActionParams(Request $request, RouteResult $routeResult)
305305
case 'bool':
306306
$value = (bool) $value;
307307
break 2;
308+
case 'string':
309+
$value = (string) $value;
310+
break 2;
308311
default:
309312
switch ($type['type'])
310313
{

src/Components/swoole/tests/unit/HttpServer/ApiServer/Controller/EnumController.php src/Components/swoole/tests/unit/HttpServer/ApiServer/Controller/ParamsController.php

+11-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace Imi\Swoole\Test\HttpServer\ApiServer\Controller;
66

7-
if (\PHP_VERSION_ID >= 80100 && !class_exists(EnumController::class, false))
7+
if (\PHP_VERSION_ID >= 80100 && !class_exists(ParamsController::class, false))
88
{
99
eval(<<<'PHP'
1010
namespace Imi\Swoole\Test\HttpServer\ApiServer\Controller;
@@ -15,8 +15,8 @@
1515
use Imi\Test\Component\Enum\TestEnumBeanBacked;
1616
use Imi\Test\Component\Enum\TestEnumBeanBackedInt;
1717
18-
#[Controller(prefix: '/enum/')]
19-
class EnumController extends HttpController
18+
#[Controller(prefix: '/params/')]
19+
class ParamsController extends HttpController
2020
{
2121
#[Action]
2222
public function test1(TestEnumBean $enum, TestEnumBeanBacked $enumBacked, TestEnumBeanBackedInt $enumBackedInt): array
@@ -37,6 +37,14 @@ public function test2(TestEnumBean|string $enum = '', TestEnumBeanBacked|string
3737
'enumBackedInt' => $enumBackedInt,
3838
];
3939
}
40+
41+
#[Action]
42+
public function test3(string|int $value): array
43+
{
44+
return [
45+
'value' => $value,
46+
];
47+
}
4048
}
4149
PHP);
4250
}

src/Components/swoole/tests/unit/HttpServer/Tests/RequestTest.php

+18-4
Original file line numberDiff line numberDiff line change
@@ -392,29 +392,43 @@ public function testEnum(): void
392392
$this->markTestSkipped();
393393
}
394394
$http = new HttpRequest();
395-
$response = $http->get($this->host . 'enum/test1?enum=A&enumBacked=imi&enumBackedInt=1');
395+
$response = $http->get($this->host . 'params/test1?enum=A&enumBacked=imi&enumBackedInt=1');
396396
$this->assertEquals([
397397
'enum' => 'A',
398398
'enumBacked' => 'imi',
399399
'enumBackedInt' => 1,
400400
], $response->json(true));
401-
$response = $http->get($this->host . 'enum/test2');
401+
$response = $http->get($this->host . 'params/test2');
402402
$this->assertEquals([
403403
'enum' => '',
404404
'enumBacked' => '',
405405
'enumBackedInt' => 0,
406406
], $response->json(true));
407-
$response = $http->get($this->host . 'enum/test2?enum=A&enumBacked=imi&enumBackedInt=1');
407+
$response = $http->get($this->host . 'params/test2?enum=A&enumBacked=imi&enumBackedInt=1');
408408
$this->assertEquals([
409409
'enum' => 'A',
410410
'enumBacked' => 'imi',
411411
'enumBackedInt' => 1,
412412
], $response->json(true));
413-
$response = $http->get($this->host . 'enum/test2?enum=x&enumBacked=x&enumBackedInt=0');
413+
$response = $http->get($this->host . 'params/test2?enum=x&enumBacked=x&enumBackedInt=0');
414414
$this->assertEquals([
415415
'enum' => 'x',
416416
'enumBacked' => 'x',
417417
'enumBackedInt' => 0,
418418
], $response->json(true));
419+
$response = $http->get($this->host . 'params/test3?value=1');
420+
$this->assertEquals([
421+
'value' => '1',
422+
], $response->json(true));
423+
$response = $http->post($this->host . 'params/test3', [
424+
'value' => 1,
425+
], 'json');
426+
$this->assertEquals([
427+
'value' => 1,
428+
], $response->json(true));
429+
$response = $http->get($this->host . 'params/test3?value=a');
430+
$this->assertEquals([
431+
'value' => 'a',
432+
], $response->json(true));
419433
}
420434
}

src/Server/Http/Middleware/ActionMiddleware.php

+3
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,9 @@ private function prepareActionParams(Request $request, RouteResult $routeResult)
291291
case 'bool':
292292
$value = (bool) $value;
293293
break 2;
294+
case 'string':
295+
$value = (string) $value;
296+
break 2;
294297
default:
295298
switch ($type['type'])
296299
{

tests/unit/Component/.runtime/.gitkeep

Whitespace-only changes.

0 commit comments

Comments
 (0)