test_openapi_operation_ids.py 733 B

123456789101112131415161718192021
  1. """Every operation in the OpenAPI schema has its own operation id.
  2. A route registered with api_route(methods=[A, B]) gives both methods the
  3. same id, and FastAPI only warns about it while it builds /openapi.json.
  4. Register one decorator per method instead.
  5. """
  6. import warnings
  7. from fastapi.openapi.utils import get_openapi
  8. from backend.app.main import app
  9. def test_operation_ids_are_unique():
  10. with warnings.catch_warnings():
  11. warnings.simplefilter("error")
  12. schema = get_openapi(title=app.title, version=app.version, routes=app.routes)
  13. ids = [op["operationId"] for path in schema["paths"].values() for op in path.values()]
  14. assert len(ids) == len(set(ids)), sorted({i for i in ids if ids.count(i) > 1})