Skip to content

Commit 5d6b769

Browse files
authored
Fix issue #907 by ensuring the parameters is properly "exploded" (#14825)
1 parent b4eb9a4 commit 5d6b769

13 files changed

Lines changed: 114 additions & 2 deletions

File tree

modules/openapi-generator/src/main/resources/Java/libraries/resttemplate/api.mustache

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,10 @@ public class {{classname}} {
126126
final MultiValueMap<String, String> localVarCookieParams = new LinkedMultiValueMap<String, String>();
127127
final MultiValueMap<String, Object> localVarFormParams = new LinkedMultiValueMap<String, Object>();{{#hasQueryParams}}
128128

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

132134
{{#headerParams}}if ({{paramName}} != null)
133135
localVarHeaderParams.add("{{baseName}}", apiClient.parameterToString({{paramName}}));{{^-last}}

modules/openapi-generator/src/test/java/org/openapitools/codegen/java/JavaClientCodegenTest.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import io.swagger.v3.oas.models.responses.ApiResponse;
3434
import io.swagger.v3.parser.core.models.ParseOptions;
3535
import io.swagger.v3.parser.util.SchemaTypeUtil;
36+
import lombok.SneakyThrows;
3637
import org.openapitools.codegen.ClientOptInput;
3738
import org.openapitools.codegen.CodegenConstants;
3839
import org.openapitools.codegen.CodegenModel;
@@ -1968,6 +1969,50 @@ public void testForJavaNativeJsonSubtype() throws IOException {
19681969
assertFileNotContains(Paths.get(outputPath + "/src/main/java/org/openapitools/client/model/Pet.java"), "@JsonSubTypes.Type(value = Lizard.class, name = \"Lizard\")");
19691970
}
19701971

1972+
@Test
1973+
public void shouldProperlyExplodeRestTemplateQueryParameters_issue907() {
1974+
1975+
final Map<String, File> files = generateFromContract(
1976+
"src/test/resources/3_0/java/explode-query-parameter.yaml",
1977+
JavaClientCodegen.RESTTEMPLATE
1978+
);
1979+
1980+
JavaFileAssert.assertThat(files.get("DefaultApi.java"))
1981+
.printFileContent()
1982+
.assertMethod("searchWithHttpInfo")
1983+
.bodyContainsLines("localVarQueryParams.putAll(apiClient.parameterToMultiValueMap(null, \"regular-param\", regularParam));")
1984+
.bodyContainsLines("localVarQueryParams.putAll(apiClient.parameterToMultiValueMap(null, \"someString\", objectParam.getSomeString()));")
1985+
.bodyContainsLines("localVarQueryParams.putAll(apiClient.parameterToMultiValueMap(null, \"someBoolean\", objectParam.getSomeBoolean()));")
1986+
.bodyContainsLines("localVarQueryParams.putAll(apiClient.parameterToMultiValueMap(null, \"someInteger\", objectParam.getSomeInteger()));")
1987+
;
1988+
}
1989+
1990+
private static Map<String, File> generateFromContract(final String pathToSpecification, final String library) {
1991+
return generateFromContract(pathToSpecification, library, new HashMap<>());
1992+
}
1993+
1994+
@SneakyThrows
1995+
private static Map<String, File> generateFromContract(
1996+
final String pathToSpecification,
1997+
final String library,
1998+
final Map<String, Object> properties
1999+
) {
2000+
final File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
2001+
output.deleteOnExit();
2002+
2003+
final CodegenConfigurator configurator = new CodegenConfigurator()
2004+
.setGeneratorName("java")
2005+
.setLibrary(library)
2006+
.setAdditionalProperties(properties)
2007+
.setInputSpec(pathToSpecification)
2008+
.setOutputDir(output.getAbsolutePath());
2009+
2010+
final ClientOptInput clientOptInput = configurator.toClientOptInput();
2011+
final DefaultGenerator generator = new DefaultGenerator();
2012+
return generator.opts(clientOptInput).generate().stream()
2013+
.collect(Collectors.toMap(File::getName, Function.identity()));
2014+
}
2015+
19712016
@Test
19722017
public void testForJavaApacheHttpClientJsonSubtype() throws IOException {
19732018
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
openapi: 3.0.3
2+
info:
3+
title: Explode query params
4+
description: "Explode query params"
5+
version: "1.0.0"
6+
servers:
7+
- url: http://localhost:8080
8+
paths:
9+
/api/search:
10+
get:
11+
operationId: Search
12+
parameters:
13+
- name: regular-param
14+
in: query
15+
required: false
16+
schema:
17+
type: string
18+
- name: object-param
19+
in: query
20+
required: true
21+
schema:
22+
type: object
23+
properties:
24+
someString:
25+
type: string
26+
someBoolean:
27+
type: boolean
28+
someInteger:
29+
type: integer
30+
responses:
31+
'200':
32+
description: Some description.
33+
content:
34+
application/json:
35+
schema:
36+
$ref: '#/components/schemas/SomeReturnValue'
37+
components:
38+
schemas:
39+
SomeReturnValue:
40+
type: object
41+
required:
42+
- someValue
43+
properties:
44+
someValue:
45+
type: string

samples/client/petstore/java/resttemplate-jakarta/src/main/java/org/openapitools/client/api/PetApi.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ public ResponseEntity<List<Pet>> findPetsByStatusWithHttpInfo(List<String> statu
186186

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

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

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

244+
243245
final String[] localVarAccepts = {
244246
"application/xml", "application/json"
245247
};

samples/client/petstore/java/resttemplate-jakarta/src/main/java/org/openapitools/client/api/UserApi.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,7 @@ public ResponseEntity<String> loginUserWithHttpInfo(String username, String pass
328328
localVarQueryParams.putAll(apiClient.parameterToMultiValueMap(null, "username", username));
329329
localVarQueryParams.putAll(apiClient.parameterToMultiValueMap(null, "password", password));
330330

331+
331332
final String[] localVarAccepts = {
332333
"application/xml", "application/json"
333334
};

samples/client/petstore/java/resttemplate-swagger1/src/main/java/org/openapitools/client/api/PetApi.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ public ResponseEntity<List<Pet>> findPetsByStatusWithHttpInfo(List<String> statu
186186

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

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

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

244+
243245
final String[] localVarAccepts = {
244246
"application/xml", "application/json"
245247
};

samples/client/petstore/java/resttemplate-swagger1/src/main/java/org/openapitools/client/api/UserApi.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,7 @@ public ResponseEntity<String> loginUserWithHttpInfo(String username, String pass
328328
localVarQueryParams.putAll(apiClient.parameterToMultiValueMap(null, "username", username));
329329
localVarQueryParams.putAll(apiClient.parameterToMultiValueMap(null, "password", password));
330330

331+
331332
final String[] localVarAccepts = {
332333
"application/xml", "application/json"
333334
};

samples/client/petstore/java/resttemplate-withXml/src/main/java/org/openapitools/client/api/FakeApi.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,7 @@ public ResponseEntity<Void> testBodyWithQueryParamsWithHttpInfo(String query, Us
349349

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

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

579+
578580
if (enumHeaderStringArray != null)
579581
localVarHeaderParams.add("enum_header_string_array", apiClient.parameterToString(enumHeaderStringArray));
580582
if (enumHeaderString != null)
@@ -655,6 +657,7 @@ public ResponseEntity<Void> testGroupParametersWithHttpInfo(Integer requiredStri
655657
localVarQueryParams.putAll(apiClient.parameterToMultiValueMap(null, "string_group", stringGroup));
656658
localVarQueryParams.putAll(apiClient.parameterToMultiValueMap(null, "int64_group", int64Group));
657659

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

845+
842846
final String[] localVarAccepts = { };
843847
final List<MediaType> localVarAccept = apiClient.selectHeaderAccept(localVarAccepts);
844848
final String[] localVarContentTypes = { };

samples/client/petstore/java/resttemplate-withXml/src/main/java/org/openapitools/client/api/PetApi.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ public ResponseEntity<List<Pet>> findPetsByStatusWithHttpInfo(List<String> statu
186186

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

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

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

244+
243245
final String[] localVarAccepts = {
244246
"application/xml", "application/json"
245247
};

samples/client/petstore/java/resttemplate-withXml/src/main/java/org/openapitools/client/api/UserApi.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,7 @@ public ResponseEntity<String> loginUserWithHttpInfo(String username, String pass
322322
localVarQueryParams.putAll(apiClient.parameterToMultiValueMap(null, "username", username));
323323
localVarQueryParams.putAll(apiClient.parameterToMultiValueMap(null, "password", password));
324324

325+
325326
final String[] localVarAccepts = {
326327
"application/xml", "application/json"
327328
};

0 commit comments

Comments
 (0)