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

epugh 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 c44af90f3fe SOLR-17241: Honor wt on v2 collection responses from 
non-hosting nodes (#5266)
c44af90f3fe is described below

commit c44af90f3fe328fda3fbba254f9757f440627f0d
Author: Nick <[email protected]>
AuthorDate: Sat Nov 21 21:35:37 2026 -0500

    SOLR-17241: Honor wt on v2 collection responses from non-hosting nodes 
(#5266)
---
 .../unreleased/SOLR-17241-wt-non-hosting-node.yml  |   9 +
 .../solr/jersey/MediaTypeOverridingFilter.java     |   9 +-
 .../org/apache/solr/jersey/MessageBodyWriters.java |  11 +-
 .../apache/solr/handler/V2ApiIntegrationTest.java  |  48 ++++++
 .../solr/jersey/MediaTypeOverridingFilterTest.java | 186 +++++++++++++++++++++
 5 files changed, 260 insertions(+), 3 deletions(-)

diff --git a/changelog/unreleased/SOLR-17241-wt-non-hosting-node.yml 
b/changelog/unreleased/SOLR-17241-wt-non-hosting-node.yml
new file mode 100644
index 00000000000..4ec6b443909
--- /dev/null
+++ b/changelog/unreleased/SOLR-17241-wt-non-hosting-node.yml
@@ -0,0 +1,9 @@
+title: >
+  Honor wt on v2 collection responses from nodes without a local replica,
+  avoiding SolrJ response-format errors
+type: fixed
+authors:
+  - name: Nick Shanin
+links:
+  - name: SOLR-17241
+    url: https://issues.apache.org/jira/browse/SOLR-17241
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 70122f29425..5b4b0e761fe 100644
--- a/solr/core/src/java/org/apache/solr/jersey/MediaTypeOverridingFilter.java
+++ b/solr/core/src/java/org/apache/solr/jersey/MediaTypeOverridingFilter.java
@@ -21,6 +21,7 @@ import static jakarta.ws.rs.core.HttpHeaders.ACCEPT;
 import static jakarta.ws.rs.core.HttpHeaders.CONTENT_TYPE;
 import static jakarta.ws.rs.core.MediaType.APPLICATION_JSON;
 import static org.apache.solr.common.params.CommonParams.WT;
+import static org.apache.solr.jersey.RequestContextKeys.SOLR_PARAMS;
 import static org.apache.solr.jersey.RequestContextKeys.SOLR_QUERY_REQUEST;
 
 import jakarta.ws.rs.container.ContainerRequestContext;
@@ -31,6 +32,7 @@ import jakarta.ws.rs.core.Context;
 import java.io.IOException;
 import java.util.List;
 import org.apache.solr.api.JerseyResource;
+import org.apache.solr.common.params.SolrParams;
 import org.apache.solr.handler.admin.ZookeeperRead;
 import org.apache.solr.handler.api.V2ApiUtils;
 import org.apache.solr.request.SolrQueryRequest;
@@ -65,8 +67,11 @@ public class MediaTypeOverridingFilter implements 
ContainerResponseFilter {
 
     final SolrQueryRequest solrQueryRequest =
         (SolrQueryRequest) requestContext.getProperty(SOLR_QUERY_REQUEST);
-    // TODO Is it valid for SQRequest to be null?
-    final var params = (solrQueryRequest != null) ? 
solrQueryRequest.getParams() : null;
+    // Collection-scoped Jersey requests can be served without a local 
SolrQueryRequest.
+    final var params =
+        (solrQueryRequest != null)
+            ? solrQueryRequest.getParams()
+            : (SolrParams) requestContext.getProperty(SOLR_PARAMS);
     if (params != null && params.get(WT) != null) { // Override for 'wt'
       final String mediaType = V2ApiUtils.getMediaTypeFromWtParam(params, 
null);
       if (mediaType != null) {
diff --git a/solr/core/src/java/org/apache/solr/jersey/MessageBodyWriters.java 
b/solr/core/src/java/org/apache/solr/jersey/MessageBodyWriters.java
index cf4d5a434ab..fad5dbc33d9 100644
--- a/solr/core/src/java/org/apache/solr/jersey/MessageBodyWriters.java
+++ b/solr/core/src/java/org/apache/solr/jersey/MessageBodyWriters.java
@@ -18,6 +18,7 @@
 package org.apache.solr.jersey;
 
 import static 
org.apache.solr.client.solrj.response.JavaBinResponseParser.JAVABIN_CONTENT_TYPE_V2;
+import static org.apache.solr.jersey.RequestContextKeys.SOLR_PARAMS;
 import static org.apache.solr.jersey.RequestContextKeys.SOLR_QUERY_REQUEST;
 import static org.apache.solr.jersey.RequestContextKeys.SOLR_QUERY_RESPONSE;
 import static 
org.apache.solr.response.QueryResponseWriter.CONTENT_TYPE_TEXT_UTF8;
@@ -34,8 +35,10 @@ import java.io.IOException;
 import java.io.OutputStream;
 import java.lang.annotation.Annotation;
 import java.lang.reflect.Type;
+import org.apache.solr.common.params.SolrParams;
 import org.apache.solr.handler.api.V2ApiUtils;
 import org.apache.solr.request.SolrQueryRequest;
+import org.apache.solr.request.SolrQueryRequestBase;
 import org.apache.solr.response.CSVResponseWriter;
 import org.apache.solr.response.JavaBinResponseWriter;
 import org.apache.solr.response.PrometheusResponseWriter;
@@ -164,8 +167,14 @@ public class MessageBodyWriters {
         throws IOException, WebApplicationException {
       final ContainerRequestContext requestContext =
           resourceContext.getResource(ContainerRequestContext.class);
-      final SolrQueryRequest solrQueryRequest =
+      final SolrQueryRequest existingRequest =
           (SolrQueryRequest) requestContext.getProperty(SOLR_QUERY_REQUEST);
+      // Collection-scoped requests served by a non-hosting node have no 
SolrQueryRequest
+      final SolrQueryRequest solrQueryRequest =
+          existingRequest != null
+              ? existingRequest
+              : new SolrQueryRequestBase(
+                  null, (SolrParams) requestContext.getProperty(SOLR_PARAMS));
       final SolrQueryResponse solrQueryResponse =
           (SolrQueryResponse) requestContext.getProperty(SOLR_QUERY_RESPONSE);
 
diff --git 
a/solr/core/src/test/org/apache/solr/handler/V2ApiIntegrationTest.java 
b/solr/core/src/test/org/apache/solr/handler/V2ApiIntegrationTest.java
index 4bdeb7bcae2..370496c51e6 100644
--- a/solr/core/src/test/org/apache/solr/handler/V2ApiIntegrationTest.java
+++ b/solr/core/src/test/org/apache/solr/handler/V2ApiIntegrationTest.java
@@ -41,9 +41,12 @@ import 
org.apache.solr.client.solrj.response.XMLResponseParser;
 import org.apache.solr.client.solrj.response.json.JsonMapResponseParser;
 import org.apache.solr.cloud.SolrCloudTestCase;
 import org.apache.solr.common.SolrDocumentList;
+import org.apache.solr.common.cloud.Replica;
 import org.apache.solr.common.params.ModifiableSolrParams;
 import org.apache.solr.common.util.NamedList;
 import org.apache.solr.common.util.Utils;
+import org.apache.solr.core.SolrCore;
+import org.apache.solr.embedded.JettySolrRunner;
 import org.eclipse.jetty.client.HttpClient;
 import org.junit.BeforeClass;
 import org.junit.Test;
@@ -175,6 +178,51 @@ public class V2ApiIntegrationTest extends 
SolrCloudTestCase {
     assertEquals("application/json", 
response.getHeaders().get("Content-type"));
   }
 
+  @Test
+  public void testReloadHonorsWtOnNonHostingNode() throws Exception {
+    final String collection = "reload_one_replica";
+    CollectionAdminRequest.createCollection(collection, "conf1", 1, 1)
+        .process(cluster.getSolrClient());
+    try {
+      cluster.waitForActiveCollection(collection, 1, 1);
+      final Replica replica =
+          
getCollectionState(collection).getSlice("shard1").getReplicas().iterator().next();
+      final JettySolrRunner hostingNode =
+          cluster.getJettySolrRunners().stream()
+              .filter(jetty -> 
jetty.getNodeName().equals(replica.getNodeName()))
+              .findFirst()
+              .orElseThrow();
+      final JettySolrRunner nonHostingNode =
+          cluster.getJettySolrRunners().stream()
+              .filter(jetty -> 
!jetty.getNodeName().equals(replica.getNodeName()))
+              .findFirst()
+              .orElseThrow();
+
+      for (JettySolrRunner target : List.of(hostingNode, nonHostingNode)) {
+        try (SolrCore before = 
hostingNode.getCoreContainer().getCore(replica.getCoreName());
+            var client = new 
HttpJettySolrClient.Builder(target.getBaseUrl().toString()).build()) {
+          assertNotNull(before);
+          final V2Request request =
+              new V2Request.Builder("/collections/" + collection + "/reload")
+                  .withMethod(SolrRequest.METHOD.POST)
+                  .withParams(params("wt", "javabin"))
+                  .withPayload("{}")
+                  .build();
+          request.setResponseParser(new JavaBinResponseParser());
+
+          final V2Response response = request.process(client);
+          assertEquals("Reload through " + target.getNodeName(), 0, 
response.getStatus());
+          try (SolrCore after = 
hostingNode.getCoreContainer().getCore(replica.getCoreName())) {
+            assertNotNull(after);
+            assertNotSame("Reload must replace the hosted core", before, 
after);
+          }
+        }
+      }
+    } finally {
+      
CollectionAdminRequest.deleteCollection(collection).process(cluster.getSolrClient());
+    }
+  }
+
   private HttpClient getRawClient() {
     return cluster.getRandomJetty(random()).getSolrClient().getHttpClient();
   }
diff --git 
a/solr/core/src/test/org/apache/solr/jersey/MediaTypeOverridingFilterTest.java 
b/solr/core/src/test/org/apache/solr/jersey/MediaTypeOverridingFilterTest.java
new file mode 100644
index 00000000000..1c63aaa2b23
--- /dev/null
+++ 
b/solr/core/src/test/org/apache/solr/jersey/MediaTypeOverridingFilterTest.java
@@ -0,0 +1,186 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.solr.jersey;
+
+import static jakarta.ws.rs.core.HttpHeaders.ACCEPT;
+import static jakarta.ws.rs.core.HttpHeaders.CONTENT_TYPE;
+import static jakarta.ws.rs.core.MediaType.APPLICATION_JSON;
+import static jakarta.ws.rs.core.MediaType.APPLICATION_XML;
+import static org.apache.solr.SolrTestCaseJ4.assumeWorkingMockito;
+import static org.apache.solr.SolrTestCaseJ4.params;
+import static 
org.apache.solr.client.solrj.response.JavaBinResponseParser.JAVABIN_CONTENT_TYPE_V2;
+import static org.apache.solr.common.params.CommonParams.WT;
+import static org.apache.solr.jersey.RequestContextKeys.SOLR_PARAMS;
+import static org.apache.solr.jersey.RequestContextKeys.SOLR_QUERY_REQUEST;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import jakarta.ws.rs.container.ContainerRequestContext;
+import jakarta.ws.rs.container.ContainerResponseContext;
+import jakarta.ws.rs.container.ResourceInfo;
+import jakarta.ws.rs.core.MultivaluedHashMap;
+import jakarta.ws.rs.core.MultivaluedMap;
+import java.util.Map;
+import org.apache.solr.SolrTestCase;
+import org.apache.solr.handler.admin.ZookeeperRead;
+import org.apache.solr.handler.admin.api.ReloadCollectionAPI;
+import org.apache.solr.request.SolrQueryRequest;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.mockito.InjectMocks;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+
+/** Checks response negotiation when a Jersey resource has no local 
SolrQueryRequest. */
+public class MediaTypeOverridingFilterTest extends SolrTestCase {
+
+  @Mock private ResourceInfo resourceInfo;
+  @InjectMocks private MediaTypeOverridingFilter filter;
+
+  private AutoCloseable mocks;
+  private ContainerRequestContext request;
+  private ContainerResponseContext response;
+  private MultivaluedMap<String, String> requestHeaders;
+  private MultivaluedMap<String, Object> responseHeaders;
+
+  @BeforeClass
+  public static void ensureWorkingMockito() {
+    assumeWorkingMockito();
+  }
+
+  @Before
+  public void setUpFilter() {
+    mocks = MockitoAnnotations.openMocks(this);
+    request = mock(ContainerRequestContext.class);
+    response = mock(ContainerResponseContext.class);
+    requestHeaders = new MultivaluedHashMap<>();
+    responseHeaders = new MultivaluedHashMap<>();
+    responseHeaders.putSingle(CONTENT_TYPE, APPLICATION_XML);
+    doReturn(ReloadCollectionAPI.class).when(resourceInfo).getResourceClass();
+    when(request.getHeaders()).thenReturn(requestHeaders);
+    when(request.getHeaderString(ACCEPT)).thenAnswer(ignored -> 
requestHeaders.getFirst(ACCEPT));
+    when(response.getHeaders()).thenReturn(responseHeaders);
+    when(response.getStatus()).thenReturn(200);
+  }
+
+  @After
+  public void closeMocks() throws Exception {
+    mocks.close();
+  }
+
+  @Test
+  public void testWtFromQueryParamsWithoutSolrQueryRequest() throws Exception {
+    requestHeaders.putSingle(ACCEPT, "text/plain");
+    final Map<String, String> formats =
+        Map.of(
+            "json", APPLICATION_JSON, "xml", APPLICATION_XML, "javabin", 
JAVABIN_CONTENT_TYPE_V2);
+    for (var format : formats.entrySet()) {
+      responseHeaders.putSingle(CONTENT_TYPE, "text/plain");
+      when(request.getProperty(SOLR_PARAMS)).thenReturn(params(WT, 
format.getKey()));
+
+      filter.filter(request, response);
+
+      assertEquals(format.getKey(), format.getValue(), 
responseHeaders.getFirst(CONTENT_TYPE));
+    }
+  }
+
+  @Test
+  public void testSolrQueryRequestParamsTakePrecedence() throws Exception {
+    responseHeaders.putSingle(CONTENT_TYPE, "text/plain");
+    final SolrQueryRequest solrRequest = mock(SolrQueryRequest.class);
+    when(solrRequest.getParams()).thenReturn(params(WT, "xml"));
+    when(request.getProperty(SOLR_QUERY_REQUEST)).thenReturn(solrRequest);
+    when(request.getProperty(SOLR_PARAMS)).thenReturn(params(WT, "javabin"));
+
+    filter.filter(request, response);
+
+    assertEquals(APPLICATION_XML, responseHeaders.getFirst(CONTENT_TYPE));
+  }
+
+  @Test
+  public void testAcceptHeaderUsedWithoutWt() throws Exception {
+    when(request.getProperty(SOLR_PARAMS)).thenReturn(params());
+    requestHeaders.putSingle(ACCEPT, APPLICATION_XML);
+
+    filter.filter(request, response);
+
+    assertEquals(APPLICATION_XML, responseHeaders.getFirst(CONTENT_TYPE));
+  }
+
+  @Test
+  public void testDefaultsToJsonWithoutWt() throws Exception {
+    when(request.getProperty(SOLR_PARAMS)).thenReturn(params());
+    for (String accept : new String[] {null, "*/*"}) {
+      requestHeaders.clear();
+      if (accept != null) {
+        requestHeaders.putSingle(ACCEPT, accept);
+      }
+      responseHeaders.putSingle(CONTENT_TYPE, APPLICATION_XML);
+
+      filter.filter(request, response);
+
+      assertEquals(APPLICATION_JSON, responseHeaders.getFirst(CONTENT_TYPE));
+    }
+  }
+
+  @Test
+  public void testMissingParamsStillDefaultsToJson() throws Exception {
+    filter.filter(request, response);
+
+    assertEquals(APPLICATION_JSON, responseHeaders.getFirst(CONTENT_TYPE));
+  }
+
+  @Test
+  public void testErrorsPreserveMediaType() throws Exception {
+    when(request.getProperty(SOLR_PARAMS)).thenReturn(params(WT, "javabin"));
+    for (int status : new int[] {400, 500}) {
+      when(response.getStatus()).thenReturn(status);
+
+      filter.filter(request, response);
+
+      assertEquals(APPLICATION_XML, responseHeaders.getFirst(CONTENT_TYPE));
+    }
+  }
+
+  @Test
+  public void testExemptResourcesPreserveMediaType() throws Exception {
+    when(request.getProperty(SOLR_PARAMS)).thenReturn(params(WT, "javabin"));
+    for (Class<?> resourceClass : new Class<?>[] {null, ZookeeperRead.class}) {
+      doReturn(resourceClass).when(resourceInfo).getResourceClass();
+
+      filter.filter(request, response);
+
+      assertEquals(APPLICATION_XML, responseHeaders.getFirst(CONTENT_TYPE));
+    }
+  }
+
+  @Test
+  public void testBlankAndUnknownWtDoNotOverrideNegotiatedType() throws 
Exception {
+    requestHeaders.putSingle(ACCEPT, APPLICATION_XML);
+    for (String wt : new String[] {"", "unknown"}) {
+      when(request.getProperty(SOLR_PARAMS)).thenReturn(params(WT, wt));
+
+      filter.filter(request, response);
+
+      assertEquals(APPLICATION_XML, responseHeaders.getFirst(CONTENT_TYPE));
+    }
+  }
+}

Reply via email to