Update the check_examples.py script to resolve references

The script previously wasn't chasing down valid references in examples.
pull/1413/head
Travis Ralston 6 years ago
parent 7314405904
commit 797aca87bd

@ -56,7 +56,8 @@ def check_parameter(filepath, request, parameter):
# Setting the 'id' tells jsonschema where the file is so that it # Setting the 'id' tells jsonschema where the file is so that it
# can correctly resolve relative $ref references in the schema # can correctly resolve relative $ref references in the schema
schema['id'] = fileurl schema['id'] = fileurl
resolver = jsonschema.RefResolver(filepath, schema, handlers={"file": load_yaml}) example = resolve_references(filepath, example)
resolver = jsonschema.RefResolver(filepath, schema, handlers={"file": load_file})
jsonschema.validate(example, schema, resolver=resolver) jsonschema.validate(example, schema, resolver=resolver)
except Exception as e: except Exception as e:
raise ValueError("Error validating JSON schema for %r" % ( raise ValueError("Error validating JSON schema for %r" % (
@ -76,7 +77,8 @@ def check_response(filepath, request, code, response):
# Setting the 'id' tells jsonschema where the file is so that it # Setting the 'id' tells jsonschema where the file is so that it
# can correctly resolve relative $ref references in the schema # can correctly resolve relative $ref references in the schema
schema['id'] = fileurl schema['id'] = fileurl
resolver = jsonschema.RefResolver(filepath, schema, handlers={"file": load_yaml}) example = resolve_references(filepath, example)
resolver = jsonschema.RefResolver(filepath, schema, handlers={"file": load_file})
jsonschema.validate(example, schema, resolver=resolver) jsonschema.validate(example, schema, resolver=resolver)
except Exception as e: except Exception as e:
raise ValueError("Error validating JSON schema for %r %r" % ( raise ValueError("Error validating JSON schema for %r %r" % (
@ -104,6 +106,27 @@ def check_swagger_file(filepath):
check_response(filepath, request, code, response) check_response(filepath, request, code, response)
def resolve_references(path, schema):
if isinstance(schema, dict):
# do $ref first
if '$ref' in schema:
value = schema['$ref']
path = os.path.abspath(os.path.join(os.path.dirname(path), value))
ref = load_file("file://" + path)
result = resolve_references(path, ref)
del schema['$ref']
else:
result = {}
for key, value in schema.items():
result[key] = resolve_references(path, value)
return result
elif isinstance(schema, list):
return [resolve_references(path, value) for value in schema]
else:
return schema
def load_yaml(path): def load_yaml(path):
if not path.startswith("file:///"): if not path.startswith("file:///"):
raise Exception("Bad ref: %s" % (path,)) raise Exception("Bad ref: %s" % (path,))
@ -112,6 +135,24 @@ def load_yaml(path):
return yaml.load(f) return yaml.load(f)
def load_json(path):
if not path.startswith("file:///"):
raise Exception("Bad ref: %s" % (path,))
path = path[len("file://"):]
with open(path, "r") as f:
return json.load(f)
def load_file(path):
print("Loading reference: %s" % path)
if path.endswith(".json"):
return load_json(path)
else:
# We have to assume it's YAML because some of the YAML examples
# do not have file extensions.
return load_yaml(path)
if __name__ == '__main__': if __name__ == '__main__':
paths = sys.argv[1:] paths = sys.argv[1:]
if not paths: if not paths:

Loading…
Cancel
Save