Skip to content
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

Add referrer to handler's call #445

Closed
wants to merge 2 commits into from
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
34 changes: 32 additions & 2 deletions jsonschema/tests/test_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -1260,7 +1260,7 @@ def test_custom_uri_scheme_handlers(self):
)
with resolver.resolving(ref) as resolved:
self.assertEqual(resolved, schema)
foo_handler.assert_called_once_with(ref)
foo_handler.assert_called_once_with(ref, referrer={})

def test_cache_remote_on(self):
ref = "foo://bar"
Expand All @@ -1272,7 +1272,7 @@ def test_cache_remote_on(self):
pass
with resolver.resolving(ref):
pass
foo_handler.assert_called_once_with(ref)
foo_handler.assert_called_once_with(ref, referrer={})

def test_cache_remote_off(self):
ref = "foo://bar"
Expand Down Expand Up @@ -1302,6 +1302,36 @@ def test_helpful_error_message_on_failed_pop_scope(self):
resolver.pop_scope()
self.assertIn("Failed to pop the scope", str(exc.exception))

def test_scheme_default_fallback(self):
ref = './paths.json'
file_handler = mock.Mock()
resolver = validators.RefResolver(
"", "", handlers={"file": file_handler}
)
with resolver.resolving(ref):
pass
file_handler.assert_called_with(ref, referrer='')

def test_relative_resolver(self):
def file_handler(uri, referrer=None):
# Crude, do not use as a reference implementation
import os.path
no_scheme = referrer[7:] # file:/// 0-based
return 'file://' + os.path.abspath(os.path.join(no_scheme, uri))

ref = '../../schemas/common.json'
base = 'file:///home/julian/spec/'
referrer = base + 'components/common.json'
expected = base + 'schemas/common.json'
resolver = validators.RefResolver(
"", referrer, handlers={"file": file_handler}
)
with resolver.resolving(ref):
pass

self.assertTrue(ref in resolver.store)
self.assertEqual(resolver.store[ref], expected)


class UniqueTupleItemsMixin(object):
"""
Expand Down
6 changes: 5 additions & 1 deletion jsonschema/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,8 @@ class RefResolver(object):
Whether remote refs should be cached after first resolution

"""
default_scheme = 'file'
""" If urlsplit() does not find a scheme, we use this one. """

def __init__(
self,
Expand Down Expand Up @@ -739,9 +741,11 @@ def resolve_remote(self, uri):
requests = None

scheme = urlsplit(uri).scheme
if not scheme:
scheme = self.default_scheme

if scheme in self.handlers:
result = self.handlers[scheme](uri)
result = self.handlers[scheme](uri, referrer=self.referrer)
elif (
scheme in [u"http", u"https"] and
requests and
Expand Down