From 4799bc5a4a36d4b550f69f8e2a233b857b6b0340 Mon Sep 17 00:00:00 2001 From: David Smiley Date: Fri, 15 Jun 2018 00:46:17 -0400 Subject: [PATCH] SOLR-12362: JSON parsing: upgrade to List on end of array not start. And fix TestJsonRecordReader assumptions RE null keys --- .../solr/common/util/JsonRecordReader.java | 16 ++++++++++------ .../solr/common/util/TestJsonRecordReader.java | 10 +++------- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/solr/solrj/src/java/org/apache/solr/common/util/JsonRecordReader.java b/solr/solrj/src/java/org/apache/solr/common/util/JsonRecordReader.java index 7f93bc4691a..f66c88e4118 100644 --- a/solr/solrj/src/java/org/apache/solr/common/util/JsonRecordReader.java +++ b/solr/solrj/src/java/org/apache/solr/common/util/JsonRecordReader.java @@ -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); } } } diff --git a/solr/solrj/src/test/org/apache/solr/common/util/TestJsonRecordReader.java b/solr/solrj/src/test/org/apache/solr/common/util/TestJsonRecordReader.java index da75a43cf15..9b109707d53 100644 --- a/solr/solrj/src/test/org/apache/solr/common/util/TestJsonRecordReader.java +++ b/solr/solrj/src/test/org/apache/solr/common/util/TestJsonRecordReader.java @@ -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");