Merge "Added collection slash bug"
diff --git a/src/main/java/de/ids_mannheim/korap/query/serialize/QuerySerializer.java b/src/main/java/de/ids_mannheim/korap/query/serialize/QuerySerializer.java
index 92ac1f3..643eb08 100644
--- a/src/main/java/de/ids_mannheim/korap/query/serialize/QuerySerializer.java
+++ b/src/main/java/de/ids_mannheim/korap/query/serialize/QuerySerializer.java
@@ -22,8 +22,13 @@
*/
public class QuerySerializer {
+ // fixme: not used in any way!
+ @Deprecated
static HashMap<String, Class<? extends AbstractQueryProcessor>> qlProcessorAssignment;
+
+
+
static {
qlProcessorAssignment = new HashMap<String, Class<? extends AbstractQueryProcessor>>();
qlProcessorAssignment
@@ -174,11 +179,10 @@
List warnings = (List) requestMap.get("warnings");
List messages = (List) requestMap.get("messages");
this.collection = mergeCollection(collection, this.collection);
- if (this.collection != null && !this.collection.isEmpty())
- requestMap.put("collection", this.collection);
+ requestMap.put("collection", this.collection);
if (this.meta != null) {
- meta.putAll(this.meta);
- requestMap.put("meta", meta);
+ this.meta.putAll(meta);
+ requestMap.put("meta", this.meta);
}
if (this.errors != null && !this.errors.isEmpty()) {
errors.addAll(this.errors);
@@ -193,11 +197,29 @@
requestMap.put("messages", messages);
}
- return requestMap;
+ return cleanup(requestMap);
}
return new HashMap<>();
}
+ private Map<String, Object> cleanup(Map<String, Object> requestMap) {
+ Iterator<Map.Entry<String, Object>> set = requestMap.entrySet()
+ .iterator();
+ while (set.hasNext()) {
+ Map.Entry<String, Object> entry = set.next();
+ if (entry.getValue() instanceof List && ((List) entry.getValue())
+ .isEmpty())
+ set.remove();
+ else if (entry.getValue() instanceof Map && ((Map) entry.getValue())
+ .isEmpty())
+ set.remove();
+ else if (entry.getValue() instanceof String && ((String) entry
+ .getValue()).isEmpty())
+ set.remove();
+ }
+ return requestMap;
+ }
+
private Map<String, Object> mergeCollection(Map<String, Object> collection1,
Map<String, Object> collection2) {
if (collection1 == null || collection1.isEmpty()) {
diff --git a/src/test/java/de/ids_mannheim/korap/query/serialize/EmptyResultsTest.java b/src/test/java/de/ids_mannheim/korap/query/serialize/EmptyResultsTest.java
new file mode 100644
index 0000000..fd2e64f
--- /dev/null
+++ b/src/test/java/de/ids_mannheim/korap/query/serialize/EmptyResultsTest.java
@@ -0,0 +1,44 @@
+package de.ids_mannheim.korap.query.serialize;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import org.junit.Assert;
+import org.junit.Test;
+
+/**
+ * @author hanl
+ * @date 02/07/2015
+ */
+public class EmptyResultsTest {
+
+ private static ObjectMapper mapper = new ObjectMapper();
+
+ @Test
+ public void testEmptyQueryObject() {
+ QuerySerializer s = new QuerySerializer();
+ s.setQuery("prox/unit=word/distance<=5", "cql");
+ JsonNode node = mapper.valueToTree(s.build());
+ Assert.assertEquals(node.has("query"), false);
+ Assert.assertEquals(node.has("collection"), false);
+ }
+
+ @Test
+ public void testEmptyCollectionObject() {
+ QuerySerializer s = new QuerySerializer();
+ s.setQuery("[base=Wort]", "poliqarp");
+
+ JsonNode node = mapper.valueToTree(s.build());
+ Assert.assertEquals(node.has("query"), true);
+ Assert.assertEquals(node.has("collection"), false);
+ }
+
+ @Test
+ public void testEmptyMetaObject() {
+ QuerySerializer s = new QuerySerializer();
+ s.setQuery("[base=Wort]", "poliqarp");
+
+ JsonNode node = mapper.valueToTree(s.build());
+ Assert.assertEquals(node.has("meta"), false);
+ }
+
+}