From 5c739212fd62f7734a58bbce9f73666dc1418df4 Mon Sep 17 00:00:00 2001 From: Alex Deparvu Date: Tue, 3 Oct 2023 15:04:50 -0700 Subject: [PATCH] SOLR-17009 json.wrf parameter ignored in JacksonJsonWriter --- solr/CHANGES.txt | 2 ++ .../solr/response/JacksonJsonWriter.java | 6 ++++ .../apache/solr/response/JSONWriterTest.java | 33 +++++++++++++++++++ 3 files changed, 41 insertions(+) diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt index ef7e37ea61e..9a02f787bbe 100644 --- a/solr/CHANGES.txt +++ b/solr/CHANGES.txt @@ -256,6 +256,8 @@ Bug Fixes * SOLR-16644: Fixing the entropy warning threshold using scaling based on poolsize (Raghavan Muthuregunathan) +* SOLR-17009: json.wrf parameter ignored in JacksonJsonWriter (Alex Deparvu) + Dependency Upgrades --------------------- diff --git a/solr/core/src/java/org/apache/solr/response/JacksonJsonWriter.java b/solr/core/src/java/org/apache/solr/response/JacksonJsonWriter.java index 4de5039ba1e..79b09ae3bf4 100644 --- a/solr/core/src/java/org/apache/solr/response/JacksonJsonWriter.java +++ b/solr/core/src/java/org/apache/solr/response/JacksonJsonWriter.java @@ -79,7 +79,13 @@ public WriterImpl( @Override public void writeResponse() throws IOException { + if (wrapperFunction != null) { + writeStr(null, wrapperFunction + "(", false); + } super.writeNamedList(null, rsp.getValues()); + if (wrapperFunction != null) { + writeStr(null, ")", false); + } gen.close(); } diff --git a/solr/core/src/test/org/apache/solr/response/JSONWriterTest.java b/solr/core/src/test/org/apache/solr/response/JSONWriterTest.java index 9fcdcea4e5f..771def51eed 100644 --- a/solr/core/src/test/org/apache/solr/response/JSONWriterTest.java +++ b/solr/core/src/test/org/apache/solr/response/JSONWriterTest.java @@ -16,6 +16,7 @@ */ package org.apache.solr.response; +import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.StringWriter; import java.lang.reflect.Method; @@ -25,6 +26,7 @@ import java.util.Arrays; import java.util.List; import java.util.Objects; + import org.apache.solr.JSONTestUtil; import org.apache.solr.SolrTestCaseJ4; import org.apache.solr.common.SolrDocument; @@ -314,4 +316,35 @@ public void testConstantsUnchanged() { assertEquals("arrntv", JSONWriter.JSON_NL_ARROFNTV); assertEquals("json.wrf", JSONWriter.JSON_WRAPPER_FUNCTION); } + + @Test + public void testWfrJacksonJsonWriter() throws IOException { + SolrQueryRequest req = req("wt", "json", JSONWriter.JSON_WRAPPER_FUNCTION, "testFun"); + SolrQueryResponse rsp = new SolrQueryResponse(); + rsp.add("param0", "v0"); + rsp.add("param1", 42); + + JacksonJsonWriter w = new JacksonJsonWriter(); + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + w.write(baos, req, rsp); + String received = new String(baos.toByteArray()); + String expected = "testFun( {\n \"param0\":\"v0\",\n \"param1\":42\n} )"; + jsonEq(expected, received); + req.close(); + } + + @Test + public void testWfrJSONWriter() throws IOException { + SolrQueryRequest req = req("wt", "json", JSONWriter.JSON_WRAPPER_FUNCTION, "testFun"); + SolrQueryResponse rsp = new SolrQueryResponse(); + rsp.add("param0", "v0"); + rsp.add("param1", 42); + + JSONResponseWriter w = new JSONResponseWriter(); + StringWriter buf = new StringWriter(); + w.write(buf, req, rsp); + String expected = "testFun({\n \"param0\":\"v0\",\n \"param1\":42})"; + jsonEq(expected, buf.toString()); + req.close(); + } }