From 645eb357405e4fed431c6e5043ba77db280e6797 Mon Sep 17 00:00:00 2001 From: Yonik Seeley Date: Tue, 9 Jun 2015 15:48:16 +0000 Subject: [PATCH] SOLR-7574: fix NPE due to missing body with json content type git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1684458 13f79535-47bb-0310-9956-ffa450edef68 --- solr/CHANGES.txt | 3 +++ .../org/apache/solr/request/json/RequestUtil.java | 9 +++++++-- .../apache/solr/search/json/TestJsonRequest.java | 15 +++++++++++++++ 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt index f6defc5f664..cffb21ab668 100644 --- a/solr/CHANGES.txt +++ b/solr/CHANGES.txt @@ -174,6 +174,9 @@ Bug Fixes * SOLR-7518: New Facet Module should respect shards.tolerant and process all non-failing shards instead of throwing an exception. (yonik) +* SOLR-7574: A request with a json content type but no body caused a null pointer exception (yonik) + + Other Changes ---------------------- diff --git a/solr/core/src/java/org/apache/solr/request/json/RequestUtil.java b/solr/core/src/java/org/apache/solr/request/json/RequestUtil.java index 0b08d16e0a5..310fce47228 100644 --- a/solr/core/src/java/org/apache/solr/request/json/RequestUtil.java +++ b/solr/core/src/java/org/apache/solr/request/json/RequestUtil.java @@ -76,7 +76,9 @@ public class RequestUtil { try { String jsonString = IOUtils.toString( cs.getReader() ); - MultiMapSolrParams.addParam(JSON, jsonString, map); + if (jsonString != null) { + MultiMapSolrParams.addParam(JSON, jsonString, map); + } } catch (IOException e) { throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Exception reading content stream for request:"+req, e); } @@ -257,7 +259,10 @@ public class RequestUtil { path = path.subList(1, path.size()); for (String jsonStr : vals) { Object o = ObjectBuilder.fromJSON(jsonStr); - ObjectUtil.mergeObjects(json, path, o, handler); + // zero-length strings or comments can cause this to be null (and a zero-length string can result from a json content-type w/o a body) + if (o != null) { + ObjectUtil.mergeObjects(json, path, o, handler); + } } } catch (IOException e) { // impossible diff --git a/solr/core/src/test/org/apache/solr/search/json/TestJsonRequest.java b/solr/core/src/test/org/apache/solr/search/json/TestJsonRequest.java index 430b5083ae1..a9e78315796 100644 --- a/solr/core/src/test/org/apache/solr/search/json/TestJsonRequest.java +++ b/solr/core/src/test/org/apache/solr/search/json/TestJsonRequest.java @@ -89,6 +89,16 @@ public class TestJsonRequest extends SolrTestCaseHS { , "response/numFound==1" ); + // test multiple json params with one being zero length + client.testJQ( params("json","{query:'cat_s:A'}", "json","{filter:'where_s:NY'}", "json","") + , "response/numFound==1" + ); + + // test multiple json params with one being a comment + client.testJQ( params("json","{query:'cat_s:A'}", "json","{filter:'where_s:NY'}", "json","/* */") + , "response/numFound==1" + ); + // test merging multi-valued params into list client.testJQ( params("json","{query:'*:*'}", "json","{filter:'where_s:NY'}", "json","{filter:'cat_s:A'}") , "response/numFound==1" @@ -114,6 +124,11 @@ public class TestJsonRequest extends SolrTestCaseHS { , "response/numFound==1" ); + // test inserting and merging with paths with an empty string and a comment + client.testJQ( params("json.query","'*:*'", "json.filter","'where_s:NY'", "json.filter","'cat_s:A'", "json.filter","", "json.filter","/* */") + , "response/numFound==1" + ); + // test overwriting of non-multivalued params client.testJQ( params("json.query","'foo_s:NONE'", "json.filter","'where_s:NY'", "json.filter","'cat_s:A'", "json.query","'*:*'") , "response/numFound==1"