Skip to content

Commit

Permalink
Fix issue OpenAPITools#907 by ensuring the parameters is properly "ex…
Browse files Browse the repository at this point in the history
…ploded"
  • Loading branch information
GregDThomas committed Mar 31, 2023
1 parent b59d535 commit 4216ad6
Show file tree
Hide file tree
Showing 13 changed files with 114 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,10 @@ public class {{classname}} {
final MultiValueMap<String, String> localVarCookieParams = new LinkedMultiValueMap<String, String>();
final MultiValueMap<String, Object> localVarFormParams = new LinkedMultiValueMap<String, Object>();{{#hasQueryParams}}

{{#queryParams}}localVarQueryParams.putAll(apiClient.parameterToMultiValueMap({{#collectionFormat}}ApiClient.CollectionFormat.valueOf("{{{.}}}".toUpperCase(Locale.ROOT)){{/collectionFormat}}{{^collectionFormat}}null{{/collectionFormat}}, "{{baseName}}", {{paramName}}));{{^-last}}
{{/-last}}{{/queryParams}}{{/hasQueryParams}}{{#hasHeaderParams}}
{{#queryParams}}{{#isExplode}}{{#hasVars}}{{#vars}} localVarQueryParams.putAll(apiClient.parameterToMultiValueMap({{#collectionFormat}}ApiClient.CollectionFormat.valueOf("{{{.}}}".toUpperCase(Locale.ROOT)){{/collectionFormat}}{{^collectionFormat}}null{{/collectionFormat}}, "{{baseName}}", {{paramName}}.{{getter}}()));
{{/vars}}{{/hasVars}}{{^hasVars}} localVarQueryParams.putAll(apiClient.parameterToMultiValueMap({{#collectionFormat}}ApiClient.CollectionFormat.valueOf("{{{.}}}".toUpperCase(Locale.ROOT)){{/collectionFormat}}{{^collectionFormat}}null{{/collectionFormat}}, "{{baseName}}", {{paramName}}));
{{/hasVars}}{{/isExplode}}{{^isExplode}} localVarQueryParams.putAll(apiClient.parameterToMultiValueMap({{#collectionFormat}}ApiClient.CollectionFormat.valueOf("{{{.}}}".toUpperCase(Locale.ROOT)){{/collectionFormat}}{{^collectionFormat}}null{{/collectionFormat}}, "{{baseName}}", {{paramName}}));
{{/isExplode}}{{/queryParams}}{{/hasQueryParams}}{{#hasHeaderParams}}

{{#headerParams}}if ({{paramName}} != null)
localVarHeaderParams.add("{{baseName}}", apiClient.parameterToString({{paramName}}));{{^-last}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import io.swagger.v3.oas.models.responses.ApiResponse;
import io.swagger.v3.parser.core.models.ParseOptions;
import io.swagger.v3.parser.util.SchemaTypeUtil;
import lombok.SneakyThrows;
import org.openapitools.codegen.ClientOptInput;
import org.openapitools.codegen.CodegenConstants;
import org.openapitools.codegen.CodegenModel;
Expand Down Expand Up @@ -1874,6 +1875,50 @@ public void testForJavaNativeJsonSubtype() throws IOException {
assertFileNotContains(Paths.get(outputPath + "/src/main/java/org/openapitools/client/model/Pet.java"), "@JsonSubTypes.Type(value = Lizard.class, name = \"Lizard\")");
}

@Test
public void shouldProperlyExplodeRestTemplateQueryParameters_issue907() {

final Map<String, File> files = generateFromContract(
"src/test/resources/3_0/java/explode-query-parameter.yaml",
JavaClientCodegen.RESTTEMPLATE
);

JavaFileAssert.assertThat(files.get("DefaultApi.java"))
.printFileContent()
.assertMethod("searchWithHttpInfo")
.bodyContainsLines("localVarQueryParams.putAll(apiClient.parameterToMultiValueMap(null, \"regular-param\", regularParam));")
.bodyContainsLines("localVarQueryParams.putAll(apiClient.parameterToMultiValueMap(null, \"someString\", objectParam.getSomeString()));")
.bodyContainsLines("localVarQueryParams.putAll(apiClient.parameterToMultiValueMap(null, \"someBoolean\", objectParam.getSomeBoolean()));")
.bodyContainsLines("localVarQueryParams.putAll(apiClient.parameterToMultiValueMap(null, \"someInteger\", objectParam.getSomeInteger()));")
;
}

private static Map<String, File> generateFromContract(final String pathToSpecification, final String library) {
return generateFromContract(pathToSpecification, library, new HashMap<>());
}

@SneakyThrows
private static Map<String, File> generateFromContract(
final String pathToSpecification,
final String library,
final Map<String, Object> properties
) {
final File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
output.deleteOnExit();

final CodegenConfigurator configurator = new CodegenConfigurator()
.setGeneratorName("java")
.setLibrary(library)
.setAdditionalProperties(properties)
.setInputSpec(pathToSpecification)
.setOutputDir(output.getAbsolutePath());

final ClientOptInput clientOptInput = configurator.toClientOptInput();
final DefaultGenerator generator = new DefaultGenerator();
return generator.opts(clientOptInput).generate().stream()
.collect(Collectors.toMap(File::getName, Function.identity()));
}

@Test
public void testForJavaApacheHttpClientJsonSubtype() throws IOException {
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
openapi: 3.0.3
info:
title: Explode query params
description: "Explode query params"
version: "1.0.0"
servers:
- url: http://localhost:8080
paths:
/api/search:
get:
operationId: Search
parameters:
- name: regular-param
in: query
required: false
schema:
type: string
- name: object-param
in: query
required: true
schema:
type: object
properties:
someString:
type: string
someBoolean:
type: boolean
someInteger:
type: integer
responses:
'200':
description: Some description.
content:
application/json:
schema:
$ref: '#/components/schemas/SomeReturnValue'
components:
schemas:
SomeReturnValue:
type: object
required:
- someValue
properties:
someValue:
type: string
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ public ResponseEntity<List<Pet>> findPetsByStatusWithHttpInfo(List<String> statu

localVarQueryParams.putAll(apiClient.parameterToMultiValueMap(ApiClient.CollectionFormat.valueOf("csv".toUpperCase(Locale.ROOT)), "status", status));


final String[] localVarAccepts = {
"application/xml", "application/json"
};
Expand Down Expand Up @@ -240,6 +241,7 @@ public ResponseEntity<List<Pet>> findPetsByTagsWithHttpInfo(List<String> tags) t

localVarQueryParams.putAll(apiClient.parameterToMultiValueMap(ApiClient.CollectionFormat.valueOf("csv".toUpperCase(Locale.ROOT)), "tags", tags));


final String[] localVarAccepts = {
"application/xml", "application/json"
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,7 @@ public ResponseEntity<String> loginUserWithHttpInfo(String username, String pass
localVarQueryParams.putAll(apiClient.parameterToMultiValueMap(null, "username", username));
localVarQueryParams.putAll(apiClient.parameterToMultiValueMap(null, "password", password));


final String[] localVarAccepts = {
"application/xml", "application/json"
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ public ResponseEntity<List<Pet>> findPetsByStatusWithHttpInfo(List<String> statu

localVarQueryParams.putAll(apiClient.parameterToMultiValueMap(ApiClient.CollectionFormat.valueOf("csv".toUpperCase(Locale.ROOT)), "status", status));


final String[] localVarAccepts = {
"application/xml", "application/json"
};
Expand Down Expand Up @@ -240,6 +241,7 @@ public ResponseEntity<List<Pet>> findPetsByTagsWithHttpInfo(List<String> tags) t

localVarQueryParams.putAll(apiClient.parameterToMultiValueMap(ApiClient.CollectionFormat.valueOf("csv".toUpperCase(Locale.ROOT)), "tags", tags));


final String[] localVarAccepts = {
"application/xml", "application/json"
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,7 @@ public ResponseEntity<String> loginUserWithHttpInfo(String username, String pass
localVarQueryParams.putAll(apiClient.parameterToMultiValueMap(null, "username", username));
localVarQueryParams.putAll(apiClient.parameterToMultiValueMap(null, "password", password));


final String[] localVarAccepts = {
"application/xml", "application/json"
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,7 @@ public ResponseEntity<Void> testBodyWithQueryParamsWithHttpInfo(String query, Us

localVarQueryParams.putAll(apiClient.parameterToMultiValueMap(null, "query", query));


final String[] localVarAccepts = { };
final List<MediaType> localVarAccept = apiClient.selectHeaderAccept(localVarAccepts);
final String[] localVarContentTypes = {
Expand Down Expand Up @@ -575,6 +576,7 @@ public ResponseEntity<Void> testEnumParametersWithHttpInfo(List<String> enumHead
localVarQueryParams.putAll(apiClient.parameterToMultiValueMap(null, "enum_query_integer", enumQueryInteger));
localVarQueryParams.putAll(apiClient.parameterToMultiValueMap(null, "enum_query_double", enumQueryDouble));


if (enumHeaderStringArray != null)
localVarHeaderParams.add("enum_header_string_array", apiClient.parameterToString(enumHeaderStringArray));
if (enumHeaderString != null)
Expand Down Expand Up @@ -655,6 +657,7 @@ public ResponseEntity<Void> testGroupParametersWithHttpInfo(Integer requiredStri
localVarQueryParams.putAll(apiClient.parameterToMultiValueMap(null, "string_group", stringGroup));
localVarQueryParams.putAll(apiClient.parameterToMultiValueMap(null, "int64_group", int64Group));


if (requiredBooleanGroup != null)
localVarHeaderParams.add("required_boolean_group", apiClient.parameterToString(requiredBooleanGroup));
if (booleanGroup != null)
Expand Down Expand Up @@ -839,6 +842,7 @@ public ResponseEntity<Void> testQueryParameterCollectionFormatWithHttpInfo(List<
localVarQueryParams.putAll(apiClient.parameterToMultiValueMap(ApiClient.CollectionFormat.valueOf("csv".toUpperCase(Locale.ROOT)), "url", url));
localVarQueryParams.putAll(apiClient.parameterToMultiValueMap(ApiClient.CollectionFormat.valueOf("multi".toUpperCase(Locale.ROOT)), "context", context));


final String[] localVarAccepts = { };
final List<MediaType> localVarAccept = apiClient.selectHeaderAccept(localVarAccepts);
final String[] localVarContentTypes = { };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ public ResponseEntity<List<Pet>> findPetsByStatusWithHttpInfo(List<String> statu

localVarQueryParams.putAll(apiClient.parameterToMultiValueMap(ApiClient.CollectionFormat.valueOf("csv".toUpperCase(Locale.ROOT)), "status", status));


final String[] localVarAccepts = {
"application/xml", "application/json"
};
Expand Down Expand Up @@ -240,6 +241,7 @@ public ResponseEntity<Set<Pet>> findPetsByTagsWithHttpInfo(Set<String> tags) thr

localVarQueryParams.putAll(apiClient.parameterToMultiValueMap(ApiClient.CollectionFormat.valueOf("csv".toUpperCase(Locale.ROOT)), "tags", tags));


final String[] localVarAccepts = {
"application/xml", "application/json"
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,7 @@ public ResponseEntity<String> loginUserWithHttpInfo(String username, String pass
localVarQueryParams.putAll(apiClient.parameterToMultiValueMap(null, "username", username));
localVarQueryParams.putAll(apiClient.parameterToMultiValueMap(null, "password", password));


final String[] localVarAccepts = {
"application/xml", "application/json"
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,7 @@ public ResponseEntity<Void> testBodyWithQueryParamsWithHttpInfo(String query, Us

localVarQueryParams.putAll(apiClient.parameterToMultiValueMap(null, "query", query));


final String[] localVarAccepts = { };
final List<MediaType> localVarAccept = apiClient.selectHeaderAccept(localVarAccepts);
final String[] localVarContentTypes = {
Expand Down Expand Up @@ -575,6 +576,7 @@ public ResponseEntity<Void> testEnumParametersWithHttpInfo(List<String> enumHead
localVarQueryParams.putAll(apiClient.parameterToMultiValueMap(null, "enum_query_integer", enumQueryInteger));
localVarQueryParams.putAll(apiClient.parameterToMultiValueMap(null, "enum_query_double", enumQueryDouble));


if (enumHeaderStringArray != null)
localVarHeaderParams.add("enum_header_string_array", apiClient.parameterToString(enumHeaderStringArray));
if (enumHeaderString != null)
Expand Down Expand Up @@ -655,6 +657,7 @@ public ResponseEntity<Void> testGroupParametersWithHttpInfo(Integer requiredStri
localVarQueryParams.putAll(apiClient.parameterToMultiValueMap(null, "string_group", stringGroup));
localVarQueryParams.putAll(apiClient.parameterToMultiValueMap(null, "int64_group", int64Group));


if (requiredBooleanGroup != null)
localVarHeaderParams.add("required_boolean_group", apiClient.parameterToString(requiredBooleanGroup));
if (booleanGroup != null)
Expand Down Expand Up @@ -839,6 +842,7 @@ public ResponseEntity<Void> testQueryParameterCollectionFormatWithHttpInfo(List<
localVarQueryParams.putAll(apiClient.parameterToMultiValueMap(ApiClient.CollectionFormat.valueOf("csv".toUpperCase(Locale.ROOT)), "url", url));
localVarQueryParams.putAll(apiClient.parameterToMultiValueMap(ApiClient.CollectionFormat.valueOf("multi".toUpperCase(Locale.ROOT)), "context", context));


final String[] localVarAccepts = { };
final List<MediaType> localVarAccept = apiClient.selectHeaderAccept(localVarAccepts);
final String[] localVarContentTypes = { };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ public ResponseEntity<List<Pet>> findPetsByStatusWithHttpInfo(List<String> statu

localVarQueryParams.putAll(apiClient.parameterToMultiValueMap(ApiClient.CollectionFormat.valueOf("csv".toUpperCase(Locale.ROOT)), "status", status));


final String[] localVarAccepts = {
"application/xml", "application/json"
};
Expand Down Expand Up @@ -240,6 +241,7 @@ public ResponseEntity<Set<Pet>> findPetsByTagsWithHttpInfo(Set<String> tags) thr

localVarQueryParams.putAll(apiClient.parameterToMultiValueMap(ApiClient.CollectionFormat.valueOf("csv".toUpperCase(Locale.ROOT)), "tags", tags));


final String[] localVarAccepts = {
"application/xml", "application/json"
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,7 @@ public ResponseEntity<String> loginUserWithHttpInfo(String username, String pass
localVarQueryParams.putAll(apiClient.parameterToMultiValueMap(null, "username", username));
localVarQueryParams.putAll(apiClient.parameterToMultiValueMap(null, "password", password));


final String[] localVarAccepts = {
"application/xml", "application/json"
};
Expand Down

0 comments on commit 4216ad6

Please sign in to comment.