Skip to content

Commit fce53a4

Browse files
committed
Handle opaque/path-rootless base IRIs
Add new spec tests, remove custom tests See: #232 w3c/json-ld-api#103
1 parent 4eb40f9 commit fce53a4

File tree

10 files changed

+54
-44
lines changed

10 files changed

+54
-44
lines changed

core/src/main/java/com/github/jsonldjava/utils/JsonLdUrl.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -261,23 +261,30 @@ public static String resolve(String baseUri, String pathToResolve) {
261261
}
262262
try {
263263
URI uri = new URI(baseUri);
264+
// URI#resolve drops base scheme for opaque URIs, https://github.com/jsonld-java/jsonld-java/issues/232
265+
if (uri.isOpaque()) {
266+
String basePath = uri.getPath() != null ? uri.getPath() : uri.getSchemeSpecificPart();
267+
// Drop the last segment, see https://tools.ietf.org/html/rfc3986#section-5.2.3 (2nd bullet point)
268+
basePath = basePath.contains("/") ? basePath.substring(0, basePath.lastIndexOf('/') + 1) : "";
269+
return new URI(uri.getScheme(), basePath + pathToResolve, null).toString();
270+
}
264271
// "a base URI [...] does not allow a fragment" (https://tools.ietf.org/html/rfc3986#section-4.3)
265272
uri = new URI(uri.getScheme(), uri.getAuthority(), uri.getPath(), uri.getQuery(), null);
266273
// query string parsing
267274
if (pathToResolve.startsWith("?")) {
268275
// drop query, https://tools.ietf.org/html/rfc3986#section-5.2.2: T.query = R.query;
269276
uri = new URI(uri.getScheme(), uri.getAuthority(), uri.getPath(), null, null);
270-
// add query to the end manually (as URI.resolve does it wrong)
277+
// add query to the end manually (as URI#resolve does it wrong)
271278
return uri.toString() + pathToResolve;
272279
} else if (pathToResolve.startsWith("#")) {
273-
// add fragment to the end manually (as URI.resolve does it wrong)
280+
// add fragment to the end manually (as URI#resolve does it wrong)
274281
return uri.toString() + pathToResolve;
275282
}
276283
uri = uri.resolve(pathToResolve);
277284
// java doesn't discard unnecessary dot segments
278285
String path = uri.getPath();
279286
if (path != null) {
280-
path = JsonLdUrl.removeDotSegments(uri.getPath(), true);
287+
path = JsonLdUrl.removeDotSegments(path, true);
281288
}
282289
return new URI(uri.getScheme(), uri.getAuthority(), path, uri.getQuery(),
283290
uri.getFragment()).toString();

core/src/test/java/com/github/jsonldjava/core/ToRDFTest.java renamed to core/src/test/java/com/github/jsonldjava/core/ArrayContextToRDFTest.java

Lines changed: 1 addition & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,13 @@
44
import static org.junit.Assert.assertFalse;
55
import static org.junit.Assert.assertNotNull;
66

7-
import java.io.BufferedReader;
8-
import java.io.IOException;
9-
import java.io.InputStreamReader;
107
import java.net.URL;
11-
import java.nio.charset.StandardCharsets;
12-
import java.util.List;
13-
import java.util.stream.Collectors;
148

15-
import org.junit.Ignore;
169
import org.junit.Test;
1710

1811
import com.github.jsonldjava.utils.JsonUtils;
19-
import com.github.jsonldjava.utils.TestUtils;
2012

21-
public class ToRDFTest {
13+
public class ArrayContextToRDFTest {
2214
@Test
2315
public void toRdfWithNamespace() throws Exception {
2416

@@ -51,35 +43,4 @@ public RemoteDocument loadDocument(String url) throws JsonLdError {
5143
assertFalse(rdf.getNamespaces().containsKey("term1"));
5244

5345
}
54-
55-
@Test
56-
// See https://github.com/jsonld-java/jsonld-java/issues/232
57-
public void toRdfWithHttpBaseIri() throws IOException, JsonLdError {
58-
testToRdf("/custom/toRdf-0001-in.jsonld", "/custom/toRdf-0001-out.nq", "http://example.org/");
59-
}
60-
61-
@Test
62-
// See https://github.com/jsonld-java/jsonld-java/issues/232
63-
public void toRdfWithHierarchicalBaseIri() throws IOException, JsonLdError {
64-
testToRdf("/custom/toRdf-0001-in.jsonld", "/custom/toRdf-0002-out.nq", "tag:/example/");
65-
}
66-
67-
@Test
68-
@Ignore
69-
// See https://github.com/jsonld-java/jsonld-java/issues/232#issuecomment-493454096
70-
public void toRdfWithOpaqueBaseIri() throws IOException, JsonLdError {
71-
testToRdf("/custom/toRdf-0001-in.jsonld", "/custom/toRdf-0003-out.nq", "tag:example/");
72-
}
73-
74-
private void testToRdf(String inFile, String outFile, String baseIri) throws IOException {
75-
final Object input = JsonUtils
76-
.fromInputStream(getClass().getResourceAsStream(inFile));
77-
List<String> resultLines = new BufferedReader(new InputStreamReader(
78-
getClass().getResourceAsStream(outFile), StandardCharsets.UTF_8)).lines()
79-
.collect(Collectors.toList());
80-
JsonLdOptions options = new JsonLdOptions(baseIri);
81-
options.format = JsonLdConsts.APPLICATION_NQUADS;
82-
Object result = JsonLdProcessor.toRDF(input, options);
83-
assertEquals(TestUtils.join(resultLines, "\n").trim(), ((String) result).trim());
84-
}
8546
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<tag:example/relativeURIWithNoBase> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://example.org/SomeRDFSClass> .
1+
<tag:relativeURIWithNoBase> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://example.org/SomeRDFSClass> .
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"@context": {"@base": "tag:example", "urn:ex:p": {"@type": "@id"}},
3+
"@graph": [
4+
{"@id": "urn:ex:s307", "urn:ex:p": "a"}
5+
]
6+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<urn:ex:s307> <urn:ex:p> <tag:a> .
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"@context": {"@base": "tag:example/foo", "urn:ex:p": {"@type": "@id"}},
3+
"@graph": [
4+
{"@id": "urn:ex:s308", "urn:ex:p": "a"}
5+
]
6+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<urn:ex:s308> <urn:ex:p> <tag:example/a> .
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"@context": {"@base": "tag:example/foo/", "urn:ex:p": {"@type": "@id"}},
3+
"@graph": [
4+
{"@id": "urn:ex:s309", "urn:ex:p": "a"}
5+
]
6+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<urn:ex:s309> <urn:ex:p> <tag:example/foo/a> .

core/src/test/resources/json-ld.org/toRdf-manifest.jsonld

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -863,6 +863,27 @@
863863
"purpose": "IRI resolution according to RFC3986.",
864864
"input": "toRdf-0129-in.jsonld",
865865
"expect": "toRdf-0129-out.nq"
866+
}, {
867+
"@id": "#t0130",
868+
"@type": ["jld:PositiveEvaluationTest", "jld:ToRDFTest"],
869+
"name": "IRI Resolution (10)",
870+
"purpose": "IRI resolution according to RFC3986.",
871+
"input": "toRdf-0130-in.jsonld",
872+
"expect": "toRdf-0130-out.nq"
873+
}, {
874+
"@id": "#t0131",
875+
"@type": ["jld:PositiveEvaluationTest", "jld:ToRDFTest"],
876+
"name": "IRI Resolution (11)",
877+
"purpose": "IRI resolution according to RFC3986.",
878+
"input": "toRdf-0131-in.jsonld",
879+
"expect": "toRdf-0131-out.nq"
880+
}, {
881+
"@id": "#t0132",
882+
"@type": ["jld:PositiveEvaluationTest", "jld:ToRDFTest"],
883+
"name": "IRI Resolution (12)",
884+
"purpose": "IRI resolution according to RFC3986.",
885+
"input": "toRdf-0132-in.jsonld",
886+
"expect": "toRdf-0132-out.nq"
866887
}
867888
]
868889
}

0 commit comments

Comments
 (0)