Skip to content

Eterna2/fix relative ref #595

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
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
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,9 @@ _cache
_static
_templates

.eggs
.tox

*.py[cod]

TODO
33 changes: 33 additions & 0 deletions jsonschema/tests/test_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -1664,6 +1664,39 @@ def test_it_retrieves_local_refs_via_urlopen(self):
with self.resolver.resolving(ref) as resolved:
self.assertEqual(resolved, "bar")

def test_it_retrieves_local_relative_refs_via_open(self):
with tempfile.NamedTemporaryFile(delete=False, mode="wt") as tempf:
self.addCleanup(os.remove, tempf.name)
json.dump({"foo": "bar"}, tempf)

ref = "{}#foo".format(os.path.relpath(tempf.name))
with self.resolver.resolving(ref) as resolved:
self.assertEqual(resolved, "bar")

def test_it_retrieves_relative_refs_via_urlopen(self):
ref = "fuzz.json#baz"
schema = {"baz": 12}
resolver = validators.RefResolver(
"http://foo.com/bar.json", self.referrer, self.store,
)
if "requests" in sys.modules:
self.addCleanup(
sys.modules.__setitem__, "requests", sys.modules["requests"],
)
sys.modules["requests"] = None

@contextmanager
def fake_urlopen(url):
self.assertEqual(url, "http://foo.com/fuzz.json")
yield BytesIO(json.dumps(schema).encode("utf8"))

self.addCleanup(setattr, validators, "urlopen", validators.urlopen)
validators.urlopen = fake_urlopen

with resolver.resolving(ref) as resolved:
pass
self.assertEqual(resolved, 12)

def test_it_can_construct_a_base_uri_from_a_schema(self):
schema = {"id": "foo"}
resolver = validators.RefResolver.from_schema(
Expand Down
21 changes: 18 additions & 3 deletions jsonschema/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import contextlib
import json
import numbers
import os

from six import add_metaclass

Expand All @@ -25,6 +26,7 @@
urljoin,
urlopen,
urlsplit,
urlunsplit,
)

# Sigh. https://gitlab.com/pycqa/flake8/issues/280
Expand Down Expand Up @@ -826,9 +828,22 @@ def resolve_remote(self, uri):
# json over http
result = requests.get(uri).json()
else:
# Otherwise, pass off to urllib and assume utf-8
with urlopen(uri) as url:
result = json.loads(url.read().decode("utf-8"))
try:
# Otherwise, pass off to urllib and assume utf-8
with urlopen(uri) as url:
result = json.loads(url.read().decode("utf-8"))
except Exception:
try:
# try relative path on local fs
with open(os.path.join(self.base_uri, uri)) as fin:
result = json.load(fin)
except IOError:
scheme, network, path, qs, frag = urlsplit(self.base_uri)
path = "/".join(path.split("/")[0:-1]) # base path
base_url = urlunsplit((scheme, network, path, qs, frag))
# point to relative url
with urlopen(urljoin(base_url, uri)) as url:
result = json.loads(url.read().decode("utf-8"))

if self.cache_remote:
self.store[uri] = result
Expand Down