diff --git a/CHANGELOG.md b/CHANGELOG.md index 71166a9b0..95122beab 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -33,6 +33,8 @@ - Use a helper for generalized N-Quads normalization in the test runner, since rdflib cannot parse blank-node predicates. Fixes [toRdf#te075](https://w3c.github.io/json-ld-api/tests/toRdf-manifest#te075). - `jsonld.expand()`, `jsonld.to_rdf()` and the other operations no longer fail with an `OverflowError` for an integer too large for a double. `jsonld.to_rdf()` writes it as `INF` or `-INF`, the infinite `xsd:double` values, or, in `json-ld-1.0` mode and not typed as `xsd:double`, as an exact `xsd:integer`. Inside `@json` literals, `jsonld.to_rdf()` and `jsonld.normalize()` still raise `OverflowError` for such numbers, as RFC 8785 only allows numbers that fit in a double. - `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`). ### Changed - **BREAKING**: Developer install, command execution, documentation, and release artifact diff --git a/lib/pyld/iri_resolver.py b/lib/pyld/iri_resolver.py index 2090a68fa..3d6f17f10 100644 --- a/lib/pyld/iri_resolver.py +++ b/lib/pyld/iri_resolver.py @@ -46,8 +46,9 @@ def remove_dot_segments(path: str) -> str: if segment_buffers: segment_buffers.pop() - # Add trailing slash segment if ends with '/..' - if i + 3 >= length: + # Add trailing slash segment if the path ends with '/..', + # also before a query or fragment + if next_ch != '/': segment_buffers.append([]) i += 3 @@ -60,8 +61,9 @@ def remove_dot_segments(path: str) -> str: i += 1 continue - # Add trailing slash if ends with '/.' - if i + 2 >= length: + # Add trailing slash if the path ends with '/.', + # also before a query or fragment + if next_ch != '/': segment_buffers.append([]) # Stay in current directory — skip @@ -150,17 +152,20 @@ def resolve(relative_iri: str, base_iri: str = None) -> str: ) return base_iri - # If the value starts with a query character, concat directly (strip existing query) - if relative_iri.startswith("?"): - base_query_pos = base_iri.find("?") - if base_query_pos > 0: - base_iri = base_iri[:base_query_pos] - return base_iri + relative_iri - # If the value starts with a fragment character, concat directly if relative_iri.startswith("#"): return base_iri + relative_iri + # The query of the base IRI only applies to an empty value or a fragment + # (RFC 3986, section 5.2.2) + base_query_pos = base_iri.find("?") + if base_query_pos > 0: + base_iri = base_iri[:base_query_pos] + + # If the value starts with a query character, concat directly + if relative_iri.startswith("?"): + return base_iri + relative_iri + # Ignore baseIRI if it is empty if not base_iri: relative_colon_pos = relative_iri.find(":") @@ -193,11 +198,10 @@ def resolve(relative_iri: str, base_iri: str = None) -> str: base_slash_after_colon_pos = base_iri.find("/", base_colon_pos + 3) if base_slash_after_colon_pos < 0: if len(base_iri) > base_colon_pos + 3: - return ( - base_iri - + "/" - + remove_dot_segments_of_path(relative_iri, value_colon_pos) - ) + # The base has an authority and an empty path, so its path is + # taken as '/' (RFC 3986, section 5.2.3) + 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 @@ -219,13 +223,6 @@ def resolve(relative_iri: str, base_iri: str = None) -> str: # Ignore everything after last '/' in base path if last_slash >= 0 and last_slash < len(base_path) - 1: base_path = base_path[: last_slash + 1] - if ( - relative_iri.startswith(".") - and not relative_iri.startswith("..") - and not relative_iri.startswith("./") - and len(relative_iri) > 2 - ): - relative_iri = relative_iri[1:] relative_iri = base_path + relative_iri relative_iri = remove_dot_segments(relative_iri) diff --git a/tests/iri_resolver/test_remove_dot_segments.py b/tests/iri_resolver/test_remove_dot_segments.py index 95a33681f..7300b6451 100644 --- a/tests/iri_resolver/test_remove_dot_segments.py +++ b/tests/iri_resolver/test_remove_dot_segments.py @@ -100,7 +100,7 @@ def test_not_modify_parent_paths_in_queries(): def test_mixed_current_and_parent_with_fragment(): - assert remove_dot_segments('/abc/def/./ghi/../..#abc') == '/abc#abc' + assert remove_dot_segments('/abc/def/./ghi/../..#abc') == '/abc/#abc' def test_fragment_without_another_path(): @@ -151,6 +151,22 @@ def test_end_with_slash_if_trailing_slash_dot(): assert remove_dot_segments('/a/bb/ccc/./g/.') == '/a/bb/ccc/g/' +def test_end_with_slash_if_slash_dot_before_query(): + assert remove_dot_segments('/abc/def/.?a=b') == '/abc/def/?a=b' + + +def test_end_with_slash_if_slash_dot_before_fragment(): + assert remove_dot_segments('/abc/def/.#abc') == '/abc/def/#abc' + + +def test_end_with_slash_if_slash_double_dot_before_query(): + assert remove_dot_segments('/abc/def/..?a=b') == '/abc/?a=b' + + +def test_end_with_slash_if_slash_double_dot_before_fragment(): + assert remove_dot_segments('/abc/def/..#abc') == '/abc/#abc' + + def test_triple_dots_as_normal_segment(): assert remove_dot_segments('/invalid/...') == '/invalid/...' diff --git a/tests/iri_resolver/test_resolve.py b/tests/iri_resolver/test_resolve.py index 92082e33b..adf3a165b 100644 --- a/tests/iri_resolver/test_resolve.py +++ b/tests/iri_resolver/test_resolve.py @@ -63,6 +63,13 @@ def test_non_absolute_base_error(): resolve('abc', 'def') +def test_base_without_scheme_colon_only_in_query_error(): + with pytest.raises( + ValueError, match=r"Found invalid baseIRI 'def' for value 'abc'" + ): + resolve('abc', 'def?x:y') + + def test_non_absolute_base_empty_value_error(): with pytest.raises(ValueError, match=r"Found invalid baseIRI 'def' for value ''"): resolve('', 'def') @@ -80,6 +87,101 @@ def test_base_without_path_dot_segments(): assert resolve('abc/./', 'http://base.org') == 'http://base.org/abc/' +def test_base_without_path_root_path(): + assert resolve('/', 'http://base.org') == 'http://base.org/' + + +def test_base_without_path_absolute_path(): + assert resolve('/abc', 'http://base.org') == 'http://base.org/abc' + + +def test_base_without_path_absolute_path_with_dot_segments(): + assert resolve('/abc/../def', 'http://base.org') == 'http://base.org/def' + + +def test_base_without_path_parent_segment(): + assert resolve('../abc', 'http://base.org') == 'http://base.org/abc' + + +def test_base_without_path_relative_with_query(): + assert resolve('abc?x=1', 'http://base.org') == 'http://base.org/abc?x=1' + + +def test_base_without_path_dot_segments_in_query(): + assert resolve('abc?x=/../y', 'http://base.org') == 'http://base.org/abc?x=/../y' + + +def test_base_without_path_double_dot(): + assert resolve('..', 'http://base.org') == 'http://base.org/' + + +def test_base_without_path_dot(): + assert resolve('.', 'http://base.org') == 'http://base.org/' + + +def test_base_without_path_dot_slash(): + assert resolve('./', 'http://base.org') == 'http://base.org/' + + +def test_base_without_path_dot_slash_segment(): + assert resolve('./abc', 'http://base.org') == 'http://base.org/abc' + + +def test_base_without_path_dot_segment_before_query(): + assert resolve('abc/.?x', 'http://base.org') == 'http://base.org/abc/?x' + + +def test_base_without_path_double_dot_segment_before_fragment(): + assert resolve('abc/def/..#x', 'http://base.org') == 'http://base.org/abc/#x' + + +def test_base_without_path_ignores_base_query(): + assert resolve('abc', 'http://base.org?q') == 'http://base.org/abc' + + +def test_base_without_path_relative_with_dot_segments(): + assert resolve('abc/../def', 'http://base.org') == 'http://base.org/def' + + +def test_base_without_path_with_slash_in_query(): + assert resolve('/', 'http://base.org?a/b') == 'http://base.org/' + + +def test_base_with_slash_in_query(): + assert resolve('jjj', 'http://abc/def?ghi/jkl') == 'http://abc/jjj' + + +def test_base_dot_segment_before_query(): + assert resolve('abc/.?x', 'http://base.org/') == 'http://base.org/abc/?x' + + +def test_base_with_query_dot_before_query(): + assert resolve('.?y', 'http://abc/def/?x') == 'http://abc/def/?y' + + +def test_base_with_query_dot_before_fragment(): + assert resolve('.#y', 'http://abc/def/?x') == 'http://abc/def/#y' + + +def test_base_segment_starting_with_dot(): + assert resolve('.gh', 'http://example.org/a') == 'http://example.org/.gh' + + +def test_base_first_segment_starting_with_dot(): + assert ( + resolve('.x/a', 'http://ex.org/x/yy/zzz/f.ttl') + == 'http://ex.org/x/yy/zzz/.x/a' + ) + + +def test_absolute_dot_segment_before_query(): + assert resolve('http://abc/def/.?x') == 'http://abc/def/?x' + + +def test_network_path_double_dot_segment_before_fragment(): + assert resolve('//abc/def/ghi/..#x', 'http://base.org/') == 'http://abc/def/#x' + + def test_base_only_scheme_slash_slash(): assert resolve('abc', 'http://') == 'http:abc'