Apache Lucene open-source search software
SOLR-8836: Return 400, and a SolrException when an invalid json is provided to the update handler instead of 500.
anshum30a77b73b603
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txtindex 20140209097..691e87f5aed 100644--- a/solr/CHANGES.txt+++ b/solr/CHANGES.txt@@ -425,6 +425,9 @@ Other Changes* SOLR-8799: Improve error message when tuple can't be read by SolrJ JDBC (Kevin Risden, Joel Bernstein)+* SOLR-8836: Return 400, and a SolrException when an invalid json is provided to the update handler+ instead of 500. (Jason Gerlowski via Anshum Gupta)+================== 5.5.1 ==================Bug Fixesdiff --git a/solr/core/src/java/org/apache/solr/handler/loader/JsonLoader.java b/solr/core/src/java/org/apache/solr/handler/loader/JsonLoader.javaindex e99b2435958..ba800ff3faf 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@@ -50,6 +50,7 @@ import org.apache.solr.update.RollbackUpdateCommand;import org.apache.solr.update.processor.UpdateRequestProcessor;import org.apache.solr.util.RecordingJSONParser;import org.noggit.JSONParser;+import org.noggit.JSONParser.ParseException;import org.noggit.ObjectBuilder;import org.slf4j.Logger;import org.slf4j.LoggerFactory;@@ -111,7 +112,10 @@ public class JsonLoader extends ContentStreamLoader {}this.processUpdate(reader);- } finally {+ } catch (ParseException e) {+ throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Cannot parse provided JSON: " + e.getMessage());+ }+ finally {IOUtils.closeQuietly(reader);}}diff --git a/solr/core/src/test/org/apache/solr/handler/JsonLoaderTest.java b/solr/core/src/test/org/apache/solr/handler/JsonLoaderTest.javaindex f2316da9f1d..e27ca1a0639 100644--- a/solr/core/src/test/org/apache/solr/handler/JsonLoaderTest.java+++ b/solr/core/src/test/org/apache/solr/handler/JsonLoaderTest.java@@ -181,6 +181,26 @@ public class JsonLoaderTest extends SolrTestCaseJ4 {req.close();}+ @Test+ public void testInvalidJsonProducesBadRequestSolrException() throws Exception+ {+ SolrQueryResponse rsp = new SolrQueryResponse();+ BufferingRequestProcessor p = new BufferingRequestProcessor(null);+ JsonLoader loader = new JsonLoader();+ String invalidJsonString = "}{";++ try(SolrQueryRequest req = req()) {+ try {+ loader.load(req, rsp, new ContentStreamBase.StringStream(invalidJsonString), p);+ fail("Expected invalid JSON to produce a SolrException.");+ } catch (SolrException expectedException) {+ assertEquals(SolrException.ErrorCode.BAD_REQUEST.code, expectedException.code());+ assertTrue(expectedException.getMessage().contains("Cannot parse"));+ assertTrue(expectedException.getMessage().contains("JSON"));+ }+ }+ }+public void testSimpleFormatInAdd() throws Exception{String str = "{'add':[{'id':'1'},{'id':'2'}]}".replace('\'', '"');