Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
value = 42
# No path can be resolved in a value type
result = None
with pytest.raises(TypeError):
result = path_get(value, ('foo', 'bar'), 123)
assert result is None
# However, we can resolve zero length paths
result = path_get(value, (), 123)
assert 42 == result
result = path_get(None, (), 123)
assert 123 == result
# Also we can resolve None-type paths
result = path_get(value, None, 321)
assert 42 == result
result = path_get(None, None, 321)
assert 321 == result
def test_get_mapping_default():
value = { 'foo': 1, 'bar': 2, 3: 3 }
# String paths should work in a Mapping
result = path_get(value, ('foo',), 123)
assert 1 == result
# So should numeric keys
result = path_get(value, (3,), 123)
assert 3 == result
# Zero length paths should return the value or default value
result = path_get(value, (), 123)
assert { 'foo': 1, 'bar': 2, 3: 3 } == result
result = path_get(None, (), 123)
assert 123 == result
# And None paths as well
result = path_get(value, None, 321)
assert { 'foo': 1, 'bar': 2, 3: 3 } == result
result = path_get(None, None, 321)
assert 321 == result
# By default, all externals need to be resolved.
from prance.util.path import path_get
val = path_get(res.specs, ('paths', '/pets', 'get', 'responses', '200', 'schema'))
assert '$ref' not in val
val = path_get(res.specs, ('paths', '/pets', 'get', 'responses', 'default', 'schema'))
assert '$ref' not in val
val = path_get(res.specs, ('paths', '/pets', 'post', 'responses', 'default', 'schema'))
assert '$ref' not in val
val = path_get(res.specs, ('paths', '/pets/{petId}', 'get', 'parameters', 0))
assert '$ref' not in val
val = path_get(res.specs, ('paths', '/pets/{petId}', 'get', 'responses', '200', 'schema'))
assert '$ref' in val
val = path_get(res.specs, ('paths', '/pets/{petId}', 'get', 'responses', 'default', 'schema'))
assert '$ref' in val
# By default, all externals need to be resolved.
from prance.util.path import path_get
val = path_get(res.specs, ('paths', '/pets', 'get', 'responses', '200', 'schema'))
assert '$ref' in val
val = path_get(res.specs, ('paths', '/pets', 'get', 'responses', 'default', 'schema'))
assert '$ref' in val
val = path_get(res.specs, ('paths', '/pets', 'post', 'responses', 'default', 'schema'))
assert '$ref' in val
val = path_get(res.specs, ('paths', '/pets/{petId}', 'get', 'parameters', 0))
assert '$ref' in val
val = path_get(res.specs, ('paths', '/pets/{petId}', 'get', 'responses', '200', 'schema'))
assert '$ref' in val
val = path_get(res.specs, ('paths', '/pets/{petId}', 'get', 'responses', 'default', 'schema'))
assert '$ref' not in val
def test_get_collection_default():
value = (1, 2, 3)
# String paths in a Sequence should raise KeyError
result = None
with pytest.raises(KeyError):
result = path_get(value, ('foo', 'bar'), 123)
assert result is None
# A numeric path should work, though
result = path_get(value, (1,), 123)
assert 2 == result
# Zero length paths should return the value or default value
result = path_get(value, (), 123)
assert (1, 2, 3) == result
result = path_get(None, (), 123)
assert 123 == result
# And None paths as well
result = path_get(value, None, 321)
assert (1, 2, 3) == result
result = path_get(None, None, 321)
assert 321 == result
def test_get_informative_index_error():
base = { 'foo': { 'bar': [123] } }
with pytest.raises(IndexError, match = r'.*"/foo/bar".*123'):
path_get(base, ('foo', 'bar', 123))
:param list obj_path: The object path within the URL resource.
:param tuple recursions: A recursion stack for resolving references.
:return: A copy of the dereferenced value, with all internal references
resolved.
"""
# In order to start dereferencing anything in the referenced URL, we have
# to read and parse it, of course.
contents = _url.fetch_url(ref_url, self.__reference_cache)
# In this inner parser's specification, we can now look for the referenced
# object.
value = contents
if len(obj_path) != 0:
from prance.util.path import path_get
try:
value = path_get(value, obj_path)
except KeyError:
raise _url.ResolutionError('Cannot resolve reference "%s"!'
% (ref_url.geturl(), ))
# Deep copy value; we don't want to create recursive structures
import copy
value = copy.deepcopy(value)
# Now resolve partial specs
value = self._resolve_partial(ref_url, value, recursions)
# That's it!
return value