Files
romm/backend/utils/openapi.py
Georges-Antoine Assi 808239db37 refactor(streaming): generate the remaining streaming API types from the backend
The launch socket payloads, the 409 busy detail and the card-import answer
were hand-written in services/api/streaming.ts and could drift from the
backend models they mirror.

- Publish Socket.IO payload models into the OpenAPI components
  (utils/openapi.py), starting with the three streaming launch events.
- Raise the busy 409 from a ContainerBusyDetail model and declare it on
  the game and desktop claim routes.
- Derive MemoryCardImport from ClaimStreamingSessionRequest, and type the
  list and control calls with their generated response models.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-24 20:57:36 -04:00

29 lines
1.0 KiB
Python

from collections.abc import Sequence
from typing import Any
from fastapi import FastAPI
from pydantic import BaseModel
from pydantic.json_schema import models_json_schema
def publish_socket_payloads(app: FastAPI, models: Sequence[type[BaseModel]]) -> None:
"""Add Socket.IO payload models to the app's OpenAPI components.
No route references them, so FastAPI would leave them out, and the
frontend's generated types would lack the shapes its socket handlers read.
"""
build_schema = app.openapi
def openapi() -> dict[str, Any]:
if app.openapi_schema is None:
schema = build_schema()
_, definitions = models_json_schema(
[(model, "serialization") for model in models],
ref_template="#/components/schemas/{model}",
)
components = schema.setdefault("components", {}).setdefault("schemas", {})
components.update(definitions.get("$defs", {}))
return app.openapi_schema
app.openapi = openapi # type: ignore[method-assign]