From 6ed9e44f001c217c0f9433c982348e37fc3f0b7c Mon Sep 17 00:00:00 2001 From: Mark Paronyan Date: Wed, 7 Oct 2026 11:11:59 +0000 Subject: [PATCH 1/2] feat(api): generate unique readable OpenAPI operationIds --- gns3server/api/operation_ids.py | 55 +++++++++++++ gns3server/api/routes/compute/__init__.py | 2 + gns3server/api/routes/controller/__init__.py | 8 +- gns3server/api/server.py | 5 +- tests/api/routes/test_operation_ids.py | 83 ++++++++++++++++++++ 5 files changed, 149 insertions(+), 4 deletions(-) create mode 100644 gns3server/api/operation_ids.py create mode 100644 tests/api/routes/test_operation_ids.py diff --git a/gns3server/api/operation_ids.py b/gns3server/api/operation_ids.py new file mode 100644 index 000000000..ae8c9a2ae --- /dev/null +++ b/gns3server/api/operation_ids.py @@ -0,0 +1,55 @@ +#!/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 . + +""" +Readable and unique OpenAPI operationIds. +""" + +import re +from typing import Callable + +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) -> None: + """ + Registers a catch-all route per HTTP method so that every method gets its own operationId. + """ + + for method in STUB_METHODS: + router.add_api_route("/{path:path}", endpoint, methods=[method], name=f"{name}_{method.lower()}") diff --git a/gns3server/api/routes/compute/__init__.py b/gns3server/api/routes/compute/__init__.py index 934243922..2bd9326a1 100644 --- a/gns3server/api/routes/compute/__init__.py +++ b/gns3server/api/routes/compute/__init__.py @@ -21,6 +21,7 @@ from fastapi import Depends, FastAPI, Request from fastapi.responses import JSONResponse from starlette.exceptions import HTTPException as StarletteHTTPException +from gns3server.api.operation_ids import generate_operation_id from gns3server.compute.compute_error import ( ComputeError, ComputeForbiddenError, @@ -61,6 +62,7 @@ compute_api = FastAPI( title="GNS3 compute API", description="This page describes the private compute API for GNS3. PLEASE DO NOT USE DIRECTLY!", version="3.0.0", + generate_unique_id_function=generate_operation_id, ) compute_api.state.controller_host = None diff --git a/gns3server/api/routes/controller/__init__.py b/gns3server/api/routes/controller/__init__.py index e22d3d507..26fef9a5e 100644 --- a/gns3server/api/routes/controller/__init__.py +++ b/gns3server/api/routes/controller/__init__.py @@ -18,6 +18,7 @@ from fastapi import APIRouter, Depends, HTTPException, status # Check AI Copilot availability from gns3server.agent import AI_COPILOT_AVAILABLE +from gns3server.api.operation_ids import add_stub_routes # Conditionally import AI-dependent routes if AI_COPILOT_AVAILABLE: @@ -32,15 +33,16 @@ else: _copilot_router = APIRouter() _llm_router = APIRouter() - @_chat_router.api_route("/{path:path}", methods=["GET", "POST", "DELETE", "PATCH", "PUT"]) - @_copilot_router.api_route("/{path:path}", methods=["GET", "POST", "DELETE", "PATCH", "PUT"]) - @_llm_router.api_route("/{path:path}", methods=["GET", "POST", "DELETE", "PATCH", "PUT"]) async def ai_not_available(path: str = ""): raise HTTPException( status_code=status.HTTP_501_NOT_IMPLEMENTED, detail="AI Copilot is not available. Install AI dependencies with: pip install gns3-server[ai-features]", ) + add_stub_routes(_chat_router, ai_not_available, "chat_not_available") + add_stub_routes(_copilot_router, ai_not_available, "copilot_not_available") + add_stub_routes(_llm_router, ai_not_available, "llm_not_available") + from . import ( acl, diff --git a/gns3server/api/server.py b/gns3server/api/server.py index 4ea037e83..f14651835 100644 --- a/gns3server/api/server.py +++ b/gns3server/api/server.py @@ -36,6 +36,7 @@ from uvicorn.main import Server as UvicornServer # MCP is an optional feature — import only if dependencies are installed from gns3server.agent import MCP_AVAILABLE +from gns3server.api.operation_ids import add_stub_routes, generate_operation_id from gns3server.api.routes import controller, index from gns3server.api.routes.compute import compute_api from gns3server.controller.controller_error import ( @@ -58,13 +59,14 @@ else: _mcp_router = APIRouter(prefix="/mcp", tags=["MCP"]) - @_mcp_router.api_route("/{path:path}", methods=["GET", "POST", "DELETE", "PATCH", "PUT"]) async def mcp_not_available(path: str = ""): raise HTTPException( status_code=status.HTTP_501_NOT_IMPLEMENTED, detail="MCP is not available. Install AI dependencies with: pip install gns3-server[ai-features]", ) + add_stub_routes(_mcp_router, mcp_not_available, "not_available") + import logging @@ -80,6 +82,7 @@ def get_application() -> FastAPI: version="3.0.0", docs_url=None, redoc_url=None, + generate_unique_id_function=generate_operation_id, ) application.add_middleware( diff --git a/tests/api/routes/test_operation_ids.py b/tests/api/routes/test_operation_ids.py new file mode 100644 index 000000000..26a0905ea --- /dev/null +++ b/tests/api/routes/test_operation_ids.py @@ -0,0 +1,83 @@ +#!/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 . + +import re +import warnings +from collections import Counter + +import pytest +from fastapi import APIRouter, FastAPI +from fastapi.openapi.utils import get_openapi + +from gns3server.api.operation_ids import STUB_METHODS, add_stub_routes, generate_operation_id +from gns3server.api.routes.compute import compute_api +from gns3server.api.server import app as controller_app + +HTTP_METHODS = ("get", "put", "post", "delete", "options", "head", "patch", "trace") + + +def build_operation_ids(application: FastAPI) -> list[str]: + + with warnings.catch_warnings(): + warnings.simplefilter("error") + spec = get_openapi(title="test", version="0", routes=application.routes) + return [ + operation["operationId"] + for path_item in spec["paths"].values() + for method, operation in path_item.items() + if method in HTTP_METHODS + ] + + +@pytest.mark.parametrize("application", [controller_app, compute_api], ids=["controller", "compute"]) +def test_operation_ids_are_unique_and_readable(application: FastAPI) -> None: + + operation_ids = build_operation_ids(application) + assert operation_ids + duplicates = [operation_id for operation_id, count in Counter(operation_ids).items() if count > 1] + assert duplicates == [] + invalid = [operation_id for operation_id in operation_ids if not re.fullmatch(r"[a-z0-9_]+", operation_id)] + assert invalid == [] + + +def test_operation_id_uses_first_tag_and_function_name() -> None: + + router = APIRouter() + + @router.get("/nodes/{node_id}", tags=["Compute nodes", "Other"]) + async def get_node(node_id: str): + return {} + + @router.get("/untagged") + async def list_untagged(): + return {} + + assert generate_operation_id(router.routes[0]) == "compute_nodes_get_node" + assert generate_operation_id(router.routes[1]) == "list_untagged" + + +def test_stub_routes_get_one_operation_id_per_method() -> None: + + async def not_available(path: str = ""): + return {} + + application = FastAPI(generate_unique_id_function=generate_operation_id) + router = APIRouter(tags=["Stub"]) + add_stub_routes(router, not_available, "stub") + application.include_router(router, prefix="/stub") + + assert sorted(build_operation_ids(application)) == sorted(f"stub_stub_{method.lower()}" for method in STUB_METHODS) From 34266d9d7b0fe3de5708bd2ed0c7fa69a1db9c54 Mon Sep 17 00:00:00 2001 From: grossmj Date: Thu, 8 Oct 2026 19:16:57 +0200 Subject: [PATCH 2/2] style: reorder imports for comply with lint --- gns3server/api/server.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gns3server/api/server.py b/gns3server/api/server.py index f18315beb..63da932a6 100644 --- a/gns3server/api/server.py +++ b/gns3server/api/server.py @@ -36,8 +36,8 @@ from uvicorn.main import Server as UvicornServer # MCP is an optional feature — import only if dependencies are installed from gns3server.agent import MCP_AVAILABLE -from gns3server.api.operation_ids import add_stub_routes, generate_operation_id from gns3server.api.errors import customize_openapi_errors, error_response +from gns3server.api.operation_ids import add_stub_routes, generate_operation_id from gns3server.api.routes import controller, index from gns3server.api.routes.compute import compute_api from gns3server.config import Config