Skip to content

Commit

Permalink
Fixed flattening returned @context
Browse files Browse the repository at this point in the history
  • Loading branch information
umbreak committed Sep 15, 2020
1 parent e953c7c commit 99f8ae0
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 63 deletions.
57 changes: 1 addition & 56 deletions core/src/main/java/com/github/jsonldjava/core/Context.java
Expand Up @@ -304,9 +304,7 @@ public Context parse(Object localContext) throws JsonLdError {
*
* http://json-ld.org/spec/latest/json-ld-api/#create-term-definition
*
* @param result
* @param context
* @param key
* @param defined
* @throws JsonLdError
*/
Expand Down Expand Up @@ -572,7 +570,7 @@ else if (relative) {
* the IRI to compact.
* @param value
* the value to check or null.
* @param relativeTo
* @param relativeToVocab
* options for how to compact IRIs: vocab: true to split
* after @vocab, false not to.
* @param reverse
Expand Down Expand Up @@ -1146,57 +1144,4 @@ else if (this.get(JsonLdConsts.LANGUAGE) != null) {
}
return rval;
}

public Map<String, Object> serialize() {
final Map<String, Object> ctx = newMap();
if (this.get(JsonLdConsts.BASE) != null
&& !this.get(JsonLdConsts.BASE).equals(options.getBase())) {
ctx.put(JsonLdConsts.BASE, this.get(JsonLdConsts.BASE));
}
if (this.get(JsonLdConsts.LANGUAGE) != null) {
ctx.put(JsonLdConsts.LANGUAGE, this.get(JsonLdConsts.LANGUAGE));
}
if (this.get(JsonLdConsts.VOCAB) != null) {
ctx.put(JsonLdConsts.VOCAB, this.get(JsonLdConsts.VOCAB));
}
for (final String term : termDefinitions.keySet()) {
final Map<String, Object> definition = (Map<String, Object>) termDefinitions.get(term);
if (definition.get(JsonLdConsts.LANGUAGE) == null
&& definition.get(JsonLdConsts.CONTAINER) == null
&& definition.get(JsonLdConsts.TYPE) == null
&& (definition.get(JsonLdConsts.REVERSE) == null
|| Boolean.FALSE.equals(definition.get(JsonLdConsts.REVERSE)))) {
final String cid = this.compactIri((String) definition.get(JsonLdConsts.ID));
ctx.put(term, term.equals(cid) ? definition.get(JsonLdConsts.ID) : cid);
} else {
final Map<String, Object> defn = newMap();
final String cid = this.compactIri((String) definition.get(JsonLdConsts.ID));
final Boolean reverseProperty = Boolean.TRUE
.equals(definition.get(JsonLdConsts.REVERSE));
if (!(term.equals(cid) && !reverseProperty)) {
defn.put(reverseProperty ? JsonLdConsts.REVERSE : JsonLdConsts.ID, cid);
}
final String typeMapping = (String) definition.get(JsonLdConsts.TYPE);
if (typeMapping != null) {
defn.put(JsonLdConsts.TYPE, JsonLdUtils.isKeyword(typeMapping) ? typeMapping
: compactIri(typeMapping, true));
}
if (definition.get(JsonLdConsts.CONTAINER) != null) {
defn.put(JsonLdConsts.CONTAINER, definition.get(JsonLdConsts.CONTAINER));
}
final Object lang = definition.get(JsonLdConsts.LANGUAGE);
if (definition.get(JsonLdConsts.LANGUAGE) != null) {
defn.put(JsonLdConsts.LANGUAGE, Boolean.FALSE.equals(lang) ? null : lang);
}
ctx.put(term, defn);
}
}

final Map<String, Object> rval = newMap();
if (!(ctx == null || ctx.isEmpty())) {
rval.put(JsonLdConsts.CONTEXT, ctx);
}
return rval;
}

}
Expand Up @@ -245,7 +245,11 @@ public static Object flatten(Object input, Object context, JsonLdOptions opts)
compacted = tmp;
}
final String alias = activeCtx.compactIri(JsonLdConsts.GRAPH);
final Map<String, Object> rval = activeCtx.serialize();
final Map<String, Object> rval = newMap();
final Object returnedContext = returnedContext(context, opts);
if(returnedContext != null) {
rval.put(JsonLdConsts.CONTEXT, returnedContext);
}
rval.put(alias, compacted);
return rval;
}
Expand Down Expand Up @@ -343,7 +347,7 @@ public static Map<String, Object> frame(Object input, Object frame, JsonLdOption
}

/**
* Builds the context to be returned in framing and compaction algorithms.
* Builds the context to be returned in framing, flattening and compaction algorithms.
* In cases where the context is empty or from an unexpected type, it returns null.
* When JsonLdOptions compactArrays is set to true and the context contains a List with a single element,
* the element is returned instead of the list
Expand Down
Expand Up @@ -66,8 +66,8 @@ public void testCompactionSingleRemoteContext() throws Exception {
// System.out.println("\n\nAfter compact:");
// System.out.println(JsonUtils.toPrettyString(compacted));

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

Expand Down
@@ -0,0 +1,78 @@
package com.github.jsonldjava.core;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import com.github.jsonldjava.utils.JsonUtils;
import org.junit.Test;

public class ContextFlatteningTest {

@Test
public void testFlatenning() throws Exception {

final Map<String, Object> contextAbbrevs = new HashMap<>();
contextAbbrevs.put("so", "http://schema.org/");

final Map<String, Object> json = new HashMap<>();
json.put("@context", contextAbbrevs);
json.put("@id", "http://example.org/my_work");

final List<Object> types = new LinkedList<>();
types.add("so:CreativeWork");

json.put("@type", types);
json.put("so:name", "My Work");
json.put("so:url", "http://example.org/my_work");

final JsonLdOptions options = new JsonLdOptions();
options.setBase("http://schema.org/");
options.setCompactArrays(true);
options.setOmitGraph(true);

// System.out.println("Before flattening");
// System.out.println(JsonUtils.toPrettyString(json));

final String flattenStr = "{\"@id\": \"http://schema.org/myid\", \"@context\": \"http://schema.org/\"}";
final Object flatten = JsonUtils.fromString(flattenStr);

final Map<String, Object> flattened = ((Map<String, Object>)JsonLdProcessor.flatten(json, flatten, options));

// System.out.println("\n\nAfter flattening:");
// System.out.println(JsonUtils.toPrettyString(flattened));

assertTrue("Flattening removed the context", flattened.containsKey("@context"));
assertFalse("Flattening of context should be a string, not a list",
flattened.get("@context") instanceof List);
}

@Test
public void testFlatteningRemoteContext() throws Exception {
final String jsonString =
"{\"@context\": {\"@vocab\": \"http://schema.org/\"}, \"knows\": [{\"name\": \"a\"}, {\"name\": \"b\"}] }";
final String flattenStr = "{\"@context\": \"http://schema.org/\"}";

final Object json = JsonUtils.fromString(jsonString);
final Object flatten = JsonUtils.fromString(flattenStr);

// System.out.println("Before flattening");
// System.out.println(JsonUtils.toPrettyString(json));

final JsonLdOptions options = new JsonLdOptions();
options.setOmitGraph(true);

final Map<String, Object> flattened = ((Map<String, Object>)JsonLdProcessor.flatten(json, flatten, options));

// System.out.println("\n\nAfter flattened:");
// System.out.println(JsonUtils.toPrettyString(flattened));

assertEquals("Wrong returned context", "http://schema.org/", flattened.get("@context"));
assertEquals("Wrong number of Json entries",2, flattened.size());
}

}
Expand Up @@ -68,9 +68,9 @@ public void testFramingRemoteContext() throws Exception {
// System.out.println("\n\nAfter framing:");
// System.out.println(JsonUtils.toPrettyString(framed));

assertEquals("Wrong framing context", "http://schema.org/", framed.get("@context"));
assertEquals("Wrong framing id", "schema:myid", framed.get("id"));
assertEquals("Wrong framing type", "Person", framed.get("type"));
assertEquals("Wrong returned context", "http://schema.org/", framed.get("@context"));
assertEquals("Wrong id", "schema:myid", framed.get("id"));
assertEquals("Wrong type", "Person", framed.get("type"));
assertEquals("Wrong number of Json entries",3, framed.size());
}

Expand Down

0 comments on commit 99f8ae0

Please sign in to comment.