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

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


The following commit(s) were added to refs/heads/branch_9x by this push:
     new 1a36bd50bd8 SOLR-17326: Fix references in generated SolrRequest impls 
(#2510)
1a36bd50bd8 is described below

commit 1a36bd50bd8515de925b7a044bf12a14bf8f9262
Author: Christos Malliaridis <[email protected]>
AuthorDate: Tue Jun 11 18:15:01 2024 +0200

    SOLR-17326: Fix references in generated SolrRequest impls (#2510)
    
    A handful of the v2 SolrRequest implementations generated
    by our OAS spec relied on response model classes whose names
    conflicted with other (unrelated) classes in solrj.  This caused
    errors at request time as JacksonParsingResponse would try to
    deserialize the JSON, XML, etc. response body into these
    unintended classes.
    
    This commit fixes this by modifying the 'api.mustache' template
    so that generated SolrRequest classes now reference their
    response model using the fully-qualified classname (i.e. including
    the package).  This resolves the ambiguity.
    
    ---------
    
    Co-authored-by: Jason Gerlowski <[email protected]>
---
 solr/CHANGES.txt                                   |  2 +
 .../solrj/src/resources/java-template/api.mustache |  4 +-
 .../client/solrj/ApiMustacheTemplateTests.java     | 81 ++++++++++++++++++++++
 3 files changed, 85 insertions(+), 2 deletions(-)

diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt
index 8ce3568fb36..10537782b70 100644
--- a/solr/CHANGES.txt
+++ b/solr/CHANGES.txt
@@ -60,6 +60,8 @@ Bug Fixes
 
 * SOLR-17315: Fixed "next step" example urls generated by bin/solr create 
commands to use the newly created  collection/core name. (Eric Pugh)
 
+* SOLR-17326: Generated v2 'SolrRequest' implementations now all serialize to 
the correct response type (Christos Malliaridis via Jason Gerlowski)
+
 Dependency Upgrades
 ---------------------
 (No changes)
diff --git a/solr/solrj/src/resources/java-template/api.mustache 
b/solr/solrj/src/resources/java-template/api.mustache
index 7fe5bdf6324..a4171667b0a 100644
--- a/solr/solrj/src/resources/java-template/api.mustache
+++ b/solr/solrj/src/resources/java-template/api.mustache
@@ -73,9 +73,9 @@ import {{modelPackage}}.{{dataType}};
 public class {{classname}} {
 
     {{#operation}}
-        public static class {{operationIdCamelCase}}Response extends 
JacksonParsingResponse<{{returnType}}> {
+        public static class {{operationIdCamelCase}}Response extends 
JacksonParsingResponse<{{modelPackage}}.{{returnType}}> {
           public {{operationIdCamelCase}}Response() {
-            super({{returnType}}.class);
+            super({{modelPackage}}.{{returnType}}.class);
           }
         }
 
diff --git 
a/solr/solrj/src/test/org/apache/solr/client/solrj/ApiMustacheTemplateTests.java
 
b/solr/solrj/src/test/org/apache/solr/client/solrj/ApiMustacheTemplateTests.java
new file mode 100644
index 00000000000..48111acf7d2
--- /dev/null
+++ 
b/solr/solrj/src/test/org/apache/solr/client/solrj/ApiMustacheTemplateTests.java
@@ -0,0 +1,81 @@
+/*
+ * 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.client.solrj;
+
+import java.io.ByteArrayInputStream;
+import java.io.InputStream;
+import java.nio.charset.StandardCharsets;
+import org.apache.solr.client.api.model.SolrJerseyResponse;
+import 
org.apache.solr.client.solrj.request.CollectionsApi.ListCollectionsResponse;
+import org.apache.solr.common.util.NamedList;
+import org.junit.Assert;
+import org.junit.Test;
+
+/**
+ * A test ensuring that specific generated SolrRequest classes deserialize 
responses into the
+ * correct type.
+ *
+ * <p>See SOLR-17326 for more context. Consider removing once SOLR-17329 has 
been completed.
+ */
+public class ApiMustacheTemplateTests {
+
+  /**
+   * Tests the return type in generated response classes. This test ensures 
that responses are still
+   * returning API models in case of a naming conflict between response class 
and return type (API
+   * model).
+   */
+  @Test
+  public void testParsedReturnTypes() {
+    JacksonParsingResponse<?> response = getJacksonParsingResponse();
+
+    Object data = null;
+    try {
+      // Parsing may fail if wrong class is used
+      data = response.getParsed();
+    } catch (Exception e) {
+      // Parsing should not fail for the given example.
+      Assert.fail("Response parsing failed with error " + e.getMessage());
+    }
+
+    // In case of a naming conflict, even if parsing would succeed, the type 
would still be the same
+    // as response
+    Assert.assertNotSame(data.getClass(), response.getClass());
+    Assert.assertFalse(data instanceof JacksonParsingResponse<?>);
+
+    // Currently all response types extend SolrJerseyResponse. Adjust if this 
change in the future.
+    Assert.assertTrue(data instanceof SolrJerseyResponse);
+  }
+
+  /**
+   * @return Returns a dummy response of {@link ListCollectionsResponse} with 
data.
+   */
+  private static JacksonParsingResponse<?> getJacksonParsingResponse() {
+    JacksonParsingResponse<?> response = new ListCollectionsResponse(); // API 
response
+
+    // Provide a dummy response for ListCollectionsResponse
+    InputStream inputStream =
+        new ByteArrayInputStream(
+            
"{\"responseHeader\":{\"status\":0,\"QTime\":0},\"collections\":[\"testCollection\"]}"
+                .getBytes(StandardCharsets.UTF_8));
+    NamedList<Object> responseData = new NamedList<>();
+    responseData.add("stream", inputStream);
+    response.setResponse(responseData);
+
+    return response;
+  }
+}

Reply via email to