forked from pawel-kow/epp-xsd-to-json-schema
-
Notifications
You must be signed in to change notification settings - Fork 0
/
append_schema_to_openapi.py
30 lines (25 loc) · 979 Bytes
/
append_schema_to_openapi.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
import yaml
import json
from yaml import Loader, Dumper
input_file_openapi = "output/openapi.yaml"
input_file_schemas = "output/epp-schema.json"
output_file_openapi = "output/openapi_merged.yaml"
def change_paths(obj):
if isinstance(obj, list):
for x in obj:
change_paths(x)
elif isinstance(obj, dict):
for l in obj:
if l == "$ref":
obj[l] = obj[l].replace("#/$defs/", "#/components/schemas/", ).replace("epp-schema.yaml", "").replace(":", "_")
else:
change_paths(obj[l])
with open(input_file_openapi, "r") as f:
openapi = yaml.load(f, Loader=Loader)
with open(input_file_schemas, "r") as f:
epp_schema = json.load(f)
openapi.setdefault("components", {}).setdefault("schemas", {})
openapi["components"]["schemas"].update(epp_schema["$defs"])
change_paths(openapi["components"]["schemas"])
with open(output_file_openapi, "w") as f:
yaml.dump(openapi, f, Dumper=Dumper)