Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Ensure operationIds are unique #114

Merged
merged 2 commits into from
Apr 9, 2024
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
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,
provokateurin marked this conversation as resolved.
Show resolved Hide resolved
...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
Loading