Skip to content

Commit 434ff64

Browse files
committed
add id with ref support
1 parent 8a9852e commit 434ff64

File tree

2 files changed

+41
-57
lines changed

2 files changed

+41
-57
lines changed

jsonschema/tests/test_jsonschema_test_suite.py

Lines changed: 0 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -151,25 +151,6 @@ def narrow_unicode_build(test): # pragma: no cover
151151
subject="ref",
152152
case_description="Recursive references between schemas",
153153
)(test)
154-
or skip(
155-
message=bug(371),
156-
subject="ref",
157-
case_description="Location-independent identifier",
158-
)(test)
159-
or skip(
160-
message=bug(371),
161-
subject="ref",
162-
case_description=(
163-
"Location-independent identifier with absolute URI"
164-
),
165-
)(test)
166-
or skip(
167-
message=bug(371),
168-
subject="ref",
169-
case_description=(
170-
"Location-independent identifier with base URI change in subschema"
171-
),
172-
)(test)
173154
or skip(
174155
message=bug(),
175156
subject="refRemote",
@@ -225,25 +206,6 @@ def narrow_unicode_build(test): # pragma: no cover
225206
subject="ref",
226207
case_description="Recursive references between schemas",
227208
)(test)
228-
or skip(
229-
message=bug(371),
230-
subject="ref",
231-
case_description="Location-independent identifier",
232-
)(test)
233-
or skip(
234-
message=bug(371),
235-
subject="ref",
236-
case_description=(
237-
"Location-independent identifier with absolute URI"
238-
),
239-
)(test)
240-
or skip(
241-
message=bug(371),
242-
subject="ref",
243-
case_description=(
244-
"Location-independent identifier with base URI change in subschema"
245-
),
246-
)(test)
247209
or skip(
248210
message=bug(),
249211
subject="refRemote",
@@ -300,25 +262,6 @@ def narrow_unicode_build(test): # pragma: no cover
300262
subject="ref",
301263
case_description="Recursive references between schemas",
302264
)(test)
303-
or skip(
304-
message=bug(371),
305-
subject="ref",
306-
case_description="Location-independent identifier",
307-
)(test)
308-
or skip(
309-
message=bug(371),
310-
subject="ref",
311-
case_description=(
312-
"Location-independent identifier with absolute URI"
313-
),
314-
)(test)
315-
or skip(
316-
message=bug(371),
317-
subject="ref",
318-
case_description=(
319-
"Location-independent identifier with base URI change in subschema"
320-
),
321-
)(test)
322265
or skip(
323266
message=bug(),
324267
subject="refRemote",

jsonschema/validators.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import contextlib
66
import json
77
import numbers
8+
import copy
89

910
from jsonschema import (
1011
_legacy_validators,
@@ -31,6 +32,10 @@
3132
# Imported for backwards compatibility.
3233
from jsonschema.exceptions import ErrorTree
3334
ErrorTree
35+
list_schema = ["http://json-schema.org/draft-03/schema#",
36+
"http://json-schema.org/draft-04/schema#",
37+
"http://json-schema.org/draft-06/schema#",
38+
"http://json-schema.org/draft-07/schema#"]
3439

3540

3641
class _DontDoThat(Exception):
@@ -654,6 +659,7 @@ def __init__(
654659
)
655660
self.store.update(store)
656661
self.store[base_uri] = referrer
662+
self.store_subschema(referrer, last_url=base_uri)
657663

658664
self._urljoin_cache = urljoin_cache
659665
self._remote_cache = remote_cache
@@ -862,6 +868,41 @@ def resolve_remote(self, uri):
862868
self.store[uri] = result
863869
return result
864870

871+
def store_subschema(self, schema, last_url=None):
872+
"""
873+
Using $id or id with $ref, save subschema to self.store
874+
875+
Arguments:
876+
877+
schema:
878+
879+
The referring schema.
880+
881+
last_url:
882+
883+
The last URL.
884+
"""
885+
if not isinstance(schema, dict) \
886+
or self.resolution_scope in list_schema:
887+
return
888+
889+
for k in schema.keys():
890+
if k in [u"id", u"$id"] and isinstance(schema[k], str):
891+
# Splicing the url in the last id with the url in this id,
892+
# and store last_url at the same time.
893+
last_url = urljoin(last_url, schema[k], allow_fragments=True)
894+
url, fragment = urldefrag(last_url)
895+
896+
# Save the schema into self.store[url]
897+
self.store[url] = copy.deepcopy(schema)
898+
899+
# Add fragment element in self.store[url]
900+
if fragment:
901+
self.store[url][fragment] = copy.deepcopy(schema)
902+
903+
if isinstance(schema[k], dict):
904+
self.store_subschema(schema[k], last_url)
905+
865906

866907
def validate(instance, schema, cls=None, *args, **kwargs):
867908
"""

0 commit comments

Comments
 (0)