Skip to content

Commit 99f8ae0

Browse files
committed
Fixed flattening returned @context
1 parent e953c7c commit 99f8ae0

File tree

5 files changed

+90
-63
lines changed

5 files changed

+90
-63
lines changed

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

Lines changed: 1 addition & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -304,9 +304,7 @@ public Context parse(Object localContext) throws JsonLdError {
304304
*
305305
* http://json-ld.org/spec/latest/json-ld-api/#create-term-definition
306306
*
307-
* @param result
308307
* @param context
309-
* @param key
310308
* @param defined
311309
* @throws JsonLdError
312310
*/
@@ -572,7 +570,7 @@ else if (relative) {
572570
* the IRI to compact.
573571
* @param value
574572
* the value to check or null.
575-
* @param relativeTo
573+
* @param relativeToVocab
576574
* options for how to compact IRIs: vocab: true to split
577575
* after @vocab, false not to.
578576
* @param reverse
@@ -1146,57 +1144,4 @@ else if (this.get(JsonLdConsts.LANGUAGE) != null) {
11461144
}
11471145
return rval;
11481146
}
1149-
1150-
public Map<String, Object> serialize() {
1151-
final Map<String, Object> ctx = newMap();
1152-
if (this.get(JsonLdConsts.BASE) != null
1153-
&& !this.get(JsonLdConsts.BASE).equals(options.getBase())) {
1154-
ctx.put(JsonLdConsts.BASE, this.get(JsonLdConsts.BASE));
1155-
}
1156-
if (this.get(JsonLdConsts.LANGUAGE) != null) {
1157-
ctx.put(JsonLdConsts.LANGUAGE, this.get(JsonLdConsts.LANGUAGE));
1158-
}
1159-
if (this.get(JsonLdConsts.VOCAB) != null) {
1160-
ctx.put(JsonLdConsts.VOCAB, this.get(JsonLdConsts.VOCAB));
1161-
}
1162-
for (final String term : termDefinitions.keySet()) {
1163-
final Map<String, Object> definition = (Map<String, Object>) termDefinitions.get(term);
1164-
if (definition.get(JsonLdConsts.LANGUAGE) == null
1165-
&& definition.get(JsonLdConsts.CONTAINER) == null
1166-
&& definition.get(JsonLdConsts.TYPE) == null
1167-
&& (definition.get(JsonLdConsts.REVERSE) == null
1168-
|| Boolean.FALSE.equals(definition.get(JsonLdConsts.REVERSE)))) {
1169-
final String cid = this.compactIri((String) definition.get(JsonLdConsts.ID));
1170-
ctx.put(term, term.equals(cid) ? definition.get(JsonLdConsts.ID) : cid);
1171-
} else {
1172-
final Map<String, Object> defn = newMap();
1173-
final String cid = this.compactIri((String) definition.get(JsonLdConsts.ID));
1174-
final Boolean reverseProperty = Boolean.TRUE
1175-
.equals(definition.get(JsonLdConsts.REVERSE));
1176-
if (!(term.equals(cid) && !reverseProperty)) {
1177-
defn.put(reverseProperty ? JsonLdConsts.REVERSE : JsonLdConsts.ID, cid);
1178-
}
1179-
final String typeMapping = (String) definition.get(JsonLdConsts.TYPE);
1180-
if (typeMapping != null) {
1181-
defn.put(JsonLdConsts.TYPE, JsonLdUtils.isKeyword(typeMapping) ? typeMapping
1182-
: compactIri(typeMapping, true));
1183-
}
1184-
if (definition.get(JsonLdConsts.CONTAINER) != null) {
1185-
defn.put(JsonLdConsts.CONTAINER, definition.get(JsonLdConsts.CONTAINER));
1186-
}
1187-
final Object lang = definition.get(JsonLdConsts.LANGUAGE);
1188-
if (definition.get(JsonLdConsts.LANGUAGE) != null) {
1189-
defn.put(JsonLdConsts.LANGUAGE, Boolean.FALSE.equals(lang) ? null : lang);
1190-
}
1191-
ctx.put(term, defn);
1192-
}
1193-
}
1194-
1195-
final Map<String, Object> rval = newMap();
1196-
if (!(ctx == null || ctx.isEmpty())) {
1197-
rval.put(JsonLdConsts.CONTEXT, ctx);
1198-
}
1199-
return rval;
1200-
}
1201-
12021147
}

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,11 @@ public static Object flatten(Object input, Object context, JsonLdOptions opts)
245245
compacted = tmp;
246246
}
247247
final String alias = activeCtx.compactIri(JsonLdConsts.GRAPH);
248-
final Map<String, Object> rval = activeCtx.serialize();
248+
final Map<String, Object> rval = newMap();
249+
final Object returnedContext = returnedContext(context, opts);
250+
if(returnedContext != null) {
251+
rval.put(JsonLdConsts.CONTEXT, returnedContext);
252+
}
249253
rval.put(alias, compacted);
250254
return rval;
251255
}
@@ -343,7 +347,7 @@ public static Map<String, Object> frame(Object input, Object frame, JsonLdOption
343347
}
344348

345349
/**
346-
* Builds the context to be returned in framing and compaction algorithms.
350+
* Builds the context to be returned in framing, flattening and compaction algorithms.
347351
* In cases where the context is empty or from an unexpected type, it returns null.
348352
* When JsonLdOptions compactArrays is set to true and the context contains a List with a single element,
349353
* the element is returned instead of the list

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ public void testCompactionSingleRemoteContext() throws Exception {
6666
// System.out.println("\n\nAfter compact:");
6767
// System.out.println(JsonUtils.toPrettyString(compacted));
6868

69-
assertEquals("Wrong compaction context", "http://schema.org/", compacted.get("@context"));
70-
assertEquals("Wrong framing type", "Person", compacted.get("type"));
69+
assertEquals("Wrong returned context", "http://schema.org/", compacted.get("@context"));
70+
assertEquals("Wrong type", "Person", compacted.get("type"));
7171
assertEquals("Wrong number of Json entries",2, compacted.size());
7272
}
7373

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package com.github.jsonldjava.core;
2+
3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertFalse;
5+
import static org.junit.Assert.assertTrue;
6+
import java.util.HashMap;
7+
import java.util.LinkedList;
8+
import java.util.List;
9+
import java.util.Map;
10+
11+
import com.github.jsonldjava.utils.JsonUtils;
12+
import org.junit.Test;
13+
14+
public class ContextFlatteningTest {
15+
16+
@Test
17+
public void testFlatenning() throws Exception {
18+
19+
final Map<String, Object> contextAbbrevs = new HashMap<>();
20+
contextAbbrevs.put("so", "http://schema.org/");
21+
22+
final Map<String, Object> json = new HashMap<>();
23+
json.put("@context", contextAbbrevs);
24+
json.put("@id", "http://example.org/my_work");
25+
26+
final List<Object> types = new LinkedList<>();
27+
types.add("so:CreativeWork");
28+
29+
json.put("@type", types);
30+
json.put("so:name", "My Work");
31+
json.put("so:url", "http://example.org/my_work");
32+
33+
final JsonLdOptions options = new JsonLdOptions();
34+
options.setBase("http://schema.org/");
35+
options.setCompactArrays(true);
36+
options.setOmitGraph(true);
37+
38+
// System.out.println("Before flattening");
39+
// System.out.println(JsonUtils.toPrettyString(json));
40+
41+
final String flattenStr = "{\"@id\": \"http://schema.org/myid\", \"@context\": \"http://schema.org/\"}";
42+
final Object flatten = JsonUtils.fromString(flattenStr);
43+
44+
final Map<String, Object> flattened = ((Map<String, Object>)JsonLdProcessor.flatten(json, flatten, options));
45+
46+
// System.out.println("\n\nAfter flattening:");
47+
// System.out.println(JsonUtils.toPrettyString(flattened));
48+
49+
assertTrue("Flattening removed the context", flattened.containsKey("@context"));
50+
assertFalse("Flattening of context should be a string, not a list",
51+
flattened.get("@context") instanceof List);
52+
}
53+
54+
@Test
55+
public void testFlatteningRemoteContext() throws Exception {
56+
final String jsonString =
57+
"{\"@context\": {\"@vocab\": \"http://schema.org/\"}, \"knows\": [{\"name\": \"a\"}, {\"name\": \"b\"}] }";
58+
final String flattenStr = "{\"@context\": \"http://schema.org/\"}";
59+
60+
final Object json = JsonUtils.fromString(jsonString);
61+
final Object flatten = JsonUtils.fromString(flattenStr);
62+
63+
// System.out.println("Before flattening");
64+
// System.out.println(JsonUtils.toPrettyString(json));
65+
66+
final JsonLdOptions options = new JsonLdOptions();
67+
options.setOmitGraph(true);
68+
69+
final Map<String, Object> flattened = ((Map<String, Object>)JsonLdProcessor.flatten(json, flatten, options));
70+
71+
// System.out.println("\n\nAfter flattened:");
72+
// System.out.println(JsonUtils.toPrettyString(flattened));
73+
74+
assertEquals("Wrong returned context", "http://schema.org/", flattened.get("@context"));
75+
assertEquals("Wrong number of Json entries",2, flattened.size());
76+
}
77+
78+
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,9 @@ public void testFramingRemoteContext() throws Exception {
6868
// System.out.println("\n\nAfter framing:");
6969
// System.out.println(JsonUtils.toPrettyString(framed));
7070

71-
assertEquals("Wrong framing context", "http://schema.org/", framed.get("@context"));
72-
assertEquals("Wrong framing id", "schema:myid", framed.get("id"));
73-
assertEquals("Wrong framing type", "Person", framed.get("type"));
71+
assertEquals("Wrong returned context", "http://schema.org/", framed.get("@context"));
72+
assertEquals("Wrong id", "schema:myid", framed.get("id"));
73+
assertEquals("Wrong type", "Person", framed.get("type"));
7474
assertEquals("Wrong number of Json entries",3, framed.size());
7575
}
7676

0 commit comments

Comments
 (0)