-
Notifications
You must be signed in to change notification settings - Fork 170
Expand file tree
/
Copy pathgenerate_openapi.py
More file actions
43 lines (34 loc) · 1.32 KB
/
generate_openapi.py
File metadata and controls
43 lines (34 loc) · 1.32 KB
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
from pathlib import Path
from django.core.management import BaseCommand, call_command
class Command(BaseCommand):
"""Generate the OpenAPI schema file from DRF endpoints."""
help = "Generate openapi.yaml using drf-spectacular."
def add_arguments(self, parser):
"""Add optional CLI arguments."""
parser.add_argument(
"--file",
default=None,
help="Output path for the schema file. Defaults to repository openapi.yaml.", # noqa: E501
)
parser.add_argument(
"--validate",
action="store_true",
help="Validate generated schema during export.",
)
def handle(self, *args, **options): # noqa: ARG002
"""Generate OpenAPI file using spectacular command."""
if options["file"]:
output_path = Path(options["file"]).expanduser().resolve()
else:
repo_root = Path(__file__).resolve().parents[4]
output_path = repo_root / "openapi.yaml"
output_path.parent.mkdir(parents=True, exist_ok=True)
call_command(
"spectacular",
file=str(output_path),
validate=options["validate"],
color=True,
)
self.stdout.write(
self.style.SUCCESS(f"OpenAPI schema generated: {output_path}"),
)