diff --git a/sphinxcontrib/openapi/utils.py b/sphinxcontrib/openapi/utils.py index 29cf488..daeedc2 100644 --- a/sphinxcontrib/openapi/utils.py +++ b/sphinxcontrib/openapi/utils.py @@ -45,7 +45,7 @@ def resolve_remote(self, uri): if scheme in [u"http", u"https"] and self._requests: response = self._requests.get(uri) - result = yaml.safe_load(response) + result = yaml.safe_load(response.content) else: # Otherwise, pass off to urllib and assume utf-8 with closing(urlopen(uri)) as url: diff --git a/tests/test_openapi.py b/tests/test_openapi.py index 103e00d..551f6a6 100644 --- a/tests/test_openapi.py +++ b/tests/test_openapi.py @@ -7,10 +7,11 @@ :copyright: (c) 2016, Ihor Kalnytskyi. :license: BSD, see LICENSE for details. """ - +import json import os import textwrap import collections +from unittest import mock import py import pytest @@ -1755,6 +1756,44 @@ def test_relative_ref_resolving_on_fs(self): }, } + @mock.patch('requests.get') + def test_relative_ref_resolving_remote(self, mock_get): + baseuri = os.path.abspath(__file__) + with open( + os.path.join(os.path.dirname(baseuri), 'testdata', 'foo.json'), + 'r', + encoding='utf-8' + ) as file: + json_content = json.loads(file.read()) + with open(os.path.join(os.path.dirname(baseuri), 'testdata', 'foo.yaml'), 'rb') as file: + yaml_content = file.read() + + def get_side_effect(path): + nonlocal json_content + if path.endswith('.json'): + return mock.Mock(json=mock.Mock(return_value=json_content)) + return mock.Mock(content=yaml_content, read=mock.Mock(side_effect=Exception)) + mock_get.side_effect = get_side_effect + + data = { + 'bar': { + '$ref': 'testdata/foo.json#/foo/b', + }, + # check also JSON to YAML references: + 'baz': { + '$ref': 'testdata/foo.yaml#/foo', + } + } + assert utils._resolve_refs('https://some/remote/file', data) == { + 'bar': { + 'c': True, + }, + 'baz': { + 'a': 17, + 'b': 13, + }, + } + def test_noproperties(self): renderer = renderers.HttpdomainOldRenderer(None, {'examples': True}) text = '\n'.join(renderer.render_restructuredtext_markup({