Deserialization of span/wrap

Change-Id: Ib0989429aad471b18a3c3a3fa2c9a60cbf49a1d4
diff --git a/Changes b/Changes
index 04373a8..2411416 100644
--- a/Changes
+++ b/Changes
@@ -18,7 +18,8 @@
 	- [bugfix] Remove legacy error handling in Match (diewald)
 	- [bugfix] JSON-serialization bug in match in case of error messages (diewald)
 	- [bugfix] 'fields' serialization (diewald)
-	- [bugfix] deserialization of group:disjunction (diewald)
+	- [bugfix] Deserialization of group:disjunction (diewald)
+	- [bugfix] Deserialization of spn/wrap (diewald)
 
 0.51 2015-03-17
         - This is a major version (prepared for the GitHub release)
diff --git a/src/main/java/de/ids_mannheim/korap/KrillQuery.java b/src/main/java/de/ids_mannheim/korap/KrillQuery.java
index 19377fd..fa649ab 100644
--- a/src/main/java/de/ids_mannheim/korap/KrillQuery.java
+++ b/src/main/java/de/ids_mannheim/korap/KrillQuery.java
@@ -301,7 +301,10 @@
                 return this._segFromJson(json.get("wrap"));
 
             case "koral:span":
-                return this._termFromJson(json);
+                if (!json.has("wrap"))
+                    return this._termFromJson(json);
+
+                return this._termFromJson(json.get("wrap"));
         };
 
         // Unknown query type
@@ -503,7 +506,6 @@
     private JsonNode _extractReferentClass (JsonNode node, int classNum) {
         JsonNode referent;
         if (node.has("classOut") && node.get("classOut").asInt() == classNum) {
-            // System.out.println("found: " + node.toString());
             return node;
         }
         else {
@@ -1031,7 +1033,6 @@
     // Deserialize koral:term
     private SpanQueryWrapper _termFromJson (JsonNode json, String direction)
             throws QueryException {
-
         if (!json.has("key") || json.get("key").asText().length() < 1) {
             if (!json.has("attr"))
                 throw new QueryException(740,
diff --git a/src/test/java/de/ids_mannheim/korap/query/TestKrillQueryJSON.java b/src/test/java/de/ids_mannheim/korap/query/TestKrillQueryJSON.java
index 4ffbb78..6914dd7 100644
--- a/src/test/java/de/ids_mannheim/korap/query/TestKrillQueryJSON.java
+++ b/src/test/java/de/ids_mannheim/korap/query/TestKrillQueryJSON.java
@@ -476,6 +476,16 @@
 
 
     @Test
+    public void queryJSONspanWrapDeserializationBug () throws QueryException {
+        // contains(<s>, Erde  []* Sonne)
+        SpanQueryWrapper sqwi = jsonQuery(getClass().getResource(
+                "/queries/bugs/unspecified_key_bug.jsonld").getFile());
+
+        assertEquals(sqwi.toQuery().toString(), "spanContain(tokens:s, spanDistance(tokens:s:Erde, tokens:s:Sonne, [(w[0:100], ordered, notExcluded)]))");
+    };
+
+
+    @Test
     public void queryJSONflags2 () throws QueryException {
         // buchstabe/i
         try {
diff --git a/src/test/resources/queries/bugs/unspecified_key_bug.jsonld b/src/test/resources/queries/bugs/unspecified_key_bug.jsonld
new file mode 100644
index 0000000..afc434c
--- /dev/null
+++ b/src/test/resources/queries/bugs/unspecified_key_bug.jsonld
@@ -0,0 +1,66 @@
+{
+  "@context":"http://korap.ids-mannheim.de/ns/koral/0.3/context.jsonld",
+  "errors":[],
+  "warnings":[],
+  "messages":[
+    [303,"Deprecated 2014-07-24: 'min' and 'max' to be supported until 3 months from deprecation date."]
+  ],
+  "query":{
+    "@type":"koral:group",
+    "operation":"operation:position",
+    "frames":[
+      "frames:isAround"
+    ],
+    "operands":[
+      {
+	"@type":"koral:span",
+	"wrap":{
+	  "@type":"koral:term",
+	  "key":"s"
+	}
+      },
+      {
+	"@type":"koral:group",
+	"operation":"operation:sequence",
+	"operands":[
+	  {
+	    "@type":"koral:token",
+	    "wrap":{
+	      "@type":"koral:term",
+	      "layer":"orth",
+	      "key":"Erde",
+	      "match":"match:eq"
+	    }
+	  },
+	  {
+	    "@type":"koral:token",
+	    "wrap":{
+	      "@type":"koral:term",
+	      "layer":"orth",
+	      "key":"Sonne",
+	      "match":"match:eq"
+	    }
+	  }
+	],
+	"inOrder":true,
+	"distances":[
+	  {
+	    "@type":"koral:distance",
+	    "key":"w",
+	    "boundary":{
+	      "@type":"koral:boundary",
+	      "min":0
+	    },
+	    "min":0
+	  }
+	]
+      }
+    ]
+  },
+  "meta":{
+    "startPage":1,
+    "count":25,
+    "cutOff":true,
+    "context":"base/s:p"
+  }
+}