-
Notifications
You must be signed in to change notification settings - Fork 0
/
transform.py
116 lines (96 loc) · 3.92 KB
/
transform.py
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import json
def transform_security(data):
def update_security(obj):
if isinstance(obj, dict):
if "security" in obj and isinstance(obj["security"], list):
if len(obj["security"]) == 1 and "HTTPBearer" in obj["security"][0]:
obj["security"] = [{"HTTPBearer": []}, {}]
for value in obj.values():
update_security(value)
elif isinstance(obj, list):
for item in obj:
update_security(item)
update_security(data)
return data
def transform_pagination_fields(data):
pagination_fields = ["page", "pages", "size", "total"]
def update_fields(obj):
if isinstance(obj, dict):
for field in pagination_fields:
if field in obj and isinstance(obj[field], dict):
field_data = obj[field]
if "anyOf" in field_data:
for option in field_data["anyOf"]:
if option.get("type") == "integer":
obj[field] = {
"type": "integer",
"minimum": option.get("minimum", 0.0),
"title": field_data.get(
"title", field.capitalize()
),
}
break
for value in obj.values():
update_fields(value)
elif isinstance(obj, list):
for item in obj:
update_fields(item)
update_fields(data)
return data
def transform_metadata(data):
def update_metadata(obj):
if isinstance(obj, dict):
if "metadata" in obj and isinstance(obj["metadata"], dict):
metadata = obj["metadata"]
if "anyOf" in metadata:
for option in metadata["anyOf"]:
if option.get("type") == "object":
option["additionalProperties"] = True
elif metadata.get("type") == "object":
metadata["additionalProperties"] = True
for value in obj.values():
update_metadata(value)
elif isinstance(obj, list):
for item in obj:
update_metadata(item)
def update_filter(obj):
if isinstance(obj, dict):
if "filter" in obj and isinstance(obj["filter"], dict):
filter = obj["filter"]
if "anyOf" in filter:
for option in filter["anyOf"]:
if option.get("type") == "object":
option["additionalProperties"] = True
elif filter.get("type") == "object":
filter["additionalProperties"] = True
for value in obj.values():
update_filter(value)
elif isinstance(obj, list):
for item in obj:
update_filter(item)
update_metadata(data)
update_filter(data)
return data
def transform_openapi(input_file, output_file):
# Read the input file
with open(input_file, "r") as f:
data = json.load(f)
# Apply all transformations
data = transform_security(data)
data = transform_pagination_fields(data)
data = transform_metadata(data)
# Write the transformed data to the output file
with open(output_file, "w") as f:
json.dump(data, f, indent=2)
def normalize_json(input_file, output_file):
with open(input_file, "r") as f:
data = json.load(f)
with open(output_file, "w") as f:
json.dump(data, f, indent=2)
# Usage
normalize_json("openapi.json", "openapi.json")
input_file = "openapi-new.json"
output_file = "output.json"
transform_openapi(input_file, output_file)
normalize_json(output_file, output_file)
print(f"Transformation complete. Output written to {output_file}")