def test_fetch_url_cached(): from prance.util import fs cache = {} content1 = url.fetch_url(url.absurl(fs.abspath('tests/specs/with_externals.yaml')), cache) assert content1['swagger'] == '2.0' content2 = url.fetch_url(url.absurl(fs.abspath('tests/specs/with_externals.yaml')), cache) assert content2['swagger'] == '2.0' # Dicts are mutable, therefore we can't compare IDs. But individual # string fields should not be copied, because we shallow copy. assert id(content1['swagger']) == id(content2['swagger'])
def _fetch_url(self, url): """ Fetch the parsed contents of the given URL. Uses a caching mechanism so that each URL is only fetched once. """ url_key = _url.urlresource(url) # Same URL key means it's the current file if url_key == self._url_key: return url, self.specs # For all other URLs, we might have a cached parser around already. resolver = self.__reference_cache.get(url_key, None) # If we don't have a parser for the url yet, create and cache one. if not resolver: resolver = RefResolver(_url.fetch_url(url), url) self.__reference_cache[url_key] = resolver # Resolve references *after* (potentially) adding the resolver to the # cache. This together with the __recursion_protection allows us to detect # and report recursions and bad references. resolver.resolve_references() # That's it! return url, resolver.specs
def _dereference(self, ref_url, obj_path): """ Dereference the URL and object path. Returns the dereferenced object. :param mixed ref_url: The URL at which the reference is located. :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, strict=self.__strict) # 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, IndexError, TypeError) as ex: raise _url.ResolutionError( f'Cannot resolve reference "{ref_url.geturl()}": {str(ex)}' ) # Deep copy value; we don't want to create recursive structures import copy value = copy.deepcopy(value) # Now resolve partial specs value = self._translate_partial(ref_url, value) # That's it! return value
def test_fetch_url_http(): exturl = 'http://finkhaeuser.de/projects/prance/petstore.yaml'\ '#/definitions/Pet' content = url.fetch_url(url.absurl(exturl)) assert content['swagger'] == '2.0'
def test_fetch_url_file(): from prance.util import fs content = url.fetch_url(url.absurl( fs.abspath('tests/with_externals.yaml'))) assert content['swagger'] == '2.0'
def test_fetch_url_python(): exturl = 'python://tests/specs/petstore.yaml' content = url.fetch_url(url.absurl(exturl)) assert content['swagger'] == '2.0'
def test_fetch_url_python(): exturl = "python://tests/specs/petstore.yaml" content = url.fetch_url(url.absurl(exturl)) assert content["swagger"] == "2.0"
def test_fetch_url_http(): exturl = "http://finkhaeuser.de/projects/prance/petstore.yaml" "#/definitions/Pet" content = url.fetch_url(url.absurl(exturl)) assert content["swagger"] == "2.0"
def test_fetch_url_file(): from prance.util import fs content = url.fetch_url( url.absurl(fs.abspath("tests/specs/with_externals.yaml"))) assert content["swagger"] == "2.0"