Skip to content

Commit ae5d109

Browse files
committed
feat(ControllerMethod): Support getParam()
Signed-off-by: provokateurin <[email protected]>
1 parent 62c1caa commit ae5d109

File tree

5 files changed

+235
-3
lines changed

5 files changed

+235
-3
lines changed

src/ControllerMethod.php

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -291,9 +291,38 @@ public static function parse(string $context,
291291
if ($methodCall->var instanceof PropertyFetch &&
292292
$methodCall->var->var instanceof Variable &&
293293
$methodCall->var->var->name === 'this' &&
294-
$methodCall->var->name->name === 'request' &&
295-
$methodCall->name->name === 'getHeader') {
296-
$headers[] = Helpers::exprToValue($context . ': getHeader', $methodCall->args[0]->value);
294+
$methodCall->var->name->name === 'request') {
295+
if ($methodCall->name->name === 'getHeader') {
296+
$headers[] = $methodCall->args[0]->value->value;
297+
}
298+
if ($methodCall->name->name === 'getParam') {
299+
$name = $methodCall->args[0]->value->value;
300+
301+
if (preg_match('/^[a-zA-Z][a-zA-Z0-9_]*$/', $name)) {
302+
Logger::error($context . ': getParam: ' . $name, 'Do not use getParam() when a controller method parameter also works. With getParam() it is not possible to add a comment and specify the parameter type, therefore it should be avoided whenever possible.');
303+
}
304+
305+
$defaultValue = null;
306+
$hasDefaultValue = false;
307+
try {
308+
$defaultValue = count($methodCall->args) > 1 ? Helpers::exprToValue($context . ': getParam: ' . $name, $methodCall->args[1]->value) : null;
309+
$hasDefaultValue = true;
310+
} catch (UnsupportedExprException $e) {
311+
Logger::debug($context, $e);
312+
}
313+
314+
$type = new OpenApiType(
315+
context: $context,
316+
// We can not know the type, so need to fallback to object :/
317+
type: 'object',
318+
// IRequest::getParam() has null as a default value, so the parameter always has a default value and allows null.
319+
nullable: true,
320+
hasDefaultValue: $hasDefaultValue,
321+
defaultValue: $defaultValue,
322+
);
323+
324+
$parameters[] = new ControllerMethodParameter($context, $definitions, $name, $type);
325+
}
297326
}
298327
}
299328

tests/appinfo/routes.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@
8787
['name' => 'Settings#samePathGet', 'url' => '/api/{apiVersion}/same-path', 'verb' => 'GET', 'requirements' => ['apiVersion' => '(v2)']],
8888
['name' => 'Settings#samePathPost', 'url' => '/api/{apiVersion}/same-path', 'verb' => 'POST', 'requirements' => ['apiVersion' => '(v2)']],
8989
['name' => 'Settings#requestHeader', 'url' => '/api/{apiVersion}/request-header', 'verb' => 'POST', 'requirements' => ['apiVersion' => '(v2)']],
90+
['name' => 'Settings#requestParams', 'url' => '/api/{apiVersion}/request-params', 'verb' => 'POST', 'requirements' => ['apiVersion' => '(v2)']],
9091
['name' => 'V1\SubDir#subDirRoute', 'url' => '/sub-dir', 'verb' => 'GET'],
9192
],
9293
];

tests/lib/Controller/SettingsController.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -764,4 +764,18 @@ public function requestHeader(): DataResponse {
764764

765765
return new DataResponse();
766766
}
767+
768+
/**
769+
* A method with a request params.
770+
*
771+
* @return DataResponse<Http::STATUS_OK, list<empty>, array{}>
772+
*
773+
* 200: Admin settings updated
774+
*/
775+
public function requestParams(): DataResponse {
776+
$value = $this->request->getParam('some-param');
777+
$value = $this->request->getParam('some-param-with-explicit-default-value', 'abc');
778+
779+
return new DataResponse();
780+
}
767781
}

tests/openapi-administration.json

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5521,6 +5521,100 @@
55215521
}
55225522
}
55235523
},
5524+
"/ocs/v2.php/apps/notifications/api/{apiVersion}/request-params": {
5525+
"post": {
5526+
"operationId": "settings-request-params",
5527+
"summary": "A method with a request params.",
5528+
"description": "This endpoint requires admin access",
5529+
"tags": [
5530+
"settings"
5531+
],
5532+
"security": [
5533+
{
5534+
"bearer_auth": []
5535+
},
5536+
{
5537+
"basic_auth": []
5538+
}
5539+
],
5540+
"requestBody": {
5541+
"required": false,
5542+
"content": {
5543+
"application/json": {
5544+
"schema": {
5545+
"type": "object",
5546+
"properties": {
5547+
"some-param": {
5548+
"type": "object",
5549+
"nullable": true,
5550+
"default": null
5551+
},
5552+
"some-param-with-explicit-default-value": {
5553+
"type": "object",
5554+
"nullable": true,
5555+
"default": "abc"
5556+
}
5557+
}
5558+
}
5559+
}
5560+
}
5561+
},
5562+
"parameters": [
5563+
{
5564+
"name": "apiVersion",
5565+
"in": "path",
5566+
"required": true,
5567+
"schema": {
5568+
"type": "string",
5569+
"enum": [
5570+
"v2"
5571+
],
5572+
"default": "v2"
5573+
}
5574+
},
5575+
{
5576+
"name": "OCS-APIRequest",
5577+
"in": "header",
5578+
"description": "Required to be true for the API request to pass",
5579+
"required": true,
5580+
"schema": {
5581+
"type": "boolean",
5582+
"default": true
5583+
}
5584+
}
5585+
],
5586+
"responses": {
5587+
"200": {
5588+
"description": "Admin settings updated",
5589+
"content": {
5590+
"application/json": {
5591+
"schema": {
5592+
"type": "object",
5593+
"required": [
5594+
"ocs"
5595+
],
5596+
"properties": {
5597+
"ocs": {
5598+
"type": "object",
5599+
"required": [
5600+
"meta",
5601+
"data"
5602+
],
5603+
"properties": {
5604+
"meta": {
5605+
"$ref": "#/components/schemas/OCSMeta"
5606+
},
5607+
"data": {}
5608+
}
5609+
}
5610+
}
5611+
}
5612+
}
5613+
}
5614+
}
5615+
}
5616+
}
5617+
},
55245618
"/ocs/v2.php/tests/attribute-ocs/{param}": {
55255619
"get": {
55265620
"operationId": "routing-attributeocs-route",

tests/openapi-full.json

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5678,6 +5678,100 @@
56785678
}
56795679
}
56805680
},
5681+
"/ocs/v2.php/apps/notifications/api/{apiVersion}/request-params": {
5682+
"post": {
5683+
"operationId": "settings-request-params",
5684+
"summary": "A method with a request params.",
5685+
"description": "This endpoint requires admin access",
5686+
"tags": [
5687+
"settings"
5688+
],
5689+
"security": [
5690+
{
5691+
"bearer_auth": []
5692+
},
5693+
{
5694+
"basic_auth": []
5695+
}
5696+
],
5697+
"requestBody": {
5698+
"required": false,
5699+
"content": {
5700+
"application/json": {
5701+
"schema": {
5702+
"type": "object",
5703+
"properties": {
5704+
"some-param": {
5705+
"type": "object",
5706+
"nullable": true,
5707+
"default": null
5708+
},
5709+
"some-param-with-explicit-default-value": {
5710+
"type": "object",
5711+
"nullable": true,
5712+
"default": "abc"
5713+
}
5714+
}
5715+
}
5716+
}
5717+
}
5718+
},
5719+
"parameters": [
5720+
{
5721+
"name": "apiVersion",
5722+
"in": "path",
5723+
"required": true,
5724+
"schema": {
5725+
"type": "string",
5726+
"enum": [
5727+
"v2"
5728+
],
5729+
"default": "v2"
5730+
}
5731+
},
5732+
{
5733+
"name": "OCS-APIRequest",
5734+
"in": "header",
5735+
"description": "Required to be true for the API request to pass",
5736+
"required": true,
5737+
"schema": {
5738+
"type": "boolean",
5739+
"default": true
5740+
}
5741+
}
5742+
],
5743+
"responses": {
5744+
"200": {
5745+
"description": "Admin settings updated",
5746+
"content": {
5747+
"application/json": {
5748+
"schema": {
5749+
"type": "object",
5750+
"required": [
5751+
"ocs"
5752+
],
5753+
"properties": {
5754+
"ocs": {
5755+
"type": "object",
5756+
"required": [
5757+
"meta",
5758+
"data"
5759+
],
5760+
"properties": {
5761+
"meta": {
5762+
"$ref": "#/components/schemas/OCSMeta"
5763+
},
5764+
"data": {}
5765+
}
5766+
}
5767+
}
5768+
}
5769+
}
5770+
}
5771+
}
5772+
}
5773+
}
5774+
},
56815775
"/ocs/v2.php/tests/attribute-ocs/{param}": {
56825776
"get": {
56835777
"operationId": "routing-attributeocs-route",

0 commit comments

Comments
 (0)