SOLR-12362: JSON parsing: upgrade to List on end of array not start. And fix TestJsonRecordReader assumptions RE null keys

This commit is contained in:
David Smiley 2018-06-15 00:46:17 -04:00
parent fba42bbac6
commit 4799bc5a4a
2 changed files with 13 additions and 13 deletions

View File

@ -348,12 +348,17 @@ public class JsonRecordReader {
} else if (event == ARRAY_START) {
for (; ; ) {
event = parser.nextEvent();
if (event == ARRAY_END) break;
if (event == OBJECT_START) {
// if single item in array will still be added as array
if(!values.containsKey(name)) {
values.put(name, new ArrayList<>());
if (event == ARRAY_END) {
// ensure that the value is of type List
final Object val = values.get(name);
if (val != null && !(val instanceof List)) {
final ArrayList listVal = new ArrayList(1);
listVal.add(val);
values.put(name, listVal);
}
break;
}
if (event == OBJECT_START) {
walkObject();
}
}
@ -437,7 +442,6 @@ public class JsonRecordReader {
for (String fld : valuesAddedinThisFrame) {
values.remove(fld);
}
values.remove(null);
}
}
}

View File

@ -18,7 +18,6 @@ package org.apache.solr.common.util;
import java.io.IOException;
import java.io.StringReader;
import java.lang.invoke.MethodHandles;
import java.lang.ref.WeakReference;
import java.util.Arrays;
import java.util.Collections;
@ -29,12 +28,9 @@ import java.util.concurrent.atomic.AtomicReference;
import org.apache.commons.lang.StringUtils;
import org.apache.solr.SolrTestCaseJ4;
import org.apache.solr.util.RecordingJSONParser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class TestJsonRecordReader extends SolrTestCaseJ4 {
private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
public void testOneLevelSplit() throws IOException {
String json = "{\n" +
@ -281,7 +277,7 @@ public class TestJsonRecordReader extends SolrTestCaseJ4 {
JsonRecordReader streamer = JsonRecordReader.getInst("/|/a/b", Arrays.asList("/a/x", "/a/b/*"));
streamer.streamRecords(new StringReader(json), (record, path) -> {
assertEquals(record.get("x"), "y");
assertEquals(((Map) record.get(null)).get("c"), "d");
assertEquals(((Map) record.get("b")).get("c"), "d");
});
json = "{a:{" +
"b:[{c:c1, e:e1},{c:c2, e :e2, d:{p:q}}]," +
@ -289,7 +285,7 @@ public class TestJsonRecordReader extends SolrTestCaseJ4 {
"}}";
streamer.streamRecords(new StringReader(json), (record, path) -> {
assertEquals(record.get("x"), "y");
List l = (List) record.get(null);
List l = (List) record.get("b");
Map m = (Map) l.get(0);
assertEquals(m.get("c"), "c1");
assertEquals(m.get("e"), "e1");
@ -300,7 +296,7 @@ public class TestJsonRecordReader extends SolrTestCaseJ4 {
streamer = JsonRecordReader.getInst("/|/a/b", Arrays.asList("$FQN:/**"));
streamer.streamRecords(new StringReader(json), (record, path) -> {
assertEquals(record.get("a.x"), "y");
List l = (List) record.get(null);
List l = (List) record.get("b");
Map m = (Map) l.get(0);
assertEquals(m.get("c"), "c1");
assertEquals(m.get("e"), "e1");