File size: 1,984 Bytes
81e3673
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/env python3
"""Generate OpenAPI specification from FastAPI application."""
import argparse
import json
from pathlib import Path
from fastapi.openapi.utils import get_openapi
from main_api_app import app


def generate_openapi_spec(output_path: str = "openapi.json", version: str = None):
    """Generate OpenAPI spec from FastAPI app.

    Args:
        output_path: Path to write the OpenAPI spec JSON file
        version: Override API version (default: from app)
    """
    spec_version = version or app.version

    openapi_schema = get_openapi(
        title=app.title,
        version=spec_version,
        routes=app.routes,
    )

    # Downgrade OpenAPI version from 3.1.0 to 3.0.3 for openapi-diff compatibility
    # openapi-diff only supports 3.0.x, not 3.1.0
    if openapi_schema.get('openapi') == '3.1.0':
        openapi_schema['openapi'] = '3.0.3'

    output_file = Path(output_path)
    output_file.parent.mkdir(parents=True, exist_ok=True)

    with open(output_file, "w") as f:
        json.dump(openapi_schema, f, indent=2)

    print(f"OpenAPI spec generated: {output_path}")
    print(f"  Title: {openapi_schema['info']['title']}")
    print(f"  Version: {openapi_schema['info']['version']}")
    print(f"  OpenAPI: {openapi_schema['openapi']}")
    print(f"  Endpoints: {len(openapi_schema.get('paths', {}))}")

    return openapi_schema


def main():
    """CLI entry point for OpenAPI spec generation."""
    parser = argparse.ArgumentParser(
        description="Generate OpenAPI spec from FastAPI app"
    )
    parser.add_argument(
        "-o", "--output",
        default="backend/openapi.json",
        help="Output path for OpenAPI spec (default: backend/openapi.json)"
    )
    parser.add_argument(
        "-v", "--version",
        help="Override API version"
    )

    args = parser.parse_args()

    generate_openapi_spec(
        output_path=args.output,
        version=args.version
    )


if __name__ == "__main__":
    main()