-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_openapi.py
More file actions
89 lines (73 loc) · 2 KB
/
generate_openapi.py
File metadata and controls
89 lines (73 loc) · 2 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
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# /// script
# dependencies = [
# "ruamel-yaml",
# ]
# ///
import os
import sys
from ruamel.yaml import YAML
from pathlib import Path
from typing import Dict, Any
import tempfile
override = {
"info": {
"title": "Tilebox API",
"version": "1.0.0",
},
"servers": [
{
"url": "https://api.tilebox.com",
},
],
"components": {
"schemas": {
"connect-protocol-version": {
"default": 1,
},
},
"securitySchemes": {
"bearerAuth": {
"type": "http",
"scheme": "bearer",
},
},
},
"security": [
{
"bearerAuth": [],
},
],
}
def recursive_merge(base: Dict[Any, Any], update: Dict[Any, Any]):
for key, value in update.items():
if key in base and isinstance(base[key], dict) and isinstance(value, dict):
# Both values are dictionaries, merge recursively
recursive_merge(base[key], value)
else:
# Either key doesn't exist in base, or one of the values is not a dict
base[key] = value
def main() -> None:
output = "openapi.yaml"
if len(sys.argv) == 2:
output = sys.argv[1]
elif len(sys.argv) != 1:
print("Usage: uv run generate-openapi.py <output>")
sys.exit(1)
tmpdir = tempfile.TemporaryDirectory()
print(f"Generating protobuf to {tmpdir.name}")
os.system(f"buf generate -o {tmpdir.name}")
yaml = YAML(typ="safe")
yaml.default_flow_style = False
merged_yaml = {}
for path in Path(tmpdir.name).glob("**/*.yaml"):
print(f"Merging {path}")
with open(path) as f:
data = yaml.load(f.read())
recursive_merge(merged_yaml, data)
recursive_merge(merged_yaml, override)
tmpdir.cleanup()
print(f"Writing merged output to {output}")
with open(output, "w") as out:
yaml.dump(merged_yaml, out)
if __name__ == "__main__":
main()