Skip to content

Commit f406471

Browse files
committed
Throw error on empty key in context (see #141)
1 parent 47eaff4 commit f406471

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

core/src/main/java/com/github/jsonldjava/core/Context.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,13 @@ public Context(JsonLdOptions opts) {
4040

4141
public Context(Map<String, Object> map, JsonLdOptions opts) {
4242
super(map);
43+
checkEmptyKey(map);
4344
init(opts);
4445
}
4546

4647
public Context(Map<String, Object> map) {
4748
super(map);
49+
checkEmptyKey(map);
4850
init(new JsonLdOptions());
4951
}
5052

@@ -213,7 +215,7 @@ else if (context instanceof String) {
213215
// 3.3
214216
throw new JsonLdError(Error.INVALID_LOCAL_CONTEXT, context);
215217
}
216-
218+
checkEmptyKey((Map<String, Object>) context);
217219
// 3.4
218220
if (!parsingARemoteContext
219221
&& ((Map<String, Object>) context).containsKey(JsonLdConsts.BASE)) {
@@ -284,6 +286,15 @@ else if (context instanceof String) {
284286
return result;
285287
}
286288

289+
private void checkEmptyKey(final Map<String, Object> map) {
290+
if (map.containsKey("")) {
291+
// the term MUST NOT be an empty string ("")
292+
// https://www.w3.org/TR/json-ld/#h3_terms
293+
throw new JsonLdError(Error.INVALID_TERM_DEFINITION,
294+
String.format("empty key for value '%s'", map.get("")));
295+
}
296+
}
297+
287298
public Context parse(Object localContext) throws JsonLdError {
288299
return this.parse(localContext, new ArrayList<String>());
289300
}

core/src/test/java/com/github/jsonldjava/core/ContextTest.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,37 @@
22

33
import org.junit.Test;
44

5+
import com.google.common.collect.ImmutableMap;
6+
57
public class ContextTest {
68

79
@Test
810
public void testRemoveBase() {
911
// TODO: test if Context.removeBase actually works
1012
}
13+
14+
// See https://github.com/jsonld-java/jsonld-java/issues/141
15+
16+
@Test(expected = JsonLdError.class)
17+
public void testIssue141_errorOnEmptyKey_compact() {
18+
JsonLdProcessor.compact(ImmutableMap.of(),
19+
ImmutableMap.of("","http://example.com"), new JsonLdOptions());
20+
}
21+
22+
@Test(expected = JsonLdError.class)
23+
public void testIssue141_errorOnEmptyKey_expand() {
24+
JsonLdProcessor.expand(ImmutableMap.of("@context",
25+
ImmutableMap.of("","http://example.com")), new JsonLdOptions());
26+
}
27+
28+
@Test(expected = JsonLdError.class)
29+
public void testIssue141_errorOnEmptyKey_newContext1() {
30+
new Context(ImmutableMap.of("","http://example.com"));
31+
}
32+
33+
@Test(expected = JsonLdError.class)
34+
public void testIssue141_errorOnEmptyKey_newContext2() {
35+
new Context(ImmutableMap.of("","http://example.com"), new JsonLdOptions());
36+
}
37+
1138
}

0 commit comments

Comments
 (0)