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>
This commit is contained in:
tikD1337andClaude Opus 5.5 committed 2026-10-26 10:35:12 +01:00
1 parent ac01ee6190
commit 09b8420354
3 files changed
+117 -24

No files matched your search

+8 -5
View File
@@ -20,7 +20,7 @@ Readable and unique OpenAPI operationIds.
"""
import re
from typing import Callable
from typing import Callable, Sequence
from fastapi import APIRouter
from fastapi.routing import APIRoute
@@ -46,10 +46,13 @@ def generate_operation_id(route: APIRoute) -> str:
return name
def add_stub_routes(router: APIRouter, endpoint: Callable, name: str) -> None:
def add_stub_routes(router: APIRouter, endpoint: Callable, name: str, paths: Sequence[str] = ("/{path:path}",)) -> None:
"""
Registers a catch-all route per HTTP method so that every method gets its own operationId.
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 method in STUB_METHODS:
router.add_api_route("/{path:path}", endpoint, methods=[method], name=f"{name}_{method.lower()}")
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()}")
+32 -19
View File
@@ -21,6 +21,37 @@ from gns3server.agent import AI_COPILOT_AVAILABLE
from gns3server.api.operation_ids import add_stub_routes
from gns3server.config import Config
# The LLM model configuration routes share the /access prefix with users, groups and API keys,
# so their stub must cover only their own paths: a catch-all under /access would answer 501 for
# every /access route included after it (e.g. creating an API key).
LLM_STUB_PATHS = (
"/users/{user_id}/llm-model-configs{rest:path}",
"/groups/{group_id}/llm-model-configs{rest:path}",
)
async def ai_not_available():
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]",
)
def build_ai_stub_routers() -> tuple[APIRouter, APIRouter, APIRouter]:
"""
Stub routers (chat, copilot, LLM model configurations) that answer 501
when the AI dependencies are not installed.
"""
chat_router = APIRouter()
copilot_router = APIRouter()
llm_router = APIRouter()
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", paths=LLM_STUB_PATHS)
return chat_router, copilot_router, llm_router
# Conditionally import AI-dependent routes
if AI_COPILOT_AVAILABLE:
from . import chat, copilot, llm_model_configs
@@ -29,25 +60,7 @@ if AI_COPILOT_AVAILABLE:
_copilot_router = copilot.router
_llm_router = llm_model_configs.router
else:
# Create stub routers that return 501 for all AI endpoints
_chat_router = APIRouter()
_copilot_router = APIRouter()
_llm_router = APIRouter()
@_chat_router.api_route("/{path:path}", methods=["GET", "POST", "DELETE", "PATCH", "PUT"], include_in_schema=False)
@_copilot_router.api_route(
"/{path:path}", methods=["GET", "POST", "DELETE", "PATCH", "PUT"], include_in_schema=False
)
@_llm_router.api_route("/{path:path}", methods=["GET", "POST", "DELETE", "PATCH", "PUT"], include_in_schema=False)
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")
_chat_router, _copilot_router, _llm_router = build_ai_stub_routers()
from . import (