Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
- `jsonld.to_rdf()` writes the `xsd:double` NaN as `NaN`, its XSD canonical form, instead of `NAN`, so `jsonld.normalize()` output changes for documents with such a value.
- `iri_resolver.resolve()` fixes references without a scheme or host against a base IRI with a host and no path (`/x` against `http://example.org` gave `http://example.org//x`) or with a query, and keeps the dot of a first segment such as `.gh`.
- `iri_resolver.remove_dot_segments()`, and with it `iri_resolver.resolve()`, no longer drops the `/` that a final `.` or `..` segment leaves before a query or fragment (`/a/.?x` gave `/a?x`).
- `iri_resolver.resolve()` treats a reference with a `/`, `?` or `#` before its first `:`, such as `/a:b`, `x/y:z` or `a?b:c`, as a relative IRI and resolves it against the base IRI, as RFC 3986 does. It was returned unresolved, so `jsonld.to_rdf()` dropped it. Dot segments in the query or fragment of an IRI are no longer removed.

### Changed
- **BREAKING**: Developer install, command execution, documentation, and release artifact
Expand Down
29 changes: 17 additions & 12 deletions lib/pyld/iri_resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ def remove_dot_segments_of_path(iri: str, colon_position: int) -> str:
Remove dot segments from the path portion of an IRI (RFC 3986 §5.2.4).

:param iri: an IRI (or part of IRI).
:param colonPosition: the position of the first ':' in the IRI.
:param colon_position: the position of the ':' that ends the scheme of the
IRI, or -1 if it has no scheme.

:return: the IRI where dot segments were removed.
"""
Expand All @@ -116,9 +117,12 @@ def remove_dot_segments_of_path(iri: str, colon_position: int) -> str:
else:
search_offset = 2 if len(iri) > 1 and iri[0] == '/' and iri[1] == '/' else 0

# Find the start of the path
# Find the start of the path: the first '/', unless a query or fragment
# starts before it
path_separator = iri.find('/', search_offset)
if path_separator < 0:
if path_separator < 0 or any(
ch in iri[search_offset:path_separator] for ch in '?#'
):
return iri

base = iri[:path_separator]
Expand Down Expand Up @@ -175,10 +179,15 @@ def resolve(relative_iri: str, base_iri: str = None) -> str:
)
return remove_dot_segments_of_path(relative_iri, relative_colon_pos)

# Ignore baseIRI if the value is absolute
# Ignore baseIRI if the value is absolute.
# Per RFC 3986, a URI scheme cannot contain a '/', '?' or '#', so if one of
# them appears before the first ':', the IRI is relative (not absolute).
value_colon_pos = relative_iri.find(":")
if value_colon_pos >= 0:
if value_colon_pos >= 0 and not any(
ch in relative_iri[:value_colon_pos] for ch in "/?#"
):
return remove_dot_segments_of_path(relative_iri, value_colon_pos)
# From here on, the value has no scheme: a ':' in it does not end a scheme.

# baseIRI must be absolute
base_colon_pos = base_iri.find(":")
Expand All @@ -191,7 +200,7 @@ def resolve(relative_iri: str, base_iri: str = None) -> str:

# Inherit base scheme if relative starts with '//'
if relative_iri.startswith("//"):
return base_scheme + remove_dot_segments_of_path(relative_iri, value_colon_pos)
return base_scheme + remove_dot_segments_of_path(relative_iri, -1)

# Determine where the path of base starts
if base_iri.find("//", base_colon_pos) == base_colon_pos + 1:
Expand All @@ -203,15 +212,11 @@ def resolve(relative_iri: str, base_iri: str = None) -> str:
base_iri += "/"
base_slash_after_colon_pos = len(base_iri) - 1
else:
return base_scheme + remove_dot_segments_of_path(
relative_iri, value_colon_pos
)
return base_scheme + remove_dot_segments_of_path(relative_iri, -1)
else:
base_slash_after_colon_pos = base_iri.find("/", base_colon_pos + 1)
if base_slash_after_colon_pos < 0:
return base_scheme + remove_dot_segments_of_path(
relative_iri, value_colon_pos
)
return base_scheme + remove_dot_segments_of_path(relative_iri, -1)

# If relative starts with '/', append after base authority
if relative_iri.startswith("/"):
Expand Down
56 changes: 56 additions & 0 deletions tests/iri_resolver/test_resolve.py
Original file line number Diff line number Diff line change
Expand Up @@ -480,3 +480,59 @@ def test_relative_with_triple_dot_segment_and_2x_double_dot_and_base():

def test_questionmark_prefix_relative_with_complex_base_with_dot():
assert resolve('?y', 'http://a/bb/ccc/./d;p?q') == 'http://a/bb/ccc/./d;p?y'


def test_relative_with_slash_before_colon_and_base_with_trailing_slash():
assert resolve('x/y:z', 'http://base.org/path/') == 'http://base.org/path/x/y:z'


def test_relative_with_slash_before_colon_and_base_without_trailing_slash():
assert resolve('x/y:z', 'http://base.org/path') == 'http://base.org/x/y:z'


def test_relative_with_multiple_colons_after_slash():
assert resolve('x/y:z:w', 'http://base.org/path/') == 'http://base.org/path/x/y:z:w'


def test_absolute_path_with_colon_and_base():
assert resolve('/a:b', 'http://base.org/path/') == 'http://base.org/a:b'


def test_absolute_path_with_colon_and_dot_segments_and_base_without_path():
assert resolve('/a:b/../c', 'http://base.org') == 'http://base.org/c'


def test_relative_with_question_mark_before_colon():
assert resolve('a?b:c', 'http://base.org/path/x') == 'http://base.org/path/a?b:c'


def test_relative_with_hash_before_colon():
assert resolve('a#b:c', 'http://base.org/path/x') == 'http://base.org/path/a#b:c'


def test_network_path_with_dot_segment_before_colon():
assert resolve('//h/./a:b', 'http://base.org/path/') == 'http://h/a:b'


def test_relative_with_dot_segment_before_colon_and_base_without_slash_slash():
assert resolve('x/./y:z', 'tag:example') == 'tag:x/y:z'


def test_network_path_with_colon_and_dot_segment_before_query():
assert resolve('//h/a:b/.?q', 'http://x/') == 'http://h/a:b/?q'


def test_relative_with_colon_and_dot_segment_before_query_and_base_without_slash_slash():
assert resolve('z/a:b/.?', 'urn:x') == 'urn:z/a:b/?'


def test_relative_with_dot_segments_and_colon_in_query():
assert resolve('doc?next=/a/../b:c', 'urn:x:y') == 'urn:doc?next=/a/../b:c'


def test_relative_with_dot_segments_and_colon_in_fragment():
assert resolve('doc#/a/../b:c', 'tag:example') == 'tag:doc#/a/../b:c'


def test_absolute_iri_with_dot_segments_in_query():
assert resolve('urn:x?a/../b') == 'urn:x?a/../b'