Skip to content

Add id with ref support, solve bug(371). #717

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 0 additions & 72 deletions jsonschema/tests/test_jsonschema_test_suite.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,30 +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(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",
Expand Down Expand Up @@ -220,30 +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(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",
Expand Down Expand Up @@ -295,30 +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(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",
Expand Down
36 changes: 36 additions & 0 deletions jsonschema/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import contextlib
import json
import numbers
import copy

from jsonschema import (
_legacy_validators,
Expand Down Expand Up @@ -654,6 +655,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
Expand Down Expand Up @@ -862,6 +864,40 @@ def resolve_remote(self, uri):
self.store[uri] = result
return result

def store_subschema(self, schema, last_url=None):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To start I'd not want this to be a public method / addition to the API.

"""
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):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will have a closer look, but the schema does not have to be a dict (it's an arbitrary collections.Mapping) and does not necessarily even need to be copy-able.

return

for k in schema.keys():
if k in [u"id", u"$id"] and isinstance(schema[k], str):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should use Validator.ID_OF in some way.

# 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):
"""
Expand Down