Skip to content

Commit

Permalink
Ignore @base if remote context is not relative
Browse files Browse the repository at this point in the history
If the remote context is not relative and seems to be a http URI
don't prefix the base IRI to it.

The remote context is not validated nor tested if it's resolvable.
It's just tested if it's not a relative URI - which is totally possible
and would, in conjunction with a base IRI, made into valid remote
context.

See #304.
  • Loading branch information
dr0i committed Jan 26, 2021
1 parent b856235 commit 45c2942
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
6 changes: 5 additions & 1 deletion core/src/main/java/com/github/jsonldjava/core/Context.java
Expand Up @@ -187,7 +187,11 @@ private Context parse(Object localContext, List<String> remoteContexts,
}
// 3.2)
else if (context instanceof String) {
String uri = (String) result.get(JsonLdConsts.BASE);
String uri = null;
// @base is ignored when processing remote contexts, https://github.com/jsonld-java/jsonld-java/issues/304
if (!context.toString().matches("^[hH][tT][tT][pP][sS]?://.*")) {
uri = (String) result.get(JsonLdConsts.BASE);
}
uri = JsonLdUrl.resolve(uri, (String) context);
// 3.2.2
if (remoteContexts.contains(uri)) {
Expand Down
Expand Up @@ -381,11 +381,19 @@ public void testDisallowRemoteContexts() throws Exception {
}

@Test
public void injectContext() throws Exception {
public void testInjectContext() throws Exception {
injectContext(new JsonLdOptions());
}

@Test
public void testIssue304_remoteContextAndBaseIri() throws Exception {
injectContext(new JsonLdOptions("testing:baseIri"));
}

private void injectContext(final JsonLdOptions options) throws Exception {

final Object jsonObject = JsonUtils.fromString(
"{ \"@context\":\"http://nonexisting.example.com/thing\", \"pony\":5 }");
final JsonLdOptions options = new JsonLdOptions();

// Verify fails to find context by default
try {
Expand Down

0 comments on commit 45c2942

Please sign in to comment.