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) exit(1)
for crdFile in sys.argv[1:]: for crdFile in sys.argv[1:]:
if crdFile.startswith("http"): if crdFile.startswith("http"):
f = urllib.request.urlopen(crdFile) f = urllib.request.urlopen(crdFile)
else: else:
f = open(crdFile) f = open(crdFile)
with f: with f:
y = yaml.load(f, Loader=yaml.SafeLoader) for y in yaml.load_all(f, Loader=yaml.SafeLoader):
filename = "" if "kind" not in y:
schemaJSON = "" continue
if "spec" in y and "validation" in y["spec"] and "openAPIV3Schema" in y["spec"]["validation"]: if y["kind"] != "CustomResourceDefinition":
filename = y["spec"]["names"]["kind"].lower()+"_"+y["spec"]["version"].lower()+".json" continue
schema = y["spec"]["validation"]["openAPIV3Schema"] filename = ""
schema = additional_properties(schema) schemaJSON = ""
schema = replace_int_or_string(schema) if "spec" in y and "validation" in y["spec"] and "openAPIV3Schema" in y["spec"]["validation"]:
schemaJSON = json.dumps(schema) filename = y["spec"]["names"]["kind"].lower()+"_"+y["spec"]["version"].lower()+".json"
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"
schema = version["schema"]["openAPIV3Schema"] schema = y["spec"]["validation"]["openAPIV3Schema"]
schema = additional_properties(schema) schema = additional_properties(schema)
schema = replace_int_or_string(schema) schema = replace_int_or_string(schema)
schemaJSON = json.dumps(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.. schema = version["schema"]["openAPIV3Schema"]
filename = os.path.basename(filename) schema = additional_properties(schema)
f = open(filename, "w") schema = replace_int_or_string(schema)
f.write(schemaJSON) schemaJSON = json.dumps(schema)
f.close()
print("JSON schema written to {filename}".format(filename=filename)) # 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) exit(0)