示例#1
0
def test_split_url_reference():
    base = url.absurl('http://foo.bar/')

    # Relative reference
    parsed, path = url.split_url_reference(base, '#foo/bar')
    assert parsed.netloc == 'foo.bar'
    assert len(path) == 2
    assert path[0] == 'foo'
    assert path[1] == 'bar'

    # Leading slashes are stripped
    parsed, path = url.split_url_reference(base, '#///foo/bar')
    assert parsed.netloc == 'foo.bar'
    assert len(path) == 2
    assert path[0] == 'foo'
    assert path[1] == 'bar'

    # Absolute reference
    parsed, path = url.split_url_reference(base, 'http://somewhere/#foo/bar')
    assert parsed.netloc == 'somewhere'
    assert len(path) == 2
    assert path[0] == 'foo'
    assert path[1] == 'bar'

    # Reference with escaped parts
    parsed, path = url.split_url_reference(
        base, 'http://somewhere/#foo/bar~1baz~1quux~0foo')
    assert parsed.netloc == 'somewhere'
    assert len(path) == 2
    assert path[0] == 'foo'
    assert path[1] == 'bar/baz/quux~foo'
示例#2
0
def test_split_url_reference():
    base = url.absurl("http://foo.bar/")

    # Relative reference
    parsed, path = url.split_url_reference(base, "#foo/bar")
    assert parsed.netloc == "foo.bar"
    assert len(path) == 2
    assert path[0] == "foo"
    assert path[1] == "bar"

    # Leading slashes are stripped
    parsed, path = url.split_url_reference(base, "#///foo/bar")
    assert parsed.netloc == "foo.bar"
    assert len(path) == 2
    assert path[0] == "foo"
    assert path[1] == "bar"

    # Absolute reference
    parsed, path = url.split_url_reference(base, "http://somewhere/#foo/bar")
    assert parsed.netloc == "somewhere"
    assert len(path) == 2
    assert path[0] == "foo"
    assert path[1] == "bar"

    # Reference with escaped parts
    parsed, path = url.split_url_reference(
        base, "http://somewhere/#foo/bar~1baz~1quux~0foo")
    assert parsed.netloc == "somewhere"
    assert len(path) == 2
    assert path[0] == "foo"
    assert path[1] == "bar/baz/quux~foo"
示例#3
0
    def _dereferencing_iterator(self, base_url, partial, path, recursions):
        """
        Iterate over a partial spec, dereferencing all references within.

        Yields the resolved path and value of all items that need substituting.

        :param mixed base_url: URL that the partial specs is located at.
        :param dict partial: The partial specs to work on.
        :param tuple path: The parent path of the partial specs.
        :param tuple recursions: A recursion stack for resolving references.
        """
        from .iterators import reference_iterator

        for _, refstring, item_path in reference_iterator(partial):
            # Split the reference string into parsed URL and object path
            ref_url, obj_path = _url.split_url_reference(base_url, refstring)

            translate = (self.__resolve_method == TRANSLATE_EXTERNAL
                         and self.parsed_url.path != ref_url.path)

            if self._skip_reference(base_url, ref_url):
                continue

            # The reference path is the url resource and object path
            ref_path = (_url.urlresource(ref_url), tuple(obj_path))

            # Count how often the reference path has been recursed into.
            from collections import Counter

            rec_counter = Counter(recursions)
            next_recursions = recursions + (ref_path, )

            if rec_counter[ref_path] >= self.__reclimit:
                # The referenced value may be produced by the handler, or the handler
                # may raise, etc.
                ref_value = self.__reclimit_handler(self.__reclimit, ref_url,
                                                    next_recursions)
            else:
                # The referenced value is to be used, but let's copy it to avoid
                # building recursive structures.
                ref_value = self._dereference(ref_url, obj_path,
                                              next_recursions)

            # Full item path
            full_path = path + item_path

            # First yield parent
            if translate:
                url = self._collect_soft_refs(ref_url, obj_path, ref_value)
                yield full_path, {"$ref": "#/components/schemas/" + url}
            else:
                yield full_path, ref_value
示例#4
0
    def _translating_iterator(self, base_url, partial, path):
        from prance.util.iterators import reference_iterator
        for _, ref_string, item_path in reference_iterator(partial):
            ref_url, obj_path = _url.split_url_reference(base_url, ref_string)
            full_path = path + item_path

            if ref_url.path == self.url.path:
                # Reference to the root document.
                ref_path = obj_path
            else:
                # Reference to a non-root document.
                ref_key = _reference_key(ref_url, obj_path)
                if ref_key not in self.__collected_references:
                    self.__collected_references[ref_key] = None
                    ref_value = self._dereference(ref_url, obj_path)
                    self.__collected_references[ref_key] = ref_value
                ref_path = ['components', 'schemas', ref_key]

            ref_obj = _local_ref(ref_path)
            yield full_path, ref_obj