diff --git a/jsonschema/tests/test_validators.py b/jsonschema/tests/test_validators.py index 2f017b930..a749dfe57 100644 --- a/jsonschema/tests/test_validators.py +++ b/jsonschema/tests/test_validators.py @@ -1793,6 +1793,8 @@ def get(self, url): class _ReallyFakeJSONResponse(object): _response = attr.ib() + text = str(_response) def json(self): return json.loads(self._response) + diff --git a/jsonschema/validators.py b/jsonschema/validators.py index d6e0a0c09..37e996d4f 100644 --- a/jsonschema/validators.py +++ b/jsonschema/validators.py @@ -837,18 +837,29 @@ def resolve_remote(self, uri): except ImportError: requests = None + try: + import yaml + except ImportError: + yaml = None + scheme = urlsplit(uri).scheme if scheme in self.handlers: result = self.handlers[scheme](uri) - elif scheme in [u"http", u"https"] and requests: - # Requests has support for detecting the correct encoding of - # 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")) + path = urlsplit(uri).path + if scheme in [u"http", u"https"] and requests: + # Requests has support for detecting the correct encoding + received = requests.get(uri).text + else: + # Otherwise, pass off to urllib and assume utf-8 + with urlopen(uri) as url: + received = url.read().decode("utf-8") + + if path.endswith((".yaml", ".yml")) and yaml: + result = yaml.safe_load(received) + else: + result = json.loads(received) if self.cache_remote: self.store[uri] = result