From 434ff64b2a82668c197708498f23440a151e0b0f Mon Sep 17 00:00:00 2001 From: willson-chen Date: Wed, 5 Aug 2020 11:28:28 +0800 Subject: [PATCH 1/3] add id with ref support --- .../tests/test_jsonschema_test_suite.py | 57 ------------------- jsonschema/validators.py | 41 +++++++++++++ 2 files changed, 41 insertions(+), 57 deletions(-) diff --git a/jsonschema/tests/test_jsonschema_test_suite.py b/jsonschema/tests/test_jsonschema_test_suite.py index e561f3e2b..039eb199e 100644 --- a/jsonschema/tests/test_jsonschema_test_suite.py +++ b/jsonschema/tests/test_jsonschema_test_suite.py @@ -151,25 +151,6 @@ def narrow_unicode_build(test): # pragma: no cover subject="ref", case_description="Recursive references between schemas", )(test) - or skip( - message=bug(371), - subject="ref", - case_description="Location-independent identifier", - )(test) - or skip( - message=bug(371), - subject="ref", - case_description=( - "Location-independent identifier with absolute URI" - ), - )(test) - or skip( - message=bug(371), - subject="ref", - case_description=( - "Location-independent identifier with base URI change in subschema" - ), - )(test) or skip( message=bug(), subject="refRemote", @@ -225,25 +206,6 @@ def narrow_unicode_build(test): # pragma: no cover subject="ref", case_description="Recursive references between schemas", )(test) - or skip( - message=bug(371), - subject="ref", - case_description="Location-independent identifier", - )(test) - or skip( - message=bug(371), - subject="ref", - case_description=( - "Location-independent identifier with absolute URI" - ), - )(test) - or skip( - message=bug(371), - subject="ref", - case_description=( - "Location-independent identifier with base URI change in subschema" - ), - )(test) or skip( message=bug(), subject="refRemote", @@ -300,25 +262,6 @@ def narrow_unicode_build(test): # pragma: no cover subject="ref", case_description="Recursive references between schemas", )(test) - or skip( - message=bug(371), - subject="ref", - case_description="Location-independent identifier", - )(test) - or skip( - message=bug(371), - subject="ref", - case_description=( - "Location-independent identifier with absolute URI" - ), - )(test) - or skip( - message=bug(371), - subject="ref", - case_description=( - "Location-independent identifier with base URI change in subschema" - ), - )(test) or skip( message=bug(), subject="refRemote", diff --git a/jsonschema/validators.py b/jsonschema/validators.py index dd4a3abe4..04a368796 100644 --- a/jsonschema/validators.py +++ b/jsonschema/validators.py @@ -5,6 +5,7 @@ import contextlib import json import numbers +import copy from jsonschema import ( _legacy_validators, @@ -31,6 +32,10 @@ # Imported for backwards compatibility. from jsonschema.exceptions import ErrorTree ErrorTree +list_schema = ["http://json-schema.org/draft-03/schema#", + "http://json-schema.org/draft-04/schema#", + "http://json-schema.org/draft-06/schema#", + "http://json-schema.org/draft-07/schema#"] class _DontDoThat(Exception): @@ -654,6 +659,7 @@ def __init__( ) self.store.update(store) self.store[base_uri] = referrer + self.store_subschema(referrer, last_url=base_uri) self._urljoin_cache = urljoin_cache self._remote_cache = remote_cache @@ -862,6 +868,41 @@ def resolve_remote(self, uri): self.store[uri] = result return result + def store_subschema(self, schema, last_url=None): + """ + Using $id or id with $ref, save subschema to self.store + + Arguments: + + schema: + + The referring schema. + + last_url: + + The last URL. + """ + if not isinstance(schema, dict) \ + or self.resolution_scope in list_schema: + return + + for k in schema.keys(): + if k in [u"id", u"$id"] and isinstance(schema[k], str): + # Splicing the url in the last id with the url in this id, + # and store last_url at the same time. + last_url = urljoin(last_url, schema[k], allow_fragments=True) + url, fragment = urldefrag(last_url) + + # Save the schema into self.store[url] + self.store[url] = copy.deepcopy(schema) + + # Add fragment element in self.store[url] + if fragment: + self.store[url][fragment] = copy.deepcopy(schema) + + if isinstance(schema[k], dict): + self.store_subschema(schema[k], last_url) + def validate(instance, schema, cls=None, *args, **kwargs): """ From cbff06ee0ea6d4e3f2f570c47991d3c9ccbf8504 Mon Sep 17 00:00:00 2001 From: wilson chen Date: Sat, 8 Aug 2020 14:54:13 +0800 Subject: [PATCH 2/3] "Recursive references between schemas" is solved --- jsonschema/tests/test_jsonschema_test_suite.py | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/jsonschema/tests/test_jsonschema_test_suite.py b/jsonschema/tests/test_jsonschema_test_suite.py index 039eb199e..f65997934 100644 --- a/jsonschema/tests/test_jsonschema_test_suite.py +++ b/jsonschema/tests/test_jsonschema_test_suite.py @@ -146,11 +146,6 @@ def narrow_unicode_build(test): # pragma: no cover narrow_unicode_build(test) or missing_format(draft4_format_checker)(test) or complex_email_validation(test) - or skip( - message=bug(), - subject="ref", - case_description="Recursive references between schemas", - )(test) or skip( message=bug(), subject="refRemote", @@ -201,11 +196,6 @@ def narrow_unicode_build(test): # pragma: no cover narrow_unicode_build(test) or missing_format(draft6_format_checker)(test) or complex_email_validation(test) - or skip( - message=bug(), - subject="ref", - case_description="Recursive references between schemas", - )(test) or skip( message=bug(), subject="refRemote", @@ -257,11 +247,6 @@ def narrow_unicode_build(test): # pragma: no cover narrow_unicode_build(test) or missing_format(draft7_format_checker)(test) or complex_email_validation(test) - or skip( - message=bug(), - subject="ref", - case_description="Recursive references between schemas", - )(test) or skip( message=bug(), subject="refRemote", From f74e322b4140ff338dc52bcbd35c114f36e698c7 Mon Sep 17 00:00:00 2001 From: wilson chen Date: Tue, 11 Aug 2020 19:58:00 +0800 Subject: [PATCH 3/3] remove draft json filter --- jsonschema/validators.py | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/jsonschema/validators.py b/jsonschema/validators.py index 04a368796..1081100fd 100644 --- a/jsonschema/validators.py +++ b/jsonschema/validators.py @@ -32,10 +32,6 @@ # Imported for backwards compatibility. from jsonschema.exceptions import ErrorTree ErrorTree -list_schema = ["http://json-schema.org/draft-03/schema#", - "http://json-schema.org/draft-04/schema#", - "http://json-schema.org/draft-06/schema#", - "http://json-schema.org/draft-07/schema#"] class _DontDoThat(Exception): @@ -882,8 +878,7 @@ def store_subschema(self, schema, last_url=None): The last URL. """ - if not isinstance(schema, dict) \ - or self.resolution_scope in list_schema: + if not isinstance(schema, dict): return for k in schema.keys():