Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 19 additions & 8 deletions generate-spec.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
}

use Ahc\Cli\Input\Command;
use DirectoryIterator;
use PhpParser\Node\AttributeGroup;
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Expr\New_;
Expand All @@ -25,6 +24,8 @@
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Throw_;
use PhpParser\NodeFinder;
use PhpParser\NodeTraverser;
use PhpParser\NodeVisitor\NameResolver;
use PhpParser\ParserFactory;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTextNode;
Expand Down Expand Up @@ -69,6 +70,9 @@

$astParser = (new ParserFactory())->createForNewestSupportedVersion();
$nodeFinder = new NodeFinder;
$nameResolver = new NameResolver;
$nodeTraverser = new NodeTraverser;
$nodeTraverser->addVisitor($nameResolver);

$config = new ParserConfig(usedAttributes: ['lines' => true, 'indexes' => true, 'comments' => true]);
$lexer = new Lexer($config);
Expand All @@ -84,9 +88,12 @@
Logger::panic('appinfo', 'info.xml file at ' . $infoXMLPath . ' is not parsable');
}

$rawNamespace = $xml->namespace ?? ucfirst($xml->id);

$appIsCore = false;
$appNamespace = 'OCA\\' . $rawNamespace;
$appID = (string)$xml->id;
$readableAppID = $xml->namespace ? (string)$xml->namespace : Helpers::generateReadableAppID($appID);
$readableAppID = (string)$rawNamespace;
$appSummary = (string)$xml->summary;
$appVersion = (string)$xml->version;
$appLicence = (string)$xml->licence;
Expand All @@ -104,6 +111,7 @@
}

$appIsCore = true;
$appNamespace = 'OC\\Core';
$appID = 'core';
$readableAppID = 'Core';
$appSummary = 'Core functionality of Nextcloud';
Expand Down Expand Up @@ -242,9 +250,9 @@
$controllers = [];
$controllersDir = $sourceDir . '/Controller';
if (file_exists($controllersDir)) {
$dir = new DirectoryIterator($controllersDir);
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($controllersDir));
$controllerFiles = [];
foreach ($dir as $file) {
foreach ($iterator as $file) {
$filePath = $file->getPathname();
if (!str_ends_with($filePath, 'Controller.php')) {
continue;
Expand All @@ -254,7 +262,10 @@
sort($controllerFiles);

foreach ($controllerFiles as $filePath) {
$controllers[basename($filePath, 'Controller.php')] = $astParser->parse(file_get_contents($filePath));
$offset = strlen($controllersDir . '/');
$name = substr($filePath, $offset, strlen($filePath) - $offset - strlen('Controller.php'));
$name = str_replace('/', '\\', $name);
$controllers[$name] = $nodeTraverser->traverse($astParser->parse(file_get_contents($filePath)));
}
}

Expand All @@ -263,7 +274,7 @@
$controllerClass = null;
/** @var Class_ $class */
foreach ($nodeFinder->findInstanceOf($stmts, Class_::class) as $class) {
if ($class->name->name === $controllerName . 'Controller') {
if ($class->namespacedName->name === $appNamespace . '\\Controller\\' . $controllerName . 'Controller') {
$controllerClass = $class;
break;
}
Expand Down Expand Up @@ -369,7 +380,7 @@
$controllerClass = null;
/** @var Class_ $class */
foreach ($nodeFinder->findInstanceOf($controllers[$controllerName] ?? [], Class_::class) as $class) {
if ($class->name == $controllerName . 'Controller') {
if ($class->namespacedName->name === $appNamespace . '\\Controller\\' . $controllerName . 'Controller') {
$controllerClass = $class;
break;
}
Expand Down Expand Up @@ -410,7 +421,7 @@
Logger::panic($routeName, "Controller '" . $controllerName . "' is marked as ignore but also has other scopes");
}

$tagName = implode('_', array_map(fn (string $s) => strtolower($s), Helpers::splitOnUppercaseFollowedByNonUppercase($controllerName)));
$tagName = implode('_', array_map(fn (string $s) => strtolower($s), Helpers::splitOnUppercaseFollowedByNonUppercase(str_replace('\\', '', $controllerName))));
$doc = $controllerClass->getDocComment()?->getText();
if ($doc != null && count(array_filter($tags, fn (array $tag): bool => $tag['name'] === $tagName)) == 0) {
$classDescription = [];
Expand Down
1 change: 1 addition & 0 deletions tests/appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,5 +85,6 @@
['name' => 'Settings#deprecatedRouteAndParameterGet', 'url' => '/api/{apiVersion}/deprecated-route-parameter-get', 'verb' => 'GET', 'requirements' => ['apiVersion' => '(v2)']],
['name' => 'Settings#samePathGet', 'url' => '/api/{apiVersion}/same-path', 'verb' => 'GET', 'requirements' => ['apiVersion' => '(v2)']],
['name' => 'Settings#samePathPost', 'url' => '/api/{apiVersion}/same-path', 'verb' => 'POST', 'requirements' => ['apiVersion' => '(v2)']],
['name' => 'V1\SubDir#subDirRoute', 'url' => '/sub-dir', 'verb' => 'GET'],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Windows style paths in the name?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wasn't even aware that subfolders worked

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wasn't even aware that subfolders worked

Me neither until yesterday 🙈

Windows style paths in the name?

No, it's the backslash from the namespace. Routes never operate based on the path but the namespace (but of course those are very closely related with PSR-4).

],
];
29 changes: 29 additions & 0 deletions tests/lib/Controller/V1/SubDirController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\Notifications\Controller\V1;

use OCP\AppFramework\Http;
use OCP\AppFramework\Http\Attribute\NoAdminRequired;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\OCSController;

class SubDirController extends OCSController {
/**
* A route in a controller in a subdir.
*
* @return DataResponse<Http::STATUS_OK, list<empty>, array{}>
*
* 200: Personal settings updated
*/
#[NoAdminRequired]
public function subDirRoute(): DataResponse {
return new DataResponse();
}
}
59 changes: 59 additions & 0 deletions tests/openapi-full.json
Original file line number Diff line number Diff line change
Expand Up @@ -6533,6 +6533,65 @@
}
}
},
"/ocs/v2.php/apps/notifications/sub-dir": {
"get": {
"operationId": "v1_sub_dir-sub-dir-route",
"summary": "A route in a controller in a subdir.",
"tags": [
"v1_sub_dir"
],
"security": [
{
"bearer_auth": []
},
{
"basic_auth": []
}
],
"parameters": [
{
"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": "Personal settings updated",
"content": {
"application/json": {
"schema": {
"type": "object",
"required": [
"ocs"
],
"properties": {
"ocs": {
"type": "object",
"required": [
"meta",
"data"
],
"properties": {
"meta": {
"$ref": "#/components/schemas/OCSMeta"
},
"data": {}
}
}
}
}
}
}
}
}
}
},
"/index.php/apps/notifications/plain/with-scope": {
"get": {
"operationId": "plain-with-scope",
Expand Down
59 changes: 59 additions & 0 deletions tests/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -994,6 +994,65 @@
}
}
},
"/ocs/v2.php/apps/notifications/sub-dir": {
"get": {
"operationId": "v1_sub_dir-sub-dir-route",
"summary": "A route in a controller in a subdir.",
"tags": [
"v1_sub_dir"
],
"security": [
{
"bearer_auth": []
},
{
"basic_auth": []
}
],
"parameters": [
{
"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": "Personal settings updated",
"content": {
"application/json": {
"schema": {
"type": "object",
"required": [
"ocs"
],
"properties": {
"ocs": {
"type": "object",
"required": [
"meta",
"data"
],
"properties": {
"meta": {
"$ref": "#/components/schemas/OCSMeta"
},
"data": {}
}
}
}
}
}
}
}
}
}
},
"/index.php/apps/notifications/plain/with-scope": {
"get": {
"operationId": "plain-with-scope",
Expand Down
Loading