Skip to content

Added support for 'file://' URL schema on the RefResolver #531

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
wants to merge 1 commit 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
4 changes: 2 additions & 2 deletions jsonschema/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from urllib.parse import (
unquote, urljoin, urlunsplit, SplitResult, urlsplit as _urlsplit
)
from urllib.request import pathname2url, urlopen
from urllib.request import pathname2url, url2pathname, urlopen
str_types = str,
int_types = int,
iteritems = operator.methodcaller("items")
Expand All @@ -32,7 +32,7 @@
from urlparse import (
urljoin, urlunsplit, SplitResult, urlsplit as _urlsplit # noqa
)
from urllib import pathname2url, unquote # noqa
from urllib import pathname2url, url2pathname, unquote # noqa
import urllib2 # noqa
def urlopen(*args, **kwargs):
return contextlib.closing(urllib2.urlopen(*args, **kwargs))
Expand Down
8 changes: 8 additions & 0 deletions jsonschema/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
urljoin,
urlopen,
urlsplit,
url2pathname,
)

# Sigh. https://gitlab.com/pycqa/flake8/issues/280
Expand Down Expand Up @@ -818,6 +819,13 @@ def resolve_remote(self, uri):

if scheme in self.handlers:
result = self.handlers[scheme](uri)
elif scheme in [u"file", u""]:
# Resolve local files
path = uri
if path.startswith('file:'):
path = path[len('file:'):]
with open(url2pathname(path)) as _file:
result = json.load(_file)
elif scheme in [u"http", u"https"] and requests:
# Requests has support for detecting the correct encoding of
# json over http
Expand Down