Skip to content

Commit

Permalink
Merge pull request #192 from nextcloud/feat/generate-spec/ignore-inde…
Browse files Browse the repository at this point in the history
…x-routes-by-default

feat(generate-spec): Ignore index.php routes by default
  • Loading branch information
provokateurin authored Dec 17, 2024
2 parents a644eac + 492cfc7 commit c0761cb
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 9 deletions.
20 changes: 11 additions & 9 deletions generate-spec.php
Original file line number Diff line number Diff line change
Expand Up @@ -463,18 +463,11 @@
Logger::panic($routeName, 'Route is marked as ignore but also has other scopes');
}

if (in_array('ignore', $scopes, true)) {
if (count($scopes) === 1) {
Logger::debug($routeName, 'Route ignored because of OpenAPI attribute');
continue;
}

Logger::panic($routeName, 'Route is marked as ignore but also has other scopes');
}

if ($scopes === []) {
if ($controllerScopes !== []) {
$scopes = $controllerScopes;
} elseif (!$isOCS) {
$scopes = ['ignore'];
} elseif ($isExApp) {
$scopes = ['ex_app'];
} elseif ($isAdmin) {
Expand All @@ -484,6 +477,15 @@
}
}

if (in_array('ignore', $scopes, true)) {
if (count($scopes) === 1) {
Logger::debug($routeName, 'Route ignored because of OpenAPI attribute');
continue;
}

Logger::panic($routeName, 'Route is marked as ignore but also has other scopes');
}

$routeTags = Helpers::getOpenAPIAttributeTagsByScope($classMethod, $routeName, $tagName, reset($scopes));

if ($isOCS && !array_key_exists('OCSMeta', $schemas)) {
Expand Down
40 changes: 40 additions & 0 deletions tests/lib/Controller/PlainController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

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

namespace OCA\Notifications\Controller;

use OCP\AppFramework\Controller;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\Attribute\FrontpageRoute;
use OCP\AppFramework\Http\Attribute\NoCSRFRequired;
use OCP\AppFramework\Http\Attribute\OpenAPI;
use OCP\AppFramework\Http\Response;

class PlainController extends Controller {
#[NoCSRFRequired]
#[FrontpageRoute(verb: 'GET', url: '/plain/ignored')]
public function ignored(): Response {
return new Response();
}


/**
* Route with manual scope to not get ignored
*
* @return Response<Http::STATUS_OK, array{}>
*
* 200: Response returned
*/
#[NoCSRFRequired]
#[OpenAPI(scope: OpenAPI::SCOPE_DEFAULT)]
#[FrontpageRoute(verb: 'GET', url: '/plain/with-scope')]
public function withScope(): Response {
return new Response();
}
}
23 changes: 23 additions & 0 deletions tests/openapi-full.json
Original file line number Diff line number Diff line change
Expand Up @@ -5485,6 +5485,29 @@
}
}
},
"/index.php/apps/notifications/plain/with-scope": {
"get": {
"operationId": "plain-with-scope",
"summary": "Route with manual scope to not get ignored",
"description": "This endpoint requires admin access",
"tags": [
"plain"
],
"security": [
{
"bearer_auth": []
},
{
"basic_auth": []
}
],
"responses": {
"200": {
"description": "Response returned"
}
}
}
},
"/ocs/v2.php/apps/notifications/api/{apiVersion}/ex-app-attribute": {
"post": {
"operationId": "ex_app_settings-ex-app-scope-attribute",
Expand Down
23 changes: 23 additions & 0 deletions tests/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -797,6 +797,29 @@
}
}
}
},
"/index.php/apps/notifications/plain/with-scope": {
"get": {
"operationId": "plain-with-scope",
"summary": "Route with manual scope to not get ignored",
"description": "This endpoint requires admin access",
"tags": [
"plain"
],
"security": [
{
"bearer_auth": []
},
{
"basic_auth": []
}
],
"responses": {
"200": {
"description": "Response returned"
}
}
}
}
},
"tags": []
Expand Down

0 comments on commit c0761cb

Please sign in to comment.