Files
gns3-server/gns3server/api/operation_ids.py
T
tikD1337andClaude Opus 5.5 09b8420354 fix(api): keep the LLM stub from shadowing /access routes without AI extras
Without the AI dependencies the LLM model configuration router is replaced
by a stub that answers 501. It was a catch-all ("/{path:path}") included
under the /access prefix, so it also answered every /access route included
after it: creating an API key (POST /v3/access/api-keys) failed with "AI
Copilot is not available", which left API keys unusable on servers without
the AI extras.

The LLM stub now covers only the LLM model configuration paths, under
/access/users/{user_id}/ and /access/groups/{group_id}/. The stub routers
are built by build_ai_stub_routers() so tests can check them with or
without the extras, and add_stub_routes() takes the paths to stub.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-26 10:35:12 +01:00

59 lines
1.9 KiB
Python

#!/usr/bin/env python
#
# Copyright (C) 2026 GNS3 Technologies Inc.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
Readable and unique OpenAPI operationIds.
"""
import re
from typing import Callable, Sequence
from fastapi import APIRouter
from fastapi.routing import APIRoute
STUB_METHODS = ("GET", "POST", "DELETE", "PATCH", "PUT")
def _slug(value: str) -> str:
return re.sub(r"[^a-z0-9]+", "_", value.lower()).strip("_")
def generate_operation_id(route: APIRoute) -> str:
"""
Builds an operationId such as "nodes_get_node" from the first tag and the route name.
"""
name = _slug(route.name)
if route.tags:
tag = _slug(str(route.tags[0]))
if tag:
return f"{tag}_{name}"
return name
def add_stub_routes(router: APIRouter, endpoint: Callable, name: str, paths: Sequence[str] = ("/{path:path}",)) -> None:
"""
Registers a stub route per path and HTTP method so that every method gets its own operationId.
The default path is a catch-all for everything under the router prefix.
"""
for index, path in enumerate(paths):
suffix = f"_{index}" if len(paths) > 1 else ""
for method in STUB_METHODS:
router.add_api_route(path, endpoint, methods=[method], name=f"{name}{suffix}_{method.lower()}")