Skip to content

Commit

Permalink
Passing response content instead of response object to yaml loader
Browse files Browse the repository at this point in the history
  • Loading branch information
maxime authored and stephenfin committed Oct 24, 2023
1 parent 3eca56f commit c07af1e
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
2 changes: 1 addition & 1 deletion sphinxcontrib/openapi/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
41 changes: 40 additions & 1 deletion tests/test_openapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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({
Expand Down

0 comments on commit c07af1e

Please sign in to comment.