You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by an...@apache.org on 2016/03/15 20:16:23 UTC

lucene-solr:branch_6_0: SOLR-8836: Return 400, and a SolrException when an invalid json is provided to the update handler instead of 500.

Repository: lucene-solr
Updated Branches:
  refs/heads/branch_6_0 0bafb1710 -> cd070d7b1


SOLR-8836: Return 400, and a SolrException when an invalid json is provided to the update handler instead of 500.


Project: http://git-wip-us.apache.org/repos/asf/lucene-solr/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucene-solr/commit/cd070d7b
Tree: http://git-wip-us.apache.org/repos/asf/lucene-solr/tree/cd070d7b
Diff: http://git-wip-us.apache.org/repos/asf/lucene-solr/diff/cd070d7b

Branch: refs/heads/branch_6_0
Commit: cd070d7b17955fc3cc10f013e9694946c5ce1c88
Parents: 0bafb17
Author: anshum <an...@apache.org>
Authored: Tue Mar 15 10:55:03 2016 -0700
Committer: Anshum Gupta <an...@apache.org>
Committed: Tue Mar 15 11:53:29 2016 -0700

----------------------------------------------------------------------
 solr/CHANGES.txt                                |  3 +++
 .../apache/solr/handler/loader/JsonLoader.java  |  6 +++++-
 .../org/apache/solr/handler/JsonLoaderTest.java | 20 ++++++++++++++++++++
 3 files changed, 28 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/cd070d7b/solr/CHANGES.txt
----------------------------------------------------------------------
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt
index 26955bc..37e2819 100644
--- a/solr/CHANGES.txt
+++ b/solr/CHANGES.txt
@@ -403,6 +403,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 Fixes

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/cd070d7b/solr/core/src/java/org/apache/solr/handler/loader/JsonLoader.java
----------------------------------------------------------------------
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 e99b243..ba800ff 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);
       }
     }

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/cd070d7b/solr/core/src/test/org/apache/solr/handler/JsonLoaderTest.java
----------------------------------------------------------------------
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 f2316da..e27ca1a 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('\'', '"');