support for multi-document yaml in openapi2jsonschema

This commit is contained in:
Yann Hamon 2020-12-28 11:08:05 +01:00
parent 509ad8e997
commit f6e6f7584c

View file

@ -93,36 +93,41 @@ if len(sys.argv) == 0:
exit(1)
for crdFile in sys.argv[1:]:
if crdFile.startswith("http"):
f = urllib.request.urlopen(crdFile)
else:
f = open(crdFile)
with f:
y = yaml.load(f, Loader=yaml.SafeLoader)
filename = ""
schemaJSON = ""
if "spec" in y and "validation" in y["spec"] and "openAPIV3Schema" in y["spec"]["validation"]:
filename = y["spec"]["names"]["kind"].lower()+"_"+y["spec"]["version"].lower()+".json"
if crdFile.startswith("http"):
f = urllib.request.urlopen(crdFile)
else:
f = open(crdFile)
with f:
for y in yaml.load_all(f, Loader=yaml.SafeLoader):
if "kind" not in y:
continue
if y["kind"] != "CustomResourceDefinition":
continue
schema = y["spec"]["validation"]["openAPIV3Schema"]
schema = additional_properties(schema)
schema = replace_int_or_string(schema)
schemaJSON = json.dumps(schema)
elif "spec" in y and "versions" in y["spec"]:
for version in y["spec"]["versions"]:
if "schema" in version and "openAPIV3Schema" in version["schema"]:
filename = y["spec"]["names"]["kind"].lower()+"_"+version["name"].lower()+".json"
filename = ""
schemaJSON = ""
if "spec" in y and "validation" in y["spec"] and "openAPIV3Schema" in y["spec"]["validation"]:
filename = y["spec"]["names"]["kind"].lower()+"_"+y["spec"]["version"].lower()+".json"
schema = version["schema"]["openAPIV3Schema"]
schema = additional_properties(schema)
schema = replace_int_or_string(schema)
schemaJSON = json.dumps(schema)
schema = y["spec"]["validation"]["openAPIV3Schema"]
schema = additional_properties(schema)
schema = replace_int_or_string(schema)
schemaJSON = json.dumps(schema)
elif "spec" in y and "versions" in y["spec"]:
for version in y["spec"]["versions"]:
if "schema" in version and "openAPIV3Schema" in version["schema"]:
filename = y["spec"]["names"]["kind"].lower()+"_"+version["name"].lower()+".json"
# Dealing with user input here..
filename = os.path.basename(filename)
f = open(filename, "w")
f.write(schemaJSON)
f.close()
print("JSON schema written to {filename}".format(filename=filename))
schema = version["schema"]["openAPIV3Schema"]
schema = additional_properties(schema)
schema = replace_int_or_string(schema)
schemaJSON = json.dumps(schema)
# Dealing with user input here..
filename = os.path.basename(filename)
f = open(filename, "w")
f.write(schemaJSON)
f.close()
print("JSON schema written to {filename}".format(filename=filename))
exit(0)