You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@solr.apache.org by ge...@apache.org on 2024/06/06 17:25:45 UTC

(solr) branch main updated: SOLR-17317: Correct 'wt' interpretation for v2 APIs (#2496)

This is an automated email from the ASF dual-hosted git repository.

gerlowskija pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/solr.git


The following commit(s) were added to refs/heads/main by this push:
     new af2a3c742b5 SOLR-17317: Correct 'wt' interpretation for v2 APIs (#2496)
af2a3c742b5 is described below

commit af2a3c742b50004c69fe2be48c51f904ebdff012
Author: Jason Gerlowski <ge...@apache.org>
AuthorDate: Thu Jun 6 13:25:39 2024 -0400

    SOLR-17317: Correct 'wt' interpretation for v2 APIs (#2496)
    
    Prior to this commit a bug was causing 'wt=json' to return XML content.
    This is now fixed - the problem was a missing clause in a switch-case
    statement.
---
 solr/CHANGES.txt                                   |  2 ++
 .../org/apache/solr/handler/api/V2ApiUtils.java    | 12 ++++++-----
 .../solr/jersey/CatchAllExceptionMapper.java       |  3 ++-
 .../solr/jersey/MediaTypeOverridingFilter.java     |  5 ++++-
 .../apache/solr/handler/api/V2ApiUtilsTest.java    | 25 ++++++++++++++++++++++
 5 files changed, 40 insertions(+), 7 deletions(-)

diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt
index 558b5b21d5f..9dd14ff6b99 100644
--- a/solr/CHANGES.txt
+++ b/solr/CHANGES.txt
@@ -148,6 +148,8 @@ Bug Fixes
 
 * SOLR-17118: Simplify CoreContainerProvider initialization and JettySolrRunner. Avoid deadlock/hang. (David Smiley)
 
+* SOLR-17317: v2 APIs no longer return the incorrect content-type when 'wt=json' specified (Jason Gerlowski)
+
 Dependency Upgrades
 ---------------------
 (No changes)
diff --git a/solr/core/src/java/org/apache/solr/handler/api/V2ApiUtils.java b/solr/core/src/java/org/apache/solr/handler/api/V2ApiUtils.java
index 2fa0ae4ed3f..c317dc8fac4 100644
--- a/solr/core/src/java/org/apache/solr/handler/api/V2ApiUtils.java
+++ b/solr/core/src/java/org/apache/solr/handler/api/V2ApiUtils.java
@@ -26,9 +26,10 @@ import java.util.List;
 import java.util.Map;
 import org.apache.solr.client.api.model.SolrJerseyResponse;
 import org.apache.solr.common.MapWriter.EntryWriter;
+import org.apache.solr.common.params.SolrParams;
 import org.apache.solr.common.util.NamedList;
+import org.apache.solr.common.util.StrUtils;
 import org.apache.solr.common.util.Utils;
-import org.apache.solr.request.SolrQueryRequest;
 import org.apache.solr.response.SolrQueryResponse;
 
 /** Utilities helpful for common V2 API declaration tasks. */
@@ -87,13 +88,14 @@ public class V2ApiUtils {
     squashObjectIntoNamedList(destination, toSquash, true);
   }
 
-  public static String getMediaTypeFromWtParam(
-      SolrQueryRequest solrQueryRequest, String defaultMediaType) {
-    final String wtParam = solrQueryRequest.getParams().get(WT);
-    if (wtParam == null) return "application/json";
+  public static String getMediaTypeFromWtParam(SolrParams params, String defaultMediaType) {
+    final String wtParam = params.get(WT);
+    if (StrUtils.isBlank(wtParam)) return defaultMediaType;
 
     // The only currently-supported response-formats for JAX-RS v2 endpoints.
     switch (wtParam) {
+      case "json":
+        return "application/json";
       case "xml":
         return "application/xml";
       case "javabin":
diff --git a/solr/core/src/java/org/apache/solr/jersey/CatchAllExceptionMapper.java b/solr/core/src/java/org/apache/solr/jersey/CatchAllExceptionMapper.java
index 1b5e0be51a3..3db1c921398 100644
--- a/solr/core/src/java/org/apache/solr/jersey/CatchAllExceptionMapper.java
+++ b/solr/core/src/java/org/apache/solr/jersey/CatchAllExceptionMapper.java
@@ -116,7 +116,8 @@ public class CatchAllExceptionMapper implements ExceptionMapper<Exception> {
                 && solrQueryRequest.getCore().getCoreContainer().hideStackTrace());
     response.responseHeader.status = response.error.code;
     final String mediaType =
-        V2ApiUtils.getMediaTypeFromWtParam(solrQueryRequest, MediaType.APPLICATION_JSON);
+        V2ApiUtils.getMediaTypeFromWtParam(
+            solrQueryRequest.getParams(), MediaType.APPLICATION_JSON);
     return Response.status(response.error.code).type(mediaType).entity(response).build();
   }
 
diff --git a/solr/core/src/java/org/apache/solr/jersey/MediaTypeOverridingFilter.java b/solr/core/src/java/org/apache/solr/jersey/MediaTypeOverridingFilter.java
index 64e15645635..29695918224 100644
--- a/solr/core/src/java/org/apache/solr/jersey/MediaTypeOverridingFilter.java
+++ b/solr/core/src/java/org/apache/solr/jersey/MediaTypeOverridingFilter.java
@@ -25,6 +25,7 @@ import jakarta.ws.rs.container.ContainerResponseContext;
 import jakarta.ws.rs.container.ContainerResponseFilter;
 import jakarta.ws.rs.container.ResourceInfo;
 import jakarta.ws.rs.core.Context;
+import jakarta.ws.rs.core.MediaType;
 import java.io.IOException;
 import java.util.List;
 import org.apache.solr.api.JerseyResource;
@@ -62,7 +63,9 @@ public class MediaTypeOverridingFilter implements ContainerResponseFilter {
 
     final SolrQueryRequest solrQueryRequest =
         (SolrQueryRequest) requestContext.getProperty(SOLR_QUERY_REQUEST);
-    final String mediaType = V2ApiUtils.getMediaTypeFromWtParam(solrQueryRequest, null);
+    final String mediaType =
+        V2ApiUtils.getMediaTypeFromWtParam(
+            solrQueryRequest.getParams(), MediaType.APPLICATION_JSON);
     if (mediaType != null) {
       responseContext.getHeaders().putSingle(CONTENT_TYPE, mediaType);
     }
diff --git a/solr/core/src/test/org/apache/solr/handler/api/V2ApiUtilsTest.java b/solr/core/src/test/org/apache/solr/handler/api/V2ApiUtilsTest.java
index 5089a36dd70..38cbf00a2fa 100644
--- a/solr/core/src/test/org/apache/solr/handler/api/V2ApiUtilsTest.java
+++ b/solr/core/src/test/org/apache/solr/handler/api/V2ApiUtilsTest.java
@@ -16,7 +16,11 @@
  */
 package org.apache.solr.handler.api;
 
+import static org.apache.solr.client.solrj.impl.BinaryResponseParser.BINARY_CONTENT_TYPE_V2;
+
+import jakarta.ws.rs.core.MediaType;
 import org.apache.solr.SolrTestCaseJ4;
+import org.apache.solr.common.params.ModifiableSolrParams;
 import org.junit.Test;
 
 public class V2ApiUtilsTest extends SolrTestCaseJ4 {
@@ -36,4 +40,25 @@ public class V2ApiUtilsTest extends SolrTestCaseJ4 {
     assertFalse(
         "v2 API should be disabled if sysprop explicitly disables it", V2ApiUtils.isEnabled());
   }
+
+  @Test
+  public void testConvertsWtToMediaTypeString() {
+    assertEquals(
+        "someDefault",
+        V2ApiUtils.getMediaTypeFromWtParam(new ModifiableSolrParams(), "someDefault"));
+
+    var params = new ModifiableSolrParams();
+    params.add("wt", "json");
+    assertEquals(MediaType.APPLICATION_JSON, V2ApiUtils.getMediaTypeFromWtParam(params, null));
+
+    params.set("wt", "xml");
+    assertEquals(MediaType.APPLICATION_XML, V2ApiUtils.getMediaTypeFromWtParam(params, null));
+
+    params.set("wt", "javabin");
+    assertEquals(BINARY_CONTENT_TYPE_V2, V2ApiUtils.getMediaTypeFromWtParam(params, null));
+
+    // Defaults to, well the default, whenever an unknown/unexpected/typo'd 'wt' is provided
+    params.set("wt", "josn");
+    assertEquals("someDefault", V2ApiUtils.getMediaTypeFromWtParam(params, "someDefault"));
+  }
 }