diff --git a/modules/openapi-generator/src/main/resources/Java/libraries/resttemplate/api.mustache b/modules/openapi-generator/src/main/resources/Java/libraries/resttemplate/api.mustache index 87da6cf0402f..efb554bf644b 100644 --- a/modules/openapi-generator/src/main/resources/Java/libraries/resttemplate/api.mustache +++ b/modules/openapi-generator/src/main/resources/Java/libraries/resttemplate/api.mustache @@ -120,8 +120,10 @@ public class {{classname}} { final MultiValueMap localVarCookieParams = new LinkedMultiValueMap(); final MultiValueMap localVarFormParams = new LinkedMultiValueMap();{{#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}} diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/java/JavaClientCodegenTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/java/JavaClientCodegenTest.java index b4f7a8ca460e..1b1794bb02c5 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/java/JavaClientCodegenTest.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/java/JavaClientCodegenTest.java @@ -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; @@ -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 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 generateFromContract(final String pathToSpecification, final String library) { + return generateFromContract(pathToSpecification, library, new HashMap<>()); + } + + @SneakyThrows + private static Map generateFromContract( + final String pathToSpecification, + final String library, + final Map 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(); diff --git a/modules/openapi-generator/src/test/resources/3_0/java/explode-query-parameter.yaml b/modules/openapi-generator/src/test/resources/3_0/java/explode-query-parameter.yaml new file mode 100644 index 000000000000..0a10cca05078 --- /dev/null +++ b/modules/openapi-generator/src/test/resources/3_0/java/explode-query-parameter.yaml @@ -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 \ No newline at end of file diff --git a/samples/client/petstore/java/resttemplate-jakarta/src/main/java/org/openapitools/client/api/PetApi.java b/samples/client/petstore/java/resttemplate-jakarta/src/main/java/org/openapitools/client/api/PetApi.java index ade2b53c0849..75ba2cf8eed5 100644 --- a/samples/client/petstore/java/resttemplate-jakarta/src/main/java/org/openapitools/client/api/PetApi.java +++ b/samples/client/petstore/java/resttemplate-jakarta/src/main/java/org/openapitools/client/api/PetApi.java @@ -186,6 +186,7 @@ public ResponseEntity> findPetsByStatusWithHttpInfo(List statu localVarQueryParams.putAll(apiClient.parameterToMultiValueMap(ApiClient.CollectionFormat.valueOf("csv".toUpperCase(Locale.ROOT)), "status", status)); + final String[] localVarAccepts = { "application/xml", "application/json" }; @@ -240,6 +241,7 @@ public ResponseEntity> findPetsByTagsWithHttpInfo(List tags) t localVarQueryParams.putAll(apiClient.parameterToMultiValueMap(ApiClient.CollectionFormat.valueOf("csv".toUpperCase(Locale.ROOT)), "tags", tags)); + final String[] localVarAccepts = { "application/xml", "application/json" }; diff --git a/samples/client/petstore/java/resttemplate-jakarta/src/main/java/org/openapitools/client/api/UserApi.java b/samples/client/petstore/java/resttemplate-jakarta/src/main/java/org/openapitools/client/api/UserApi.java index 10d0d6bed3ce..3853d9f8eb01 100644 --- a/samples/client/petstore/java/resttemplate-jakarta/src/main/java/org/openapitools/client/api/UserApi.java +++ b/samples/client/petstore/java/resttemplate-jakarta/src/main/java/org/openapitools/client/api/UserApi.java @@ -328,6 +328,7 @@ public ResponseEntity 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" }; diff --git a/samples/client/petstore/java/resttemplate-swagger1/src/main/java/org/openapitools/client/api/PetApi.java b/samples/client/petstore/java/resttemplate-swagger1/src/main/java/org/openapitools/client/api/PetApi.java index 56085d401a6c..c83dcced6b89 100644 --- a/samples/client/petstore/java/resttemplate-swagger1/src/main/java/org/openapitools/client/api/PetApi.java +++ b/samples/client/petstore/java/resttemplate-swagger1/src/main/java/org/openapitools/client/api/PetApi.java @@ -186,6 +186,7 @@ public ResponseEntity> findPetsByStatusWithHttpInfo(List statu localVarQueryParams.putAll(apiClient.parameterToMultiValueMap(ApiClient.CollectionFormat.valueOf("csv".toUpperCase(Locale.ROOT)), "status", status)); + final String[] localVarAccepts = { "application/xml", "application/json" }; @@ -240,6 +241,7 @@ public ResponseEntity> findPetsByTagsWithHttpInfo(List tags) t localVarQueryParams.putAll(apiClient.parameterToMultiValueMap(ApiClient.CollectionFormat.valueOf("csv".toUpperCase(Locale.ROOT)), "tags", tags)); + final String[] localVarAccepts = { "application/xml", "application/json" }; diff --git a/samples/client/petstore/java/resttemplate-swagger1/src/main/java/org/openapitools/client/api/UserApi.java b/samples/client/petstore/java/resttemplate-swagger1/src/main/java/org/openapitools/client/api/UserApi.java index 51dcb10ef631..61837f12d6ed 100644 --- a/samples/client/petstore/java/resttemplate-swagger1/src/main/java/org/openapitools/client/api/UserApi.java +++ b/samples/client/petstore/java/resttemplate-swagger1/src/main/java/org/openapitools/client/api/UserApi.java @@ -328,6 +328,7 @@ public ResponseEntity 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" }; diff --git a/samples/client/petstore/java/resttemplate-withXml/src/main/java/org/openapitools/client/api/FakeApi.java b/samples/client/petstore/java/resttemplate-withXml/src/main/java/org/openapitools/client/api/FakeApi.java index d0a6a8ccd77c..5e074c80d11c 100644 --- a/samples/client/petstore/java/resttemplate-withXml/src/main/java/org/openapitools/client/api/FakeApi.java +++ b/samples/client/petstore/java/resttemplate-withXml/src/main/java/org/openapitools/client/api/FakeApi.java @@ -349,6 +349,7 @@ public ResponseEntity testBodyWithQueryParamsWithHttpInfo(String query, Us localVarQueryParams.putAll(apiClient.parameterToMultiValueMap(null, "query", query)); + final String[] localVarAccepts = { }; final List localVarAccept = apiClient.selectHeaderAccept(localVarAccepts); final String[] localVarContentTypes = { @@ -575,6 +576,7 @@ public ResponseEntity testEnumParametersWithHttpInfo(List 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) @@ -655,6 +657,7 @@ public ResponseEntity 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) @@ -839,6 +842,7 @@ public ResponseEntity 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 localVarAccept = apiClient.selectHeaderAccept(localVarAccepts); final String[] localVarContentTypes = { }; diff --git a/samples/client/petstore/java/resttemplate-withXml/src/main/java/org/openapitools/client/api/PetApi.java b/samples/client/petstore/java/resttemplate-withXml/src/main/java/org/openapitools/client/api/PetApi.java index 43440d7863dd..1f5ad5f5e9be 100644 --- a/samples/client/petstore/java/resttemplate-withXml/src/main/java/org/openapitools/client/api/PetApi.java +++ b/samples/client/petstore/java/resttemplate-withXml/src/main/java/org/openapitools/client/api/PetApi.java @@ -186,6 +186,7 @@ public ResponseEntity> findPetsByStatusWithHttpInfo(List statu localVarQueryParams.putAll(apiClient.parameterToMultiValueMap(ApiClient.CollectionFormat.valueOf("csv".toUpperCase(Locale.ROOT)), "status", status)); + final String[] localVarAccepts = { "application/xml", "application/json" }; @@ -240,6 +241,7 @@ public ResponseEntity> findPetsByTagsWithHttpInfo(Set tags) thr localVarQueryParams.putAll(apiClient.parameterToMultiValueMap(ApiClient.CollectionFormat.valueOf("csv".toUpperCase(Locale.ROOT)), "tags", tags)); + final String[] localVarAccepts = { "application/xml", "application/json" }; diff --git a/samples/client/petstore/java/resttemplate-withXml/src/main/java/org/openapitools/client/api/UserApi.java b/samples/client/petstore/java/resttemplate-withXml/src/main/java/org/openapitools/client/api/UserApi.java index b48b7d47c89d..749e18012fc5 100644 --- a/samples/client/petstore/java/resttemplate-withXml/src/main/java/org/openapitools/client/api/UserApi.java +++ b/samples/client/petstore/java/resttemplate-withXml/src/main/java/org/openapitools/client/api/UserApi.java @@ -322,6 +322,7 @@ public ResponseEntity 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" }; diff --git a/samples/client/petstore/java/resttemplate/src/main/java/org/openapitools/client/api/FakeApi.java b/samples/client/petstore/java/resttemplate/src/main/java/org/openapitools/client/api/FakeApi.java index d0a6a8ccd77c..5e074c80d11c 100644 --- a/samples/client/petstore/java/resttemplate/src/main/java/org/openapitools/client/api/FakeApi.java +++ b/samples/client/petstore/java/resttemplate/src/main/java/org/openapitools/client/api/FakeApi.java @@ -349,6 +349,7 @@ public ResponseEntity testBodyWithQueryParamsWithHttpInfo(String query, Us localVarQueryParams.putAll(apiClient.parameterToMultiValueMap(null, "query", query)); + final String[] localVarAccepts = { }; final List localVarAccept = apiClient.selectHeaderAccept(localVarAccepts); final String[] localVarContentTypes = { @@ -575,6 +576,7 @@ public ResponseEntity testEnumParametersWithHttpInfo(List 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) @@ -655,6 +657,7 @@ public ResponseEntity 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) @@ -839,6 +842,7 @@ public ResponseEntity 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 localVarAccept = apiClient.selectHeaderAccept(localVarAccepts); final String[] localVarContentTypes = { }; diff --git a/samples/client/petstore/java/resttemplate/src/main/java/org/openapitools/client/api/PetApi.java b/samples/client/petstore/java/resttemplate/src/main/java/org/openapitools/client/api/PetApi.java index 43440d7863dd..1f5ad5f5e9be 100644 --- a/samples/client/petstore/java/resttemplate/src/main/java/org/openapitools/client/api/PetApi.java +++ b/samples/client/petstore/java/resttemplate/src/main/java/org/openapitools/client/api/PetApi.java @@ -186,6 +186,7 @@ public ResponseEntity> findPetsByStatusWithHttpInfo(List statu localVarQueryParams.putAll(apiClient.parameterToMultiValueMap(ApiClient.CollectionFormat.valueOf("csv".toUpperCase(Locale.ROOT)), "status", status)); + final String[] localVarAccepts = { "application/xml", "application/json" }; @@ -240,6 +241,7 @@ public ResponseEntity> findPetsByTagsWithHttpInfo(Set tags) thr localVarQueryParams.putAll(apiClient.parameterToMultiValueMap(ApiClient.CollectionFormat.valueOf("csv".toUpperCase(Locale.ROOT)), "tags", tags)); + final String[] localVarAccepts = { "application/xml", "application/json" }; diff --git a/samples/client/petstore/java/resttemplate/src/main/java/org/openapitools/client/api/UserApi.java b/samples/client/petstore/java/resttemplate/src/main/java/org/openapitools/client/api/UserApi.java index b48b7d47c89d..749e18012fc5 100644 --- a/samples/client/petstore/java/resttemplate/src/main/java/org/openapitools/client/api/UserApi.java +++ b/samples/client/petstore/java/resttemplate/src/main/java/org/openapitools/client/api/UserApi.java @@ -322,6 +322,7 @@ public ResponseEntity 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" };