schema: allow generating jsonschema with no $ref tags in it

Signed-off-by: John Crispin <john@phrozen.org>
This commit is contained in:
John Crispin
2021-06-05 08:20:22 +02:00
parent 86f618203f
commit 022315bc49
2 changed files with 19 additions and 11 deletions

View File

@@ -3,9 +3,10 @@
set -e
set -x
./merge-schema.py schema schema ucentral.yml ucentral.schema.json 1
./merge-schema.py schema schema ucentral.yml ucentral.schema.pretty.json 0
./merge-schema.py state state state.yml ucentral.state.pretty.json 0
./merge-schema.py schema schema ucentral.yml ucentral.schema.json 1 1
./merge-schema.py schema schema ucentral.yml ucentral.schema.pretty.json 0 1
./merge-schema.py schema schema ucentral.yml ucentral.schema.full.json 0 0
./merge-schema.py state state state.yml ucentral.state.pretty.json 0 1
mkdir -p docs
generate-schema-doc --config expand_buttons=true ucentral.schema.pretty.json docs/ucentral-schema.html
generate-schema-doc --config expand_buttons=true ucentral.state.pretty.json docs/ucentral-state.html

View File

@@ -22,25 +22,30 @@ def schema_load(filename):
except yaml.YAMLError as exc:
print(exc)
def schema_compile(input, output, definitions, tiny):
def schema_compile(input, output, definitions, tiny, refs):
for k in input:
if tiny and (k == "description" or k == "uc-example"):
continue
if isinstance(input[k], dict):
if k not in output:
output[k] = {}
schema_compile(input[k], output[k], definitions, tiny)
schema_compile(input[k], output[k], definitions, tiny, refs)
elif k == "$ref" and input[k].startswith("https://"):
name = entity_name(input[k])
definitions[name] = schema_compile(schema_load(schema_filename(input[k])), {}, definitions, tiny)
output["$ref"] = "#/$defs/{}".format(name)
compiled = schema_compile(schema_load(schema_filename(input[k])), {}, definitions, tiny, refs)
if refs:
definitions[name] = compiled
output["$ref"] = "#/$defs/{}".format(name)
else:
for i in compiled:
output[i] = compiled[i]
elif k == "$ref" and not tiny:
output["properties"] = {"reference": {"type": "string"}}
elif k == "anyOf" or k == "oneOf" or k == "allOf":
r = []
for v in input[k]:
o = {}
schema_compile(v, o, definitions, tiny)
schema_compile(v, o, definitions, tiny, refs)
r.append(o)
output[k] = r
else:
@@ -50,12 +55,14 @@ def schema_compile(input, output, definitions, tiny):
def schema_generate():
with open(sys.argv[4], 'w') as outfile:
tiny = int(sys.argv[5])
refs = int(sys.argv[6])
defs = {}
schema = schema_compile(schema_load(sys.argv[3]), {}, defs, tiny)
schema["$defs"] = defs
schema = schema_compile(schema_load(sys.argv[3]), {}, defs, tiny, refs)
if refs:
schema["$defs"] = defs
json.dump(schema, outfile, ensure_ascii = tiny and False or True, indent = tiny and 0 or 4)
if len(sys.argv) != 6:
if len(sys.argv) != 7:
raise Exception("Invalid parameters");
schema_generate()