Skip to content

Apache Lucene open-source search software

SOLR-7574: fix NPE due to missing body with json content type

Yonik Seeley645eb357405e
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"