Skip to content

Commit

Permalink
[JAVA][APACHE] Fix apache http client query parameters (#14020)
Browse files Browse the repository at this point in the history
* Fix apache http client query parameters

* Update samples
  • Loading branch information
sorin-florea authored Nov 23, 2022
1 parent 906ec5d commit d74cefb
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,30 @@ public class {{classname}} {
{{javaUtilPrefix}}Map<String, Object> localVarFormParams = new {{javaUtilPrefix}}HashMap<String, Object>();

{{#queryParams}}
{{#isDeepObject}}
{{#collectionFormat}}localVarCollectionQueryParams.addAll(apiClient.parameterToPairs("{{{collectionFormat}}}", {{/collectionFormat}}{{^collectionFormat}}localVarQueryParams.addAll(apiClient.parameterToPair({{/collectionFormat}}"{{baseName}}", {{paramName}}));
{{/isDeepObject}}
{{^isDeepObject}}
{{#isExplode}}
{{#hasVars}}
{{#vars}}
{{#isArray}}
localVarQueryParams.addAll(apiClient.parameterToPair("multi", "{{baseName}}", {{paramName}}.{{getter}}()));
{{/isArray}}
{{^isArray}}
localVarQueryParams.addAll(apiClient.parameterToPair("{{baseName}}", {{paramName}}.{{getter}}()));
{{/isArray}}
{{/vars}}
{{/hasVars}}
{{^hasVars}}
{{#collectionFormat}}localVarCollectionQueryParams.addAll(apiClient.parameterToPairs("{{{collectionFormat}}}", {{/collectionFormat}}{{^collectionFormat}}localVarQueryParams.addAll(apiClient.parameterToPair({{/collectionFormat}}"{{baseName}}", {{paramName}}));
{{/hasVars}}
{{/isExplode}}
{{^isExplode}}
{{#collectionFormat}}localVarCollectionQueryParams.addAll(apiClient.parameterToPairs("{{{collectionFormat}}}", {{/collectionFormat}}{{^collectionFormat}}localVarQueryParams.addAll(apiClient.parameterToPair({{/collectionFormat}}"{{baseName}}", {{paramName}}));
{{/isExplode}}
{{/isDeepObject}}
{{/queryParams}}

{{#headerParams}}if ({{paramName}} != null)
localVarHeaderParams.put("{{baseName}}", apiClient.parameterToString({{paramName}}));
{{/headerParams}}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* Copyright 2018 OpenAPI-Generator Contributors (https://openapi-generator.tech)
* Copyright 2018 SmartBear Software
*
* Licensed 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
*
* https://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.openapitools.codegen.java.apachehttpclient;

import org.openapitools.codegen.ClientOptInput;
import org.openapitools.codegen.CodegenConstants;
import org.openapitools.codegen.DefaultGenerator;
import org.openapitools.codegen.TestUtils;
import org.openapitools.codegen.config.CodegenConfigurator;
import org.openapitools.codegen.languages.JavaClientCodegen;
import org.testng.Assert;
import org.testng.annotations.Test;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.openapitools.codegen.TestUtils.validateJavaSourceFiles;

public class ApacheHttpClientCodegenTest {

@Test
public void testApacheHttpClientExplodedQueryParamObject() throws IOException {
Map<String, Object> properties = new HashMap<>();
properties.put(CodegenConstants.API_PACKAGE, "xyz.abcdef.api");

File output = Files.createTempDirectory("test").toFile();
output.deleteOnExit();

final CodegenConfigurator configurator = new CodegenConfigurator()
.setGeneratorName("java")
.setLibrary(JavaClientCodegen.APACHE)
.setAdditionalProperties(properties)
.setInputSpec("src/test/resources/3_0/issue4808.yaml")
.setOutputDir(output.getAbsolutePath().replace("\\", "/"));

final ClientOptInput clientOptInput = configurator.toClientOptInput();
DefaultGenerator generator = new DefaultGenerator();
List<File> files = generator.opts(clientOptInput).generate();

Assert.assertEquals(files.size(), 41);
validateJavaSourceFiles(files);

TestUtils.assertFileContains(Paths.get(output + "/src/main/java/xyz/abcdef/api/DefaultApi.java"),
"localVarQueryParams.addAll(apiClient.parameterToPair(\"since\", queryObject.getSince()));",
"localVarQueryParams.addAll(apiClient.parameterToPair(\"sinceBuild\", queryObject.getSinceBuild()));",
"localVarQueryParams.addAll(apiClient.parameterToPair(\"maxBuilds\", queryObject.getMaxBuilds()));",
"localVarQueryParams.addAll(apiClient.parameterToPair(\"maxWaitSecs\", queryObject.getMaxWaitSecs()));"
);
}

@Test
public void testApacheHttpClientExplodedQueryParamWithArrayProperty() throws IOException {
Map<String, Object> properties = new HashMap<>();
properties.put(CodegenConstants.API_PACKAGE, "xyz.abcdef.api");

File output = Files.createTempDirectory("test").toFile();
output.deleteOnExit();

final CodegenConfigurator configurator = new CodegenConfigurator()
.setGeneratorName("java")
.setLibrary(JavaClientCodegen.APACHE)
.setAdditionalProperties(properties)
.setInputSpec("src/test/resources/3_0/exploded-query-param-array.yaml")
.setOutputDir(output.getAbsolutePath().replace("\\", "/"));

final ClientOptInput clientOptInput = configurator.toClientOptInput();
DefaultGenerator generator = new DefaultGenerator();
generator.opts(clientOptInput).generate();

TestUtils.assertFileContains(Paths.get(output + "/src/main/java/xyz/abcdef/api/DefaultApi.java"),
"localVarQueryParams.addAll(apiClient.parameterToPair(\"multi\", \"values\", queryObject.getValues()))"
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ public Client call123testSpecialTags(Client body) throws ApiException {
Map<String, String> localVarCookieParams = new HashMap<String, String>();
Map<String, Object> localVarFormParams = new HashMap<String, Object>();





Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ public void createXmlItem(XmlItem xmlItem) throws ApiException {
Map<String, String> localVarCookieParams = new HashMap<String, String>();
Map<String, Object> localVarFormParams = new HashMap<String, Object>();





Expand Down Expand Up @@ -131,7 +130,6 @@ public Boolean fakeOuterBooleanSerialize(Boolean body) throws ApiException {
Map<String, String> localVarCookieParams = new HashMap<String, String>();
Map<String, Object> localVarFormParams = new HashMap<String, Object>();





Expand Down Expand Up @@ -183,7 +181,6 @@ public OuterComposite fakeOuterCompositeSerialize(OuterComposite body) throws Ap
Map<String, String> localVarCookieParams = new HashMap<String, String>();
Map<String, Object> localVarFormParams = new HashMap<String, Object>();





Expand Down Expand Up @@ -235,7 +232,6 @@ public BigDecimal fakeOuterNumberSerialize(BigDecimal body) throws ApiException
Map<String, String> localVarCookieParams = new HashMap<String, String>();
Map<String, Object> localVarFormParams = new HashMap<String, Object>();





Expand Down Expand Up @@ -287,7 +283,6 @@ public String fakeOuterStringSerialize(String body) throws ApiException {
Map<String, String> localVarCookieParams = new HashMap<String, String>();
Map<String, Object> localVarFormParams = new HashMap<String, Object>();





Expand Down Expand Up @@ -343,7 +338,6 @@ public void testBodyWithFileSchema(FileSchemaTestClass body) throws ApiException
Map<String, String> localVarCookieParams = new HashMap<String, String>();
Map<String, Object> localVarFormParams = new HashMap<String, Object>();





Expand Down Expand Up @@ -405,7 +399,6 @@ public void testBodyWithQueryParams(String query, User body) throws ApiException
Map<String, Object> localVarFormParams = new HashMap<String, Object>();

localVarQueryParams.addAll(apiClient.parameterToPair("query", query));




Expand Down Expand Up @@ -461,7 +454,6 @@ public Client testClientModel(Client body) throws ApiException {
Map<String, String> localVarCookieParams = new HashMap<String, String>();
Map<String, Object> localVarFormParams = new HashMap<String, Object>();





Expand Down Expand Up @@ -545,7 +537,6 @@ public void testEndpointParameters(BigDecimal number, Double _double, String pat
Map<String, String> localVarCookieParams = new HashMap<String, String>();
Map<String, Object> localVarFormParams = new HashMap<String, Object>();




if (integer != null)
Expand Down Expand Up @@ -634,7 +625,6 @@ public void testEnumParameters(List<String> enumHeaderStringArray, String enumHe
localVarQueryParams.addAll(apiClient.parameterToPair("enum_query_string", enumQueryString));
localVarQueryParams.addAll(apiClient.parameterToPair("enum_query_integer", enumQueryInteger));
localVarQueryParams.addAll(apiClient.parameterToPair("enum_query_double", enumQueryDouble));

if (enumHeaderStringArray != null)
localVarHeaderParams.put("enum_header_string_array", apiClient.parameterToString(enumHeaderStringArray));
if (enumHeaderString != null)
Expand Down Expand Up @@ -716,7 +706,6 @@ public void testGroupParameters(Integer requiredStringGroup, Boolean requiredBoo
localVarQueryParams.addAll(apiClient.parameterToPair("required_int64_group", requiredInt64Group));
localVarQueryParams.addAll(apiClient.parameterToPair("string_group", stringGroup));
localVarQueryParams.addAll(apiClient.parameterToPair("int64_group", int64Group));

if (requiredBooleanGroup != null)
localVarHeaderParams.put("required_boolean_group", apiClient.parameterToString(requiredBooleanGroup));
if (booleanGroup != null)
Expand Down Expand Up @@ -775,7 +764,6 @@ public void testInlineAdditionalProperties(Map<String, String> param) throws Api
Map<String, String> localVarCookieParams = new HashMap<String, String>();
Map<String, Object> localVarFormParams = new HashMap<String, Object>();





Expand Down Expand Up @@ -836,7 +824,6 @@ public void testJsonFormData(String param, String param2) throws ApiException {
Map<String, String> localVarCookieParams = new HashMap<String, String>();
Map<String, Object> localVarFormParams = new HashMap<String, Object>();




if (param != null)
Expand Down Expand Up @@ -924,7 +911,6 @@ public void testQueryParameterCollectionFormat(List<String> pipe, List<String> i
localVarCollectionQueryParams.addAll(apiClient.parameterToPairs("ssv", "http", http));
localVarCollectionQueryParams.addAll(apiClient.parameterToPairs("csv", "url", url));
localVarCollectionQueryParams.addAll(apiClient.parameterToPairs("multi", "context", context));




Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ public Client testClassname(Client body) throws ApiException {
Map<String, String> localVarCookieParams = new HashMap<String, String>();
Map<String, Object> localVarFormParams = new HashMap<String, Object>();





Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ public void addPet(Pet body) throws ApiException {
Map<String, String> localVarCookieParams = new HashMap<String, String>();
Map<String, Object> localVarFormParams = new HashMap<String, Object>();





Expand Down Expand Up @@ -132,7 +131,6 @@ public void deletePet(Long petId, String apiKey) throws ApiException {
Map<String, String> localVarCookieParams = new HashMap<String, String>();
Map<String, Object> localVarFormParams = new HashMap<String, Object>();


if (apiKey != null)
localVarHeaderParams.put("api_key", apiClient.parameterToString(apiKey));

Expand Down Expand Up @@ -191,7 +189,6 @@ public List<Pet> findPetsByStatus(List<String> status) throws ApiException {
Map<String, Object> localVarFormParams = new HashMap<String, Object>();

localVarCollectionQueryParams.addAll(apiClient.parameterToPairs("csv", "status", status));




Expand Down Expand Up @@ -251,7 +248,6 @@ public Set<Pet> findPetsByTags(Set<String> tags) throws ApiException {
Map<String, Object> localVarFormParams = new HashMap<String, Object>();

localVarCollectionQueryParams.addAll(apiClient.parameterToPairs("csv", "tags", tags));




Expand Down Expand Up @@ -309,7 +305,6 @@ public Pet getPetById(Long petId) throws ApiException {
Map<String, String> localVarCookieParams = new HashMap<String, String>();
Map<String, Object> localVarFormParams = new HashMap<String, Object>();





Expand Down Expand Up @@ -365,7 +360,6 @@ public void updatePet(Pet body) throws ApiException {
Map<String, String> localVarCookieParams = new HashMap<String, String>();
Map<String, Object> localVarFormParams = new HashMap<String, Object>();





Expand Down Expand Up @@ -423,7 +417,6 @@ public void updatePetWithForm(Long petId, String name, String status) throws Api
Map<String, String> localVarCookieParams = new HashMap<String, String>();
Map<String, Object> localVarFormParams = new HashMap<String, Object>();




if (name != null)
Expand Down Expand Up @@ -486,7 +479,6 @@ public ModelApiResponse uploadFile(Long petId, String additionalMetadata, File _
Map<String, String> localVarCookieParams = new HashMap<String, String>();
Map<String, Object> localVarFormParams = new HashMap<String, Object>();




if (additionalMetadata != null)
Expand Down Expand Up @@ -555,7 +547,6 @@ public ModelApiResponse uploadFileWithRequiredFile(Long petId, File requiredFile
Map<String, String> localVarCookieParams = new HashMap<String, String>();
Map<String, Object> localVarFormParams = new HashMap<String, Object>();




if (additionalMetadata != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ public void deleteOrder(String orderId) throws ApiException {
Map<String, String> localVarCookieParams = new HashMap<String, String>();
Map<String, Object> localVarFormParams = new HashMap<String, Object>();





Expand Down Expand Up @@ -123,7 +122,6 @@ public Map<String, Integer> getInventory() throws ApiException {
Map<String, String> localVarCookieParams = new HashMap<String, String>();
Map<String, Object> localVarFormParams = new HashMap<String, Object>();





Expand Down Expand Up @@ -181,7 +179,6 @@ public Order getOrderById(Long orderId) throws ApiException {
Map<String, String> localVarCookieParams = new HashMap<String, String>();
Map<String, Object> localVarFormParams = new HashMap<String, Object>();





Expand Down Expand Up @@ -238,7 +235,6 @@ public Order placeOrder(Order body) throws ApiException {
Map<String, String> localVarCookieParams = new HashMap<String, String>();
Map<String, Object> localVarFormParams = new HashMap<String, Object>();





Expand Down
Loading

0 comments on commit d74cefb

Please sign in to comment.