From a07c40a217361577afc484091fe2c292dbf4f688 Mon Sep 17 00:00:00 2001 From: Noble Paul Date: Thu, 9 Jun 2016 11:34:06 +0530 Subject: [PATCH] SOLR-7123: echo shows wrong output, and added a test for multi level nesting --- .../solr/handler/loader/JsonLoader.java | 12 +++--- .../apache/solr/handler/JsonLoaderTest.java | 43 +++++++++++++++++++ 2 files changed, 50 insertions(+), 5 deletions(-) diff --git a/solr/core/src/java/org/apache/solr/handler/loader/JsonLoader.java b/solr/core/src/java/org/apache/solr/handler/loader/JsonLoader.java index 2c5b6ac1dec..318f8e6ffd1 100644 --- a/solr/core/src/java/org/apache/solr/handler/loader/JsonLoader.java +++ b/solr/core/src/java/org/apache/solr/handler/loader/JsonLoader.java @@ -221,10 +221,7 @@ public class JsonLoader extends ContentStreamLoader { docs = new ArrayList(); rsp.add("docs", docs); } - if (copy.containsKey(null)) { - copy.put(CHILD_DOC_KEY, copy.get(null)); - copy.remove(null); - } + changeChildDoc(copy); docs.add(copy); } else { AddUpdateCommand cmd = new AddUpdateCommand(req); @@ -251,7 +248,7 @@ public class JsonLoader extends ContentStreamLoader { if (o instanceof Map) result.addChildDocument(buildDoc((Map) o)); } } else if (e.getValue() instanceof Map) { - result.addChildDocument(buildDoc((Map) e)); + result.addChildDocument(buildDoc((Map) e.getValue())); } } else { result.setField(e.getKey(), e.getValue()); @@ -659,4 +656,9 @@ public class JsonLoader extends ContentStreamLoader { } } + private static Map changeChildDoc(Map m) { + if (m.containsKey(null)) m.put(CHILD_DOC_KEY, changeChildDoc((Map) m.remove(null))); + return m; + } + } diff --git a/solr/core/src/test/org/apache/solr/handler/JsonLoaderTest.java b/solr/core/src/test/org/apache/solr/handler/JsonLoaderTest.java index 7e4d28afd65..65f2d3e2bf0 100644 --- a/solr/core/src/test/org/apache/solr/handler/JsonLoaderTest.java +++ b/solr/core/src/test/org/apache/solr/handler/JsonLoaderTest.java @@ -388,6 +388,49 @@ public class JsonLoaderTest extends SolrTestCaseJ4 { assertEquals(d.getFieldValue("c"), "c2"); assertEquals(d.getFieldValue("e"), "e2"); assertEquals(d.getFieldValue("d.p"), "q"); + + json = "{\n" + + " \"id\": \"1\",\n" + + " \"name\": \"i am the parent\",\n" + + " \"cat\": \"parent\",\n" + + " \"children\": [\n" + + " {\n" + + " \"id\": \"1.1\",\n" + + " \"name\": \"i am the 1st child\",\n" + + " \"cat\": \"child\"\n" + + " },\n" + + " {\n" + + " \"id\": \"1.2\",\n" + + " \"name\": \"i am the 2nd child\",\n" + + " \"cat\": \"child\",\n" + + " \"grandchildren\": [\n" + + " {\n" + + " \"id\": \"1.2.1\",\n" + + " \"name\": \"i am the grandchild\",\n" + + " \"cat\": \"grandchild\"\n" + + " }\n" + + " ]\n" + + " }\n" + + " ]\n" + + "}"; + req = req( + "split", "/|/children|/children/grandchildren", + "f","$FQN:/**", + "f", "id:/children/id", + "f", "/name", + "f", "/children/name", + "f", "cat:/children/cat", + "f", "id:/children/grandchildren/id", + "f", "name:/children/grandchildren/name", + "f", "cat:/children/grandchildren/cat"); + req.getContext().put("path", "/update/json/docs"); + rsp = new SolrQueryResponse(); + p = new BufferingRequestProcessor(null); + loader = new JsonLoader(); + loader.load(req, rsp, new ContentStreamBase.StringStream(json), p); + assertEquals(2, p.addCommands.get(0).solrDoc.getChildDocuments().size()); + assertEquals(1, p.addCommands.get(0).solrDoc.getChildDocuments().get(1).getChildDocuments().size()); + }