Skip to content

Commit

Permalink
Add test for relative references via $ref to YAML documents
Browse files Browse the repository at this point in the history
  • Loading branch information
tchernobog committed Mar 28, 2020
1 parent ac78f12 commit af3ee79
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 7 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ docs/_build/
*.sublime-project
*.sublime-workspace

# Ignore VSCode folder
/.vscode/

# Ignore virtualenvs (who places it near)
.venv/

Expand Down
Empty file modified setup.py
100644 → 100755
Empty file.
12 changes: 9 additions & 3 deletions sphinxcontrib/openapi/openapi30.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@
"""

import copy
import collections

try:
import collections.abc
except ImportError:
import collections
collections.abc = collections

from datetime import datetime
import itertools
import json
Expand Down Expand Up @@ -64,9 +70,9 @@ def _dict_merge(dct, merge_dct):
dct: dict onto which the merge is executed
merge_dct: dct merged into dct
"""
for k, v in merge_dct.items():
for k in merge_dct.keys():
if (k in dct and isinstance(dct[k], dict)
and isinstance(merge_dct[k], collections.Mapping)):
and isinstance(merge_dct[k], collections.abc.Mapping)):
_dict_merge(dct[k], merge_dct[k])
else:
dct[k] = merge_dct[k]
Expand Down
12 changes: 9 additions & 3 deletions sphinxcontrib/openapi/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@
:license: BSD, see LICENSE for details.
"""

import collections
from __future__ import unicode_literals

try:
import collections.abc
except ImportError:
import collections
collections.abc = collections

import jsonschema
try:
Expand All @@ -33,10 +39,10 @@ def _resolve_refs(uri, spec):
resolver = jsonschema.RefResolver(uri, spec)

def _do_resolve(node):
if isinstance(node, collections.Mapping) and '$ref' in node:
if isinstance(node, collections.abc.Mapping) and '$ref' in node:
with resolver.resolving(node['$ref']) as resolved:
return resolved
elif isinstance(node, collections.Mapping):
elif isinstance(node, collections.abc.Mapping):
for k, v in node.items():
node[k] = _do_resolve(v)
elif isinstance(node, (list, tuple)):
Expand Down
10 changes: 9 additions & 1 deletion tests/test_openapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -1611,16 +1611,24 @@ def test_ref_resolving(self):

def test_relative_ref_resolving_on_fs(self):
baseuri = 'file://%s' % os.path.abspath(__file__)

data = {
'bar': {
'$ref': 'testdata/foo.json#/foo/b',
},
# check also JSON to YAML references:
'baz': {
'$ref': 'testdata/foo.yaml#/foo/b',
}
}

assert utils._resolve_refs(baseuri, data) == {
'bar': {
'c': True,
}
},
'baz': {
'c': True,
},
}

def test_noproperties(self):
Expand Down
4 changes: 4 additions & 0 deletions tests/testdata/foo.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
foo:
a: 13,
b:
c: true

0 comments on commit af3ee79

Please sign in to comment.