Skip to content

Commit 2a6b881

Browse files
Merge pull request #58 from utopia-php/fix-forward-param-fields
fix: forward deprecated and example param fields from Action to Route
2 parents 068ee46 + 9dff0cb commit 2a6b881

4 files changed

Lines changed: 63 additions & 3 deletions

File tree

src/Platform/Platform.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ protected function initHttp(array $services): void
115115
switch ($option['type']) {
116116
case 'param':
117117
$key = substr($key, stripos($key, ':') + 1);
118-
$hook->param($key, $option['default'], $option['validator'], $option['description'], $option['optional'], $option['injections'], $option['skipValidation']);
118+
$hook->param($key, $option['default'], $option['validator'], $option['description'], $option['optional'], $option['injections'], $option['skipValidation'], $option['deprecated'], $option['example']);
119119
break;
120120
case 'injection':
121121
$hook->inject($option['name']);
@@ -165,7 +165,7 @@ protected function initTasks(array $services): void
165165
switch ($option['type']) {
166166
case 'param':
167167
$key = substr($key, stripos($key, ':') + 1);
168-
$hook->param($key, $option['default'], $option['validator'], $option['description'], $option['optional'], $option['injections'], $option['skipValidation']);
168+
$hook->param($key, $option['default'], $option['validator'], $option['description'], $option['optional'], $option['injections'], $option['skipValidation'], $option['deprecated'], $option['example']);
169169
break;
170170
case 'injection':
171171
$hook->inject($option['name']);
@@ -222,7 +222,7 @@ protected function initWorker(array $services, string $workerName): void
222222
switch ($option['type']) {
223223
case 'param':
224224
$key = substr($key, stripos($key, ':') + 1);
225-
$hook->param($key, $option['default'], $option['validator'], $option['description'], $option['optional'], $option['injections'], $option['skipValidation']);
225+
$hook->param($key, $option['default'], $option['validator'], $option['description'], $option['optional'], $option['injections'], $option['skipValidation'], $option['deprecated'], $option['example']);
226226
break;
227227
case 'injection':
228228
$hook->inject($option['name']);
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace Utopia\Tests;
4+
5+
use Utopia\Platform\Action;
6+
use Utopia\Validator\Boolean;
7+
use Utopia\Validator\Range;
8+
use Utopia\Validator\Text;
9+
10+
class TestActionWithParams extends Action
11+
{
12+
public function __construct()
13+
{
14+
$this->httpPath = '/with-params';
15+
$this->httpMethod = 'GET';
16+
17+
$this
18+
->param('name', '', new Text(128), 'User name.', false, example: 'John Doe')
19+
->param('age', 0, new Range(0, 150), 'User age.', true, example: '25')
20+
->param('active', false, new Boolean(true), 'Is active.', true, deprecated: true, example: 'true')
21+
->inject('response')
22+
->callback(function ($name, $age, $active, $response) {
23+
$response->send('OK');
24+
});
25+
}
26+
}

tests/Platform/TestService.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,6 @@ public function __construct()
1313
$this->addAction('chunked', new TestActionChunked());
1414
$this->addAction('redirect', new TestActionRedirect());
1515
$this->addAction('initHook', new TestActionInit());
16+
$this->addAction('withParams', new TestActionWithParams());
1617
}
1718
}

tests/e2e/HTTPServicesTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,4 +106,37 @@ public function testHook()
106106
$this->assertEquals('Hello World!', $result);
107107
$this->assertEquals('', ($response1->getHeaders()['x-init'] ?? ''));
108108
}
109+
110+
public function testActionParamFieldsForwardedToRoute()
111+
{
112+
$routes = Http::getRoutes();
113+
114+
$route = null;
115+
foreach ($routes as $method => $methodRoutes) {
116+
foreach ($methodRoutes as $r) {
117+
if ($r->getPath() === '/with-params') {
118+
$route = $r;
119+
break 2;
120+
}
121+
}
122+
}
123+
124+
$this->assertNotNull($route, 'Route /with-params should be registered');
125+
126+
$params = $route->getParams();
127+
128+
// Verify all Action::param() fields are forwarded to the Route
129+
$actionParamKeys = ['default', 'validator', 'description', 'optional', 'injections', 'skipValidation', 'deprecated', 'example'];
130+
131+
foreach ($params as $name => $param) {
132+
foreach ($actionParamKeys as $key) {
133+
$this->assertArrayHasKey($key, $param, "Param '{$name}' is missing '{$key}' on the Route. Platform must forward all Action param fields.");
134+
}
135+
}
136+
137+
$this->assertEquals('John Doe', $params['name']['example']);
138+
$this->assertFalse($params['name']['deprecated']);
139+
$this->assertEquals('true', $params['active']['example']);
140+
$this->assertTrue($params['active']['deprecated']);
141+
}
109142
}

0 commit comments

Comments
 (0)