Skip to content

Commit

Permalink
Merge pull request #114 from nextcloud/fix/non-unique-operation-ids
Browse files Browse the repository at this point in the history
  • Loading branch information
provokateurin authored Apr 9, 2024
2 parents 570cc3e + 574fb44 commit aa4c24f
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 14 deletions.
26 changes: 18 additions & 8 deletions generate-spec
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,8 @@ if (count($parsedRoutes) === 0) {
Logger::warning("Routes", "No routes were loaded");
}

$operationIds = [];

foreach ($parsedRoutes as $key => $value) {
$isOCS = $key === "ocs";
$isIndex = $key === "routes";
Expand Down Expand Up @@ -523,13 +525,26 @@ foreach ($parsedRoutes as $key => $value) {
continue;
}

$operationId = [
$tagName,
...Helpers::splitOnUppercaseFollowedByNonUppercase($methodName)
];
if ($postfix !== null) {
$operationId[] = $postfix;
}
$operationId = strtolower(implode("-", $operationId));

if (in_array($operationId, $operationIds, true)) {
Logger::panic($routeName, 'Route is not unique! If you want to have two routes pointing to the same controller method you must specify a postfix on at least one of the routes.');
}
$operationIds[] = $operationId;

foreach ($scopes as $scope) {
$routes[$scope] ??= [];
$routes[$scope][] = new Route(
$routeName,
$routeTags[$scope] ?? [$tagName],
$methodName,
$postfix,
$operationId,
$verb,
$url,
$requirements,
Expand Down Expand Up @@ -714,11 +729,6 @@ foreach ($routes as $scope => $scopeRoutes) {
$mergedResponses[$statusCode] = $response;
}

$operationId = [...$route->tags, ...Helpers::splitOnUppercaseFollowedByNonUppercase($route->methodName)];
if ($route->postfix != null) {
$operationId[] = $route->postfix;
}

$security = [];
if ($route->isPublic) {
// Add empty authentication, meaning that it's optional. We can't know if there is a difference in behaviour for authenticated vs. unauthenticated access on public pages (e.g. capabilities)
Expand All @@ -734,7 +744,7 @@ foreach ($routes as $scope => $scopeRoutes) {
}

$operation = [
"operationId" => strtolower(implode("-", $operationId)),
"operationId" => $route->operationId,
];
if ($route->controllerMethod->summary !== null) {
$operation["summary"] = $route->controllerMethod->summary;
Expand Down
3 changes: 1 addition & 2 deletions src/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ class Route {
public function __construct(
public string $name,
public array $tags,
public string $methodName,
public ?string $postfix,
public string $operationId,
public string $verb,
public string $url,
public array $requirements,
Expand Down
4 changes: 2 additions & 2 deletions tests/openapi-administration.json
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@
},
"/ocs/v2.php/apps/notifications/api/{apiVersion}/moved-with-tag": {
"post": {
"operationId": "settings-admin-settings-moved-to-settings-tag",
"operationId": "admin_settings-moved-to-settings-tag",
"summary": "Route in default scope with tags",
"description": "This endpoint requires admin access",
"tags": [
Expand Down Expand Up @@ -227,7 +227,7 @@
},
"/ocs/v2.php/apps/notifications/api/{apiVersion}/moved-with-unnamed-tag": {
"post": {
"operationId": "settings-admin-settings-moved-to-settings-tag-unnamed",
"operationId": "admin_settings-moved-to-settings-tag-unnamed",
"summary": "Route in default scope with tags but without named parameters on the attribute",
"description": "This endpoint requires admin access",
"tags": [
Expand Down
4 changes: 2 additions & 2 deletions tests/openapi-full.json
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@
},
"/ocs/v2.php/apps/notifications/api/{apiVersion}/moved-with-tag": {
"post": {
"operationId": "settings-admin-settings-moved-to-settings-tag",
"operationId": "admin_settings-moved-to-settings-tag",
"summary": "Route in default scope with tags",
"description": "This endpoint requires admin access",
"tags": [
Expand Down Expand Up @@ -354,7 +354,7 @@
},
"/ocs/v2.php/apps/notifications/api/{apiVersion}/moved-with-unnamed-tag": {
"post": {
"operationId": "settings-admin-settings-moved-to-settings-tag-unnamed",
"operationId": "admin_settings-moved-to-settings-tag-unnamed",
"summary": "Route in default scope with tags but without named parameters on the attribute",
"description": "This endpoint requires admin access",
"tags": [
Expand Down

0 comments on commit aa4c24f

Please sign in to comment.