Skip to content

Commit b784f3f

Browse files
committed
Returned correct @context during framing
1 parent 47a595a commit b784f3f

File tree

3 files changed

+109
-20
lines changed

3 files changed

+109
-20
lines changed

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

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,6 @@ public static Map<String, Object> compact(Object input, Object context, JsonLdOp
5353
if (context instanceof Map
5454
&& ((Map<String, Object>) context).containsKey(JsonLdConsts.CONTEXT)) {
5555
context = ((Map<String, Object>) context).get(JsonLdConsts.CONTEXT);
56-
if(context instanceof String) {
57-
context = new LinkedList<>(Arrays.asList((String) context));
58-
}
5956
}
6057
Context activeCtx = new Context(opts);
6158
activeCtx = activeCtx.parse(context);
@@ -75,19 +72,12 @@ public static Map<String, Object> compact(Object input, Object context, JsonLdOp
7572
compacted = tmp;
7673
}
7774
}
78-
if (compacted != null && context != null) {
79-
// TODO: figure out if we can make "@context" appear at the start of
80-
// the keySet
81-
if ((context instanceof Map && !((Map<String, Object>) context).isEmpty())
82-
|| (context instanceof List && !((List<Object>) context).isEmpty())) {
83-
84-
if (context instanceof List && ((List<Object>) context).size() == 1
85-
&& opts.getCompactArrays()) {
86-
((Map<String, Object>) compacted).put(JsonLdConsts.CONTEXT,
87-
((List<Object>) context).get(0));
88-
} else {
89-
((Map<String, Object>) compacted).put(JsonLdConsts.CONTEXT, context);
90-
}
75+
if (compacted != null) {
76+
final Object returnedContext = returnedContext(context, opts);
77+
if(returnedContext != null) {
78+
// TODO: figure out if we can make "@context" appear at the start of
79+
// the keySet
80+
((Map<String, Object>) compacted).put(JsonLdConsts.CONTEXT, returnedContext);
9181
}
9282
}
9383

@@ -324,14 +314,18 @@ public static Map<String, Object> frame(Object input, Object frame, JsonLdOption
324314
// to a new empty
325315
// context, otherwise.
326316
final JsonLdApi api = new JsonLdApi(expandedInput, opts);
327-
final Context activeCtx = api.context
328-
.parse(((Map<String, Object>) frame).get(JsonLdConsts.CONTEXT));
317+
final Object context = ((Map<String, Object>) frame).get(JsonLdConsts.CONTEXT);
318+
final Context activeCtx = api.context.parse(context);
329319
final List<Object> framed = api.frame(expandedInput, expandedFrame);
330320
if (opts.getPruneBlankNodeIdentifiers()) {
331321
JsonLdUtils.pruneBlankNodes(framed);
332322
}
333323
Object compacted = api.compact(activeCtx, null, framed, opts.getCompactArrays());
334-
final Map<String, Object> rval = activeCtx.serialize();
324+
final Map<String, Object> rval = newMap();
325+
final Object returnedContext = returnedContext(context, opts);
326+
if(returnedContext != null) {
327+
rval.put(JsonLdConsts.CONTEXT, context);
328+
}
335329
final boolean addGraph = ((!(compacted instanceof List)) && !opts.getOmitGraph());
336330
if (addGraph && !(compacted instanceof List)) {
337331
final List<Object> tmp = new ArrayList<Object>();
@@ -348,6 +342,22 @@ public static Map<String, Object> frame(Object input, Object frame, JsonLdOption
348342
return rval;
349343
}
350344

345+
private static Object returnedContext(Object context, JsonLdOptions opts) {
346+
if (context != null &&
347+
((context instanceof Map && !((Map<String, Object>) context).isEmpty())
348+
|| (context instanceof List && !((List<Object>) context).isEmpty())
349+
|| (context instanceof String && !((String) context).isEmpty()))) {
350+
351+
if (context instanceof List && ((List<Object>) context).size() == 1
352+
&& opts.getCompactArrays()) {
353+
return ((List<Object>) context).get(0);
354+
}
355+
return context;
356+
} else {
357+
return null;
358+
}
359+
}
360+
351361
/**
352362
* A registry for RDF Parsers (in this case, JSONLDSerializers) used by
353363
* fromRDF if no specific serializer is specified and options.format is set.

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public void testCompaction() throws Exception {
5252
}
5353

5454
@Test
55-
public void testCompactionUriSingleContext() throws Exception {
55+
public void testCompactionSingleRemoteContext() throws Exception {
5656
final String jsonString = "[{\"@type\": [\"http://schema.org/Person\"] } ]";
5757
final String ctxStr = "{\"@context\": \"http://schema.org/\"}";
5858

@@ -67,6 +67,8 @@ public void testCompactionUriSingleContext() throws Exception {
6767
// System.out.println(JsonUtils.toPrettyString(compacted));
6868

6969
assertEquals("Wrong compaction context", "http://schema.org/", compacted.get("@context"));
70+
assertEquals("Wrong framing type", "Person", compacted.get("type"));
71+
assertEquals("Wrong number of Json entries",2, compacted.size());
7072
}
7173

7274
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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 ContextFramingTest {
15+
16+
// @Ignore("Disable until schema.org is fixed")
17+
@Test
18+
public void testFraming() throws Exception {
19+
20+
final Map<String, Object> contextAbbrevs = new HashMap<String, Object>();
21+
contextAbbrevs.put("so", "http://schema.org/");
22+
23+
final Map<String, Object> json = new HashMap<String, Object>();
24+
json.put("@context", contextAbbrevs);
25+
json.put("@id", "http://example.org/my_work");
26+
27+
final List<Object> types = new LinkedList<Object>();
28+
types.add("so:CreativeWork");
29+
30+
json.put("@type", types);
31+
json.put("so:name", "My Work");
32+
json.put("so:url", "http://example.org/my_work");
33+
34+
final JsonLdOptions options = new JsonLdOptions();
35+
options.setBase("http://schema.org/");
36+
options.setCompactArrays(true);
37+
options.setOmitGraph(true);
38+
39+
// System.out.println("Before compact");
40+
// System.out.println(JsonUtils.toPrettyString(json));
41+
42+
final String frameStr = "{\"@id\": \"http://schema.org/myid\", \"@context\": \"http://schema.org/\"}";
43+
final Object frame = JsonUtils.fromString(frameStr);
44+
45+
final Map<String, Object> compacted = JsonLdProcessor.frame(json, frame, options);
46+
47+
// System.out.println("\n\nAfter compact:");
48+
// System.out.println(JsonUtils.toPrettyString(compacted));
49+
50+
assertTrue("Framing removed the context", compacted.containsKey("@context"));
51+
assertFalse("Framing of context should be a string, not a list",
52+
compacted.get("@context") instanceof List);
53+
}
54+
55+
@Test
56+
public void testFramingRemoteContext() throws Exception {
57+
final String jsonString = "{\"@id\": \"http://schema.org/myid\", \"@type\": [\"http://schema.org/Person\"]}";
58+
final String frameStr = "{\"@id\": \"http://schema.org/myid\", \"@context\": \"http://schema.org/\"}";
59+
60+
final Object json = JsonUtils.fromString(jsonString);
61+
final Object frame = JsonUtils.fromString(frameStr);
62+
63+
final JsonLdOptions options = new JsonLdOptions();
64+
options.setOmitGraph(true);
65+
66+
final Map<String, Object> compacted = JsonLdProcessor.frame(json, frame, options);
67+
68+
// System.out.println("\n\nAfter compact:");
69+
// System.out.println(JsonUtils.toPrettyString(compacted));
70+
71+
assertEquals("Wrong framing context", "http://schema.org/", compacted.get("@context"));
72+
assertEquals("Wrong framing id", "schema:myid", compacted.get("id"));
73+
assertEquals("Wrong framing type", "Person", compacted.get("type"));
74+
assertEquals("Wrong number of Json entries",3, compacted.size());
75+
}
76+
77+
}

0 commit comments

Comments
 (0)