feat(api): generate unique readable OpenAPI operationIds

This commit is contained in:
Mark Paronyan committed 2026-10-07 15:53:00 +03:00
1 parent 2fb7056cb9
commit 6ed9e44f00
5 files changed
+149 -4

No files matched your search

+55
View File
@@ -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 <http://www.gnu.org/licenses/>.
"""
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()}")
@@ -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
+5 -3
View File
@@ -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,
+4 -1
View File
@@ -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(