From 31687608dc90c6a77f4e39531576d59edd505d00 Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Sun, 29 Jul 2018 12:16:10 +0900 Subject: [PATCH 1/5] Factor out common code --- api/check_examples.py | 53 ++++++++++++++++--------------------------- 1 file changed, 19 insertions(+), 34 deletions(-) diff --git a/api/check_examples.py b/api/check_examples.py index fb9430b6..81a9298e 100755 --- a/api/check_examples.py +++ b/api/check_examples.py @@ -43,22 +43,25 @@ except ImportError as e: raise +def check_schema(filepath, example, schema): + # Setting the 'id' tells jsonschema where the file is so that it + # can correctly resolve relative $ref references in the schema + schema['id'] = "file://" + os.path.abspath(filepath) + example = resolve_references(filepath, example) + resolver = jsonschema.RefResolver(filepath, schema, handlers={"file": load_file}) + jsonschema.validate(example, schema, resolver=resolver) + + def check_parameter(filepath, request, parameter): schema = parameter.get("schema") example = schema.get('example') - fileurl = "file://" + os.path.abspath(filepath) if example and schema: try: print ("Checking request schema for: %r %r" % ( filepath, request )) - # Setting the 'id' tells jsonschema where the file is so that it - # can correctly resolve relative $ref references in the schema - schema['id'] = fileurl - example = resolve_references(filepath, example) - resolver = jsonschema.RefResolver(filepath, schema, handlers={"file": load_file}) - jsonschema.validate(example, schema, resolver=resolver) + check_schema(filepath, example, schema) except Exception as e: raise ValueError("Error validating JSON schema for %r" % ( request @@ -68,18 +71,12 @@ def check_parameter(filepath, request, parameter): def check_response(filepath, request, code, response): example = response.get('examples', {}).get('application/json') schema = response.get('schema') - fileurl = "file://" + os.path.abspath(filepath) if example and schema: try: print ("Checking response schema for: %r %r %r" % ( filepath, request, code )) - # Setting the 'id' tells jsonschema where the file is so that it - # can correctly resolve relative $ref references in the schema - schema['id'] = fileurl - example = resolve_references(filepath, example) - resolver = jsonschema.RefResolver(filepath, schema, handlers={"file": load_file}) - jsonschema.validate(example, schema, resolver=resolver) + check_schema(filepath, example, schema) except Exception as e: raise ValueError("Error validating JSON schema for %r %r" % ( request, code @@ -127,30 +124,18 @@ def resolve_references(path, schema): return schema -def load_yaml(path): - if not path.startswith("file:///"): - raise Exception("Bad ref: %s" % (path,)) - path = path[len("file://"):] - with open(path, "r") as f: - return yaml.load(f) - - -def load_json(path): +def load_file(path): + print("Loading reference: %s" % 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 path.endswith(".json"): + return json.load(f) + else: + # We have to assume it's YAML because some of the YAML examples + # do not have file extensions. + return yaml.load(f) if __name__ == '__main__': From 2c9f00d37aef000fc0a483d9d4bccac973124adb Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Sun, 29 Jul 2018 12:23:41 +0900 Subject: [PATCH 2/5] Follow PEP 8 --- api/check_examples.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/check_examples.py b/api/check_examples.py index 81a9298e..3e791bae 100755 --- a/api/check_examples.py +++ b/api/check_examples.py @@ -58,7 +58,7 @@ def check_parameter(filepath, request, parameter): if example and schema: try: - print ("Checking request schema for: %r %r" % ( + print("Checking request schema for: %r %r" % ( filepath, request )) check_schema(filepath, example, schema) From d17ec7f1846a96844c750dac3935ccd32b323b74 Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Sun, 29 Jul 2018 12:25:11 +0900 Subject: [PATCH 3/5] Check that path starts with file://, not file:/// Because file:/// is not a thing on Windows. --- api/check_examples.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/check_examples.py b/api/check_examples.py index 3e791bae..eb861fca 100755 --- a/api/check_examples.py +++ b/api/check_examples.py @@ -126,7 +126,7 @@ def resolve_references(path, schema): def load_file(path): print("Loading reference: %s" % path) - if not path.startswith("file:///"): + if not path.startswith("file://"): raise Exception("Bad ref: %s" % (path,)) path = path[len("file://"):] with open(path, "r") as f: From c6bfac41c535a9608a67181f43864bafd3ae6d16 Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Sun, 29 Jul 2018 12:30:25 +0900 Subject: [PATCH 4/5] Bypass jsonschema references resolver jsonschema's resolver doesn't work with local files on Windows. resolve_references only works with the local files (on Windows as well) but that's exactly what's needed for the case (as long as we don't have remote references, that is). Signed-off-by: Alexey Rusakov --- api/check_examples.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/api/check_examples.py b/api/check_examples.py index eb861fca..cb85cb50 100755 --- a/api/check_examples.py +++ b/api/check_examples.py @@ -44,16 +44,14 @@ except ImportError as e: def check_schema(filepath, example, schema): - # Setting the 'id' tells jsonschema where the file is so that it - # can correctly resolve relative $ref references in the schema - schema['id'] = "file://" + os.path.abspath(filepath) example = resolve_references(filepath, example) + schema = resolve_references(filepath, schema) resolver = jsonschema.RefResolver(filepath, schema, handlers={"file": load_file}) jsonschema.validate(example, schema, resolver=resolver) def check_parameter(filepath, request, parameter): - schema = parameter.get("schema") + schema = parameter.get("schema", {}) example = schema.get('example') if example and schema: From 346db48588612620f179e0b9560ac4d1d8a24729 Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Sun, 29 Jul 2018 14:04:07 +0900 Subject: [PATCH 5/5] Revert giving get() a default As per the PR review. Signed-off-by: Alexey Rusakov --- api/check_examples.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/check_examples.py b/api/check_examples.py index cb85cb50..0fb275b1 100755 --- a/api/check_examples.py +++ b/api/check_examples.py @@ -51,7 +51,7 @@ def check_schema(filepath, example, schema): def check_parameter(filepath, request, parameter): - schema = parameter.get("schema", {}) + schema = parameter.get("schema") example = schema.get('example') if example and schema: