Skip to content

Commit

Permalink
fix(OCS): Fix route vs Controller differences
Browse files Browse the repository at this point in the history
Signed-off-by: provokateurin <[email protected]>
  • Loading branch information
provokateurin committed Nov 13, 2024
1 parent 2587f18 commit 123e228
Show file tree
Hide file tree
Showing 3 changed files with 184 additions and 18 deletions.
30 changes: 18 additions & 12 deletions generate-spec
Original file line number Diff line number Diff line change
Expand Up @@ -334,12 +334,11 @@ if (count($parsedRoutes) === 0) {
$operationIds = [];

foreach ($parsedRoutes as $key => $value) {
$isOCS = $key === 'ocs';
$isIndex = $key === 'routes';

if (!$isOCS && !$isIndex) {
continue;
}
$pathPrefix = match ($key) {
'ocs' => '/ocs/v2.php',
'routes' => '/index.php',
default => throw new \InvalidArgumentException('Unknown routes key "' . $key . '"'),
};

foreach ($value as $route) {
$routeName = $route['name'];
Expand All @@ -356,12 +355,7 @@ foreach ($parsedRoutes as $key => $value) {
if (str_ends_with($url, '/')) {
$url = substr($url, 0, -1);
}
if ($isIndex) {
$url = '/index.php' . $root . $url;
}
if ($isOCS) {
$url = '/ocs/v2.php' . $root . $url;
}
$url = $pathPrefix . $root . $url;

$methodName = lcfirst(str_replace('_', '', ucwords(explode('#', $routeName)[1], '_')));
if ($methodName == 'preflightedCors') {
Expand All @@ -382,6 +376,18 @@ foreach ($parsedRoutes as $key => $value) {
continue;
}

$parentControllerClass = $controllerClass->extends?->name ?? '';
$parentControllerClass = explode('\\', $parentControllerClass);
$parentControllerClass = end($parentControllerClass);
// This is very ugly, but since we do not parse the entire source code we can not say with certainty which controller type is used.
// To still allow apps to use custom controllers that extend OCSController, we only check the suffix and have the warning if the controller type can not be detected.
$isOCS = str_ends_with($parentControllerClass, 'OCSController');
if ($parentControllerClass !== 'Controller' && $parentControllerClass !== 'ApiController' && $parentControllerClass !== 'OCSController' && !$isOCS) {
Logger::warning($routeName, 'You are extending a custom controller class. Make sure that it ends with "OCSController" if it extends "OCSController" itself.');
} elseif ($isOCS !== ($pathPrefix === '/ocs/v2.php')) {
Logger::warning($routeName, 'Do not mix OCS/non-OCS routes and non-OCS/OCS controllers!');
}

$controllerScopes = Helpers::getOpenAPIAttributeScopes($controllerClass, $routeName);
if (Helpers::classMethodHasAnnotationOrAttribute($controllerClass, 'IgnoreOpenAPI')) {
if (count($controllerScopes) === 0 || (in_array('ignore', $controllerScopes, true) && count($controllerScopes) === 1)) {
Expand Down
86 changes: 83 additions & 3 deletions tests/openapi-administration.json
Original file line number Diff line number Diff line change
Expand Up @@ -4625,14 +4625,44 @@
"pattern": "^[a-z]+$",
"default": "abc"
}
},
{
"name": "OCS-APIRequest",
"in": "header",
"description": "Required to be true for the API request to pass",
"required": true,
"schema": {
"type": "boolean",
"default": true
}
}
],
"responses": {
"200": {
"description": "Success",
"content": {
"application/json": {
"schema": {}
"schema": {
"type": "object",
"required": [
"ocs"
],
"properties": {
"ocs": {
"type": "object",
"required": [
"meta",
"data"
],
"properties": {
"meta": {
"$ref": "#/components/schemas/OCSMeta"
},
"data": {}
}
}
}
}
}
}
}
Expand Down Expand Up @@ -4663,14 +4693,44 @@
"pattern": "^[a-z]+$",
"default": "abc"
}
},
{
"name": "OCS-APIRequest",
"in": "header",
"description": "Required to be true for the API request to pass",
"required": true,
"schema": {
"type": "boolean",
"default": true
}
}
],
"responses": {
"200": {
"description": "Success",
"content": {
"application/json": {
"schema": {}
"schema": {
"type": "object",
"required": [
"ocs"
],
"properties": {
"ocs": {
"type": "object",
"required": [
"meta",
"data"
],
"properties": {
"meta": {
"$ref": "#/components/schemas/OCSMeta"
},
"data": {}
}
}
}
}
}
}
}
Expand Down Expand Up @@ -4718,7 +4778,27 @@
"description": "Success",
"content": {
"application/json": {
"schema": {}
"schema": {
"type": "object",
"required": [
"ocs"
],
"properties": {
"ocs": {
"type": "object",
"required": [
"meta",
"data"
],
"properties": {
"meta": {
"$ref": "#/components/schemas/OCSMeta"
},
"data": {}
}
}
}
}
}
}
}
Expand Down
86 changes: 83 additions & 3 deletions tests/openapi-full.json
Original file line number Diff line number Diff line change
Expand Up @@ -4775,14 +4775,44 @@
"pattern": "^[a-z]+$",
"default": "abc"
}
},
{
"name": "OCS-APIRequest",
"in": "header",
"description": "Required to be true for the API request to pass",
"required": true,
"schema": {
"type": "boolean",
"default": true
}
}
],
"responses": {
"200": {
"description": "Success",
"content": {
"application/json": {
"schema": {}
"schema": {
"type": "object",
"required": [
"ocs"
],
"properties": {
"ocs": {
"type": "object",
"required": [
"meta",
"data"
],
"properties": {
"meta": {
"$ref": "#/components/schemas/OCSMeta"
},
"data": {}
}
}
}
}
}
}
}
Expand Down Expand Up @@ -4813,14 +4843,44 @@
"pattern": "^[a-z]+$",
"default": "abc"
}
},
{
"name": "OCS-APIRequest",
"in": "header",
"description": "Required to be true for the API request to pass",
"required": true,
"schema": {
"type": "boolean",
"default": true
}
}
],
"responses": {
"200": {
"description": "Success",
"content": {
"application/json": {
"schema": {}
"schema": {
"type": "object",
"required": [
"ocs"
],
"properties": {
"ocs": {
"type": "object",
"required": [
"meta",
"data"
],
"properties": {
"meta": {
"$ref": "#/components/schemas/OCSMeta"
},
"data": {}
}
}
}
}
}
}
}
Expand Down Expand Up @@ -4868,7 +4928,27 @@
"description": "Success",
"content": {
"application/json": {
"schema": {}
"schema": {
"type": "object",
"required": [
"ocs"
],
"properties": {
"ocs": {
"type": "object",
"required": [
"meta",
"data"
],
"properties": {
"meta": {
"$ref": "#/components/schemas/OCSMeta"
},
"data": {}
}
}
}
}
}
}
}
Expand Down

0 comments on commit 123e228

Please sign in to comment.