From 78bc41b54e76dd36476339ba24f98030e9d65a17 Mon Sep 17 00:00:00 2001 From: Yonik Seeley Date: Fri, 5 Dec 2008 18:04:11 +0000 Subject: [PATCH] SOLR-898: Fix null pointer exception for the JSON response writer based formats when nl.json=arrarr with null keys. git-svn-id: https://svn.apache.org/repos/asf/lucene/solr/trunk@723804 13f79535-47bb-0310-9956-ffa450edef68 --- CHANGES.txt | 3 +++ .../apache/solr/request/JSONResponseWriter.java | 8 +++++--- .../org/apache/solr/request/JSONWriterTest.java | 16 ++++++++++++++++ 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/CHANGES.txt b/CHANGES.txt index 6ce65524e10..c6f380fc84b 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -149,6 +149,9 @@ Bug Fixes 14. SOLR-892: Fix serialization of booleans for PHPSerializedResponseWriter (yonik) +15. SOLR-898: Fix null pointer exception for the JSON response writer + based formats when nl.json=arrarr with null keys. (yonik) + Other Changes ---------------------- diff --git a/src/java/org/apache/solr/request/JSONResponseWriter.java b/src/java/org/apache/solr/request/JSONResponseWriter.java index 5f7786013fe..b0bd5671774 100644 --- a/src/java/org/apache/solr/request/JSONResponseWriter.java +++ b/src/java/org/apache/solr/request/JSONResponseWriter.java @@ -245,13 +245,15 @@ class JSONWriter extends TextResponseWriter { writeArrayOpener(1); incLevel(); - writeStr(null,key,true); + if (key==null) { + writeNull(null); + } else { + writeStr(null, key, true); + } writeArraySeparator(); writeVal(key,val.getVal(i)); decLevel(); writeArrayCloser(); - - } decLevel(); diff --git a/src/test/org/apache/solr/request/JSONWriterTest.java b/src/test/org/apache/solr/request/JSONWriterTest.java index d1f171cd704..ee222c77bc7 100644 --- a/src/test/org/apache/solr/request/JSONWriterTest.java +++ b/src/test/org/apache/solr/request/JSONWriterTest.java @@ -63,5 +63,21 @@ public class JSONWriterTest extends AbstractSolrTestCase { w.write(buf, req, rsp); assertEquals(buf.toString(), "a:3:{s:5:\"data1\";s:5:\"hello\";s:5:\"data2\";i:42;s:5:\"data3\";b:1;}"); } + + public void testJSON() throws IOException { + SolrQueryRequest req = req("wt","json","json.nl","arrarr"); + SolrQueryResponse rsp = new SolrQueryResponse(); + JSONResponseWriter w = new JSONResponseWriter(); + + StringWriter buf = new StringWriter(); + NamedList nl = new NamedList(); + nl.add("data1", "hello"); + nl.add(null, 42); + rsp.add("nl", nl); + + w.write(buf, req, rsp); + assertEquals(buf.toString(), "{\"nl\":[[\"data1\",\"hello\"],[null,42]]}"); + + } }