Skip to content

Commit

Permalink
Merge pull request #251 from fsteeg/141-errorOnEmptyKey
Browse files Browse the repository at this point in the history
Throw error on empty key in context
  • Loading branch information
ansell committed Feb 23, 2019
2 parents 47eaff4 + f406471 commit a1075bd
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
13 changes: 12 additions & 1 deletion core/src/main/java/com/github/jsonldjava/core/Context.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,13 @@ public Context(JsonLdOptions opts) {

public Context(Map<String, Object> map, JsonLdOptions opts) {
super(map);
checkEmptyKey(map);
init(opts);
}

public Context(Map<String, Object> map) {
super(map);
checkEmptyKey(map);
init(new JsonLdOptions());
}

Expand Down Expand Up @@ -213,7 +215,7 @@ else if (context instanceof String) {
// 3.3
throw new JsonLdError(Error.INVALID_LOCAL_CONTEXT, context);
}

checkEmptyKey((Map<String, Object>) context);
// 3.4
if (!parsingARemoteContext
&& ((Map<String, Object>) context).containsKey(JsonLdConsts.BASE)) {
Expand Down Expand Up @@ -284,6 +286,15 @@ else if (context instanceof String) {
return result;
}

private void checkEmptyKey(final Map<String, Object> map) {
if (map.containsKey("")) {
// the term MUST NOT be an empty string ("")
// https://www.w3.org/TR/json-ld/#h3_terms
throw new JsonLdError(Error.INVALID_TERM_DEFINITION,
String.format("empty key for value '%s'", map.get("")));
}
}

public Context parse(Object localContext) throws JsonLdError {
return this.parse(localContext, new ArrayList<String>());
}
Expand Down
27 changes: 27 additions & 0 deletions core/src/test/java/com/github/jsonldjava/core/ContextTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,37 @@

import org.junit.Test;

import com.google.common.collect.ImmutableMap;

public class ContextTest {

@Test
public void testRemoveBase() {
// TODO: test if Context.removeBase actually works
}

// See https://github.com/jsonld-java/jsonld-java/issues/141

@Test(expected = JsonLdError.class)
public void testIssue141_errorOnEmptyKey_compact() {
JsonLdProcessor.compact(ImmutableMap.of(),
ImmutableMap.of("","http://example.com"), new JsonLdOptions());
}

@Test(expected = JsonLdError.class)
public void testIssue141_errorOnEmptyKey_expand() {
JsonLdProcessor.expand(ImmutableMap.of("@context",
ImmutableMap.of("","http://example.com")), new JsonLdOptions());
}

@Test(expected = JsonLdError.class)
public void testIssue141_errorOnEmptyKey_newContext1() {
new Context(ImmutableMap.of("","http://example.com"));
}

@Test(expected = JsonLdError.class)
public void testIssue141_errorOnEmptyKey_newContext2() {
new Context(ImmutableMap.of("","http://example.com"), new JsonLdOptions());
}

}

0 comments on commit a1075bd

Please sign in to comment.