Skip to content
Merged
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
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());
}

}