Json-Python-Server/generate_openapi.py

29 lines
734 B
Python
Raw Normal View History

2026-01-29 18:18:32 +08:00
import os
import sys
import json
from pathlib import Path
# Add project root to path to ensure imports work
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
try:
from app.main import app
print("Successfully imported FastAPI app.")
except ImportError as e:
print(f"Error importing app: {e}")
sys.exit(1)
def generate_openapi_json():
openapi_schema = app.openapi()
output_path = Path("openapi.json")
with open(output_path, "w", encoding="utf-8") as f:
json.dump(openapi_schema, f, indent=2, ensure_ascii=False)
print(f"OpenAPI documentation generated at: {output_path.absolute()}")
if __name__ == "__main__":
generate_openapi_json()