diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt index fb5760ba383..0c913215904 100644 --- a/solr/CHANGES.txt +++ b/solr/CHANGES.txt @@ -241,6 +241,8 @@ Bug Fixes * SOLR-13727: Fixed V2Requests - HttpSolrClient replaced first instance of "/solr" with "/api" which caused a change in host names starting with "solr". (Megan Carey via yonik) +* SOLR-13180: Fix ClassCastException in Json Request API (Johannes Kloos, Jan Høydahl, Munendra S N) + Other Changes ---------------------- diff --git a/solr/core/src/java/org/apache/solr/request/json/ObjectUtil.java b/solr/core/src/java/org/apache/solr/request/json/ObjectUtil.java index fc677812388..b9c73bc4bc3 100644 --- a/solr/core/src/java/org/apache/solr/request/json/ObjectUtil.java +++ b/solr/core/src/java/org/apache/solr/request/json/ObjectUtil.java @@ -22,6 +22,8 @@ import java.util.LinkedHashMap; import java.util.List; import java.util.Map; +import org.apache.solr.common.SolrException; + public class ObjectUtil { public static class ConflictHandler { @@ -103,10 +105,14 @@ public class ObjectUtil { // OK, now we need to merge values handler.handleConflict(outer, path, key, val, existingVal); } - } else { + } else if (val instanceof Map) { // merging at top level... Map newMap = (Map)val; handler.mergeMap(outer, newMap, path); + } else { + // todo: find a way to return query param in error message + throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, + "Expected JSON Object but got " + val.getClass().getSimpleName() + "=" + val); } } 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 6370bef7dc4..e1ddfcfb549 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 @@ -270,6 +270,8 @@ public class RequestUtil { ObjectUtil.mergeObjects(json, path, o, handler); } } + } catch (JSONParser.ParseException e ) { + throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, e); } 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 bcc936d5378..9f34db06c90 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 @@ -19,12 +19,13 @@ package org.apache.solr.search.json; import org.apache.lucene.util.LuceneTestCase; import org.apache.solr.JSONTestUtil; import org.apache.solr.SolrTestCaseHS; - +import org.apache.solr.common.SolrException; import org.apache.solr.common.params.CommonParams; import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Test; + @LuceneTestCase.SuppressCodecs({"Lucene3x","Lucene40","Lucene41","Lucene42","Lucene45","Appending"}) public class TestJsonRequest extends SolrTestCaseHS { @@ -79,6 +80,15 @@ public class TestJsonRequest extends SolrTestCaseHS { , "response/numFound==2" ); + // invalid value + SolrException ex = expectThrows(SolrException.class, () -> client.testJQ(params("q", "*:*", "json", "5"))); + assertEquals(SolrException.ErrorCode.BAD_REQUEST.code, ex.code()); + + // this is to verify other json params are not affected + client.testJQ( params("q", "cat_s:A", "json.limit", "1"), + "response/numFound==2" + ); + // test multiple json params client.testJQ( params("json","{query:'cat_s:A'}", "json","{filter:'where_s:NY'}") , "response/numFound==1"