Apache Solr open-source search software
SOLR-17326: Fix references in generated SolrRequest impls (#2510)
Christos Malliaridis461955f00118
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txtindex 1e41d5fdea6..59dccde12d0 100644--- a/solr/CHANGES.txt+++ b/solr/CHANGES.txt@@ -154,6 +154,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.mustacheindex 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.javanew file mode 100644index 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;+ }+}