export_openapi.py 844 B

12345678910111213141516171819202122232425262728293031
  1. """Dump the FastAPI OpenAPI schema to apps/api/openapi.json.
  2. This must NOT require a database connection: importing ``app.main`` only builds
  3. the app and routes; the DB engine is created lazily. Run from apps/api:
  4. python -m scripts.export_openapi
  5. # or
  6. python scripts/export_openapi.py
  7. """
  8. from __future__ import annotations
  9. import json
  10. import sys
  11. from pathlib import Path
  12. # Allow running as a bare script (python scripts/export_openapi.py).
  13. sys.path.insert(0, str(Path(__file__).resolve().parent.parent))
  14. from app.main import app # noqa: E402
  15. def main() -> None:
  16. schema = app.openapi()
  17. out_path = Path(__file__).resolve().parent.parent / "openapi.json"
  18. out_path.write_text(json.dumps(schema, indent=2, ensure_ascii=False), encoding="utf-8")
  19. print(f"wrote {out_path}")
  20. if __name__ == "__main__":
  21. main()