From 3099a2fcd384c80861cc02a480bdff9f73b24b31 Mon Sep 17 00:00:00 2001 From: Nikita Shmakov Date: Thu, 21 Dec 2023 14:35:07 +0300 Subject: [PATCH] webclient explode query parameters support (#17441) --- .../Java/libraries/webclient/api.mustache | 9 ++++--- .../codegen/java/JavaClientCodegenTest.java | 24 +++++++++++++++++++ .../org/openapitools/client/api/FakeApi.java | 7 ++++-- .../org/openapitools/client/api/PetApi.java | 5 ++-- .../org/openapitools/client/api/UserApi.java | 2 +- .../org/openapitools/client/api/FakeApi.java | 7 ++++-- .../org/openapitools/client/api/PetApi.java | 5 ++-- .../org/openapitools/client/api/UserApi.java | 2 +- .../org/openapitools/client/api/FakeApi.java | 7 ++++-- .../org/openapitools/client/api/PetApi.java | 5 ++-- .../org/openapitools/client/api/UserApi.java | 2 +- 11 files changed, 55 insertions(+), 20 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/Java/libraries/webclient/api.mustache b/modules/openapi-generator/src/main/resources/Java/libraries/webclient/api.mustache index 2a266136c30c..a7ea64a15f7f 100644 --- a/modules/openapi-generator/src/main/resources/Java/libraries/webclient/api.mustache +++ b/modules/openapi-generator/src/main/resources/Java/libraries/webclient/api.mustache @@ -92,11 +92,10 @@ public class {{classname}} { final MultiValueMap formParams = new LinkedMultiValueMap(); {{#hasQueryParams}} - {{#queryParams}} - queryParams.putAll(apiClient.parameterToMultiValueMap({{#collectionFormat}}ApiClient.CollectionFormat.valueOf("{{{.}}}".toUpperCase(Locale.ROOT)){{/collectionFormat}}{{^collectionFormat}}null{{/collectionFormat}}, "{{baseName}}", {{paramName}})); - {{/queryParams}} - {{/hasQueryParams}} - {{#hasHeaderParams}} + {{#queryParams}}{{#isExplode}}{{#hasVars}}{{#vars}}queryParams.putAll(apiClient.parameterToMultiValueMap({{#collectionFormat}}ApiClient.CollectionFormat.valueOf("{{{.}}}".toUpperCase(Locale.ROOT)){{/collectionFormat}}{{^collectionFormat}}null{{/collectionFormat}}, "{{baseName}}", {{paramName}}.{{getter}}())); + {{/vars}}{{/hasVars}}{{^hasVars}}queryParams.putAll(apiClient.parameterToMultiValueMap({{#collectionFormat}}ApiClient.CollectionFormat.valueOf("{{{.}}}".toUpperCase(Locale.ROOT)){{/collectionFormat}}{{^collectionFormat}}null{{/collectionFormat}}, "{{baseName}}", {{paramName}})); + {{/hasVars}}{{/isExplode}}{{^isExplode}}queryParams.putAll(apiClient.parameterToMultiValueMap({{#collectionFormat}}ApiClient.CollectionFormat.valueOf("{{{.}}}".toUpperCase(Locale.ROOT)){{/collectionFormat}}{{^collectionFormat}}null{{/collectionFormat}}, "{{baseName}}", {{paramName}})); + {{/isExplode}}{{/queryParams}}{{/hasQueryParams}}{{#hasHeaderParams}} {{#headerParams}} if ({{paramName}} != null) 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 9e260185f92e..a14984331eae 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 @@ -2272,6 +2272,30 @@ public void shouldProperlyExplodeRestTemplateQueryParameters_issue907() { + " objectParam.getSomeInteger()));"); } + @Test + public void shouldProperlyExplodeWebClientQueryParameters() { + final Map files = generateFromContract( + "src/test/resources/3_0/java/explode-query-parameter.yaml", + JavaClientCodegen.WEBCLIENT + ); + + JavaFileAssert.assertThat(files.get("DefaultApi.java")) + .printFileContent() + .assertMethod("searchRequestCreation") + .bodyContainsLines( + "queryParams.putAll(apiClient.parameterToMultiValueMap(null, \"regular-param\"," + + " regularParam));") + .bodyContainsLines( + "queryParams.putAll(apiClient.parameterToMultiValueMap(null, \"someString\"," + + " objectParam.getSomeString()));") + .bodyContainsLines( + "queryParams.putAll(apiClient.parameterToMultiValueMap(null, \"someBoolean\"," + + " objectParam.getSomeBoolean()));") + .bodyContainsLines( + "queryParams.putAll(apiClient.parameterToMultiValueMap(null, \"someInteger\"," + + " objectParam.getSomeInteger()));"); + } + private static Map generateFromContract(final String pathToSpecification, final String library) { return generateFromContract(pathToSpecification, library, new HashMap<>()); } diff --git a/samples/client/petstore/java/webclient-jakarta/src/main/java/org/openapitools/client/api/FakeApi.java b/samples/client/petstore/java/webclient-jakarta/src/main/java/org/openapitools/client/api/FakeApi.java index 4eda70f5a0c2..3a38dbe72869 100644 --- a/samples/client/petstore/java/webclient-jakarta/src/main/java/org/openapitools/client/api/FakeApi.java +++ b/samples/client/petstore/java/webclient-jakarta/src/main/java/org/openapitools/client/api/FakeApi.java @@ -212,6 +212,7 @@ private ResponseSpec fakeHttpSignatureTestRequestCreation(Pet pet, String query1 final MultiValueMap formParams = new LinkedMultiValueMap(); queryParams.putAll(apiClient.parameterToMultiValueMap(null, "query_1", query1)); + if (header1 != null) headerParams.add("header_1", apiClient.parameterToString(header1)); @@ -788,7 +789,7 @@ private ResponseSpec testBodyWithQueryParamsRequestCreation(String query, User u final MultiValueMap formParams = new LinkedMultiValueMap(); queryParams.putAll(apiClient.parameterToMultiValueMap(null, "query", query)); - + final String[] localVarAccepts = { }; final List localVarAccept = apiClient.selectHeaderAccept(localVarAccepts); final String[] localVarContentTypes = { @@ -1111,6 +1112,7 @@ private ResponseSpec testEnumParametersRequestCreation(List enumHeaderSt queryParams.putAll(apiClient.parameterToMultiValueMap(null, "enum_query_integer", enumQueryInteger)); queryParams.putAll(apiClient.parameterToMultiValueMap(null, "enum_query_double", enumQueryDouble)); queryParams.putAll(apiClient.parameterToMultiValueMap(ApiClient.CollectionFormat.valueOf("multi".toUpperCase(Locale.ROOT)), "enum_query_model_array", enumQueryModelArray)); + if (enumHeaderStringArray != null) headerParams.add("enum_header_string_array", apiClient.parameterToString(enumHeaderStringArray)); @@ -1234,6 +1236,7 @@ private ResponseSpec testGroupParametersRequestCreation(Integer requiredStringGr queryParams.putAll(apiClient.parameterToMultiValueMap(null, "required_int64_group", requiredInt64Group)); queryParams.putAll(apiClient.parameterToMultiValueMap(null, "string_group", stringGroup)); queryParams.putAll(apiClient.parameterToMultiValueMap(null, "int64_group", int64Group)); + if (requiredBooleanGroup != null) headerParams.add("required_boolean_group", apiClient.parameterToString(requiredBooleanGroup)); @@ -1643,7 +1646,7 @@ private ResponseSpec testQueryParameterCollectionFormatRequestCreation(List localVarAccept = apiClient.selectHeaderAccept(localVarAccepts); final String[] localVarContentTypes = { }; diff --git a/samples/client/petstore/java/webclient-jakarta/src/main/java/org/openapitools/client/api/PetApi.java b/samples/client/petstore/java/webclient-jakarta/src/main/java/org/openapitools/client/api/PetApi.java index f9ae47490280..45389ee255c1 100644 --- a/samples/client/petstore/java/webclient-jakarta/src/main/java/org/openapitools/client/api/PetApi.java +++ b/samples/client/petstore/java/webclient-jakarta/src/main/java/org/openapitools/client/api/PetApi.java @@ -147,6 +147,7 @@ private ResponseSpec deletePetRequestCreation(Long petId, String apiKey) throws final MultiValueMap cookieParams = new LinkedMultiValueMap(); final MultiValueMap formParams = new LinkedMultiValueMap(); + if (apiKey != null) headerParams.add("api_key", apiClient.parameterToString(apiKey)); final String[] localVarAccepts = { }; @@ -225,7 +226,7 @@ private ResponseSpec findPetsByStatusRequestCreation(List status) throws final MultiValueMap formParams = new LinkedMultiValueMap(); queryParams.putAll(apiClient.parameterToMultiValueMap(ApiClient.CollectionFormat.valueOf("csv".toUpperCase(Locale.ROOT)), "status", status)); - + final String[] localVarAccepts = { "application/xml", "application/json" }; @@ -305,7 +306,7 @@ private ResponseSpec findPetsByTagsRequestCreation(Set tags) throws WebC final MultiValueMap formParams = new LinkedMultiValueMap(); queryParams.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/webclient-jakarta/src/main/java/org/openapitools/client/api/UserApi.java b/samples/client/petstore/java/webclient-jakarta/src/main/java/org/openapitools/client/api/UserApi.java index ebb82cc15b9a..68346059e913 100644 --- a/samples/client/petstore/java/webclient-jakarta/src/main/java/org/openapitools/client/api/UserApi.java +++ b/samples/client/petstore/java/webclient-jakarta/src/main/java/org/openapitools/client/api/UserApi.java @@ -439,7 +439,7 @@ private ResponseSpec loginUserRequestCreation(String username, String password) queryParams.putAll(apiClient.parameterToMultiValueMap(null, "username", username)); queryParams.putAll(apiClient.parameterToMultiValueMap(null, "password", password)); - + final String[] localVarAccepts = { "application/xml", "application/json" }; diff --git a/samples/client/petstore/java/webclient-swagger2/src/main/java/org/openapitools/client/api/FakeApi.java b/samples/client/petstore/java/webclient-swagger2/src/main/java/org/openapitools/client/api/FakeApi.java index a7602fbaff3d..2170874323d3 100644 --- a/samples/client/petstore/java/webclient-swagger2/src/main/java/org/openapitools/client/api/FakeApi.java +++ b/samples/client/petstore/java/webclient-swagger2/src/main/java/org/openapitools/client/api/FakeApi.java @@ -212,6 +212,7 @@ private ResponseSpec fakeHttpSignatureTestRequestCreation(Pet pet, String query1 final MultiValueMap formParams = new LinkedMultiValueMap(); queryParams.putAll(apiClient.parameterToMultiValueMap(null, "query_1", query1)); + if (header1 != null) headerParams.add("header_1", apiClient.parameterToString(header1)); @@ -788,7 +789,7 @@ private ResponseSpec testBodyWithQueryParamsRequestCreation(String query, User u final MultiValueMap formParams = new LinkedMultiValueMap(); queryParams.putAll(apiClient.parameterToMultiValueMap(null, "query", query)); - + final String[] localVarAccepts = { }; final List localVarAccept = apiClient.selectHeaderAccept(localVarAccepts); final String[] localVarContentTypes = { @@ -1111,6 +1112,7 @@ private ResponseSpec testEnumParametersRequestCreation(List enumHeaderSt queryParams.putAll(apiClient.parameterToMultiValueMap(null, "enum_query_integer", enumQueryInteger)); queryParams.putAll(apiClient.parameterToMultiValueMap(null, "enum_query_double", enumQueryDouble)); queryParams.putAll(apiClient.parameterToMultiValueMap(ApiClient.CollectionFormat.valueOf("multi".toUpperCase(Locale.ROOT)), "enum_query_model_array", enumQueryModelArray)); + if (enumHeaderStringArray != null) headerParams.add("enum_header_string_array", apiClient.parameterToString(enumHeaderStringArray)); @@ -1234,6 +1236,7 @@ private ResponseSpec testGroupParametersRequestCreation(Integer requiredStringGr queryParams.putAll(apiClient.parameterToMultiValueMap(null, "required_int64_group", requiredInt64Group)); queryParams.putAll(apiClient.parameterToMultiValueMap(null, "string_group", stringGroup)); queryParams.putAll(apiClient.parameterToMultiValueMap(null, "int64_group", int64Group)); + if (requiredBooleanGroup != null) headerParams.add("required_boolean_group", apiClient.parameterToString(requiredBooleanGroup)); @@ -1643,7 +1646,7 @@ private ResponseSpec testQueryParameterCollectionFormatRequestCreation(List localVarAccept = apiClient.selectHeaderAccept(localVarAccepts); final String[] localVarContentTypes = { }; diff --git a/samples/client/petstore/java/webclient-swagger2/src/main/java/org/openapitools/client/api/PetApi.java b/samples/client/petstore/java/webclient-swagger2/src/main/java/org/openapitools/client/api/PetApi.java index c5588dff9700..9be0a49522a3 100644 --- a/samples/client/petstore/java/webclient-swagger2/src/main/java/org/openapitools/client/api/PetApi.java +++ b/samples/client/petstore/java/webclient-swagger2/src/main/java/org/openapitools/client/api/PetApi.java @@ -147,6 +147,7 @@ private ResponseSpec deletePetRequestCreation(Long petId, String apiKey) throws final MultiValueMap cookieParams = new LinkedMultiValueMap(); final MultiValueMap formParams = new LinkedMultiValueMap(); + if (apiKey != null) headerParams.add("api_key", apiClient.parameterToString(apiKey)); final String[] localVarAccepts = { }; @@ -225,7 +226,7 @@ private ResponseSpec findPetsByStatusRequestCreation(List status) throws final MultiValueMap formParams = new LinkedMultiValueMap(); queryParams.putAll(apiClient.parameterToMultiValueMap(ApiClient.CollectionFormat.valueOf("csv".toUpperCase(Locale.ROOT)), "status", status)); - + final String[] localVarAccepts = { "application/xml", "application/json" }; @@ -305,7 +306,7 @@ private ResponseSpec findPetsByTagsRequestCreation(Set tags) throws WebC final MultiValueMap formParams = new LinkedMultiValueMap(); queryParams.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/webclient-swagger2/src/main/java/org/openapitools/client/api/UserApi.java b/samples/client/petstore/java/webclient-swagger2/src/main/java/org/openapitools/client/api/UserApi.java index ad76ef2ea733..dab56cb7a2c0 100644 --- a/samples/client/petstore/java/webclient-swagger2/src/main/java/org/openapitools/client/api/UserApi.java +++ b/samples/client/petstore/java/webclient-swagger2/src/main/java/org/openapitools/client/api/UserApi.java @@ -439,7 +439,7 @@ private ResponseSpec loginUserRequestCreation(String username, String password) queryParams.putAll(apiClient.parameterToMultiValueMap(null, "username", username)); queryParams.putAll(apiClient.parameterToMultiValueMap(null, "password", password)); - + final String[] localVarAccepts = { "application/xml", "application/json" }; diff --git a/samples/client/petstore/java/webclient/src/main/java/org/openapitools/client/api/FakeApi.java b/samples/client/petstore/java/webclient/src/main/java/org/openapitools/client/api/FakeApi.java index a7602fbaff3d..2170874323d3 100644 --- a/samples/client/petstore/java/webclient/src/main/java/org/openapitools/client/api/FakeApi.java +++ b/samples/client/petstore/java/webclient/src/main/java/org/openapitools/client/api/FakeApi.java @@ -212,6 +212,7 @@ private ResponseSpec fakeHttpSignatureTestRequestCreation(Pet pet, String query1 final MultiValueMap formParams = new LinkedMultiValueMap(); queryParams.putAll(apiClient.parameterToMultiValueMap(null, "query_1", query1)); + if (header1 != null) headerParams.add("header_1", apiClient.parameterToString(header1)); @@ -788,7 +789,7 @@ private ResponseSpec testBodyWithQueryParamsRequestCreation(String query, User u final MultiValueMap formParams = new LinkedMultiValueMap(); queryParams.putAll(apiClient.parameterToMultiValueMap(null, "query", query)); - + final String[] localVarAccepts = { }; final List localVarAccept = apiClient.selectHeaderAccept(localVarAccepts); final String[] localVarContentTypes = { @@ -1111,6 +1112,7 @@ private ResponseSpec testEnumParametersRequestCreation(List enumHeaderSt queryParams.putAll(apiClient.parameterToMultiValueMap(null, "enum_query_integer", enumQueryInteger)); queryParams.putAll(apiClient.parameterToMultiValueMap(null, "enum_query_double", enumQueryDouble)); queryParams.putAll(apiClient.parameterToMultiValueMap(ApiClient.CollectionFormat.valueOf("multi".toUpperCase(Locale.ROOT)), "enum_query_model_array", enumQueryModelArray)); + if (enumHeaderStringArray != null) headerParams.add("enum_header_string_array", apiClient.parameterToString(enumHeaderStringArray)); @@ -1234,6 +1236,7 @@ private ResponseSpec testGroupParametersRequestCreation(Integer requiredStringGr queryParams.putAll(apiClient.parameterToMultiValueMap(null, "required_int64_group", requiredInt64Group)); queryParams.putAll(apiClient.parameterToMultiValueMap(null, "string_group", stringGroup)); queryParams.putAll(apiClient.parameterToMultiValueMap(null, "int64_group", int64Group)); + if (requiredBooleanGroup != null) headerParams.add("required_boolean_group", apiClient.parameterToString(requiredBooleanGroup)); @@ -1643,7 +1646,7 @@ private ResponseSpec testQueryParameterCollectionFormatRequestCreation(List localVarAccept = apiClient.selectHeaderAccept(localVarAccepts); final String[] localVarContentTypes = { }; diff --git a/samples/client/petstore/java/webclient/src/main/java/org/openapitools/client/api/PetApi.java b/samples/client/petstore/java/webclient/src/main/java/org/openapitools/client/api/PetApi.java index c5588dff9700..9be0a49522a3 100644 --- a/samples/client/petstore/java/webclient/src/main/java/org/openapitools/client/api/PetApi.java +++ b/samples/client/petstore/java/webclient/src/main/java/org/openapitools/client/api/PetApi.java @@ -147,6 +147,7 @@ private ResponseSpec deletePetRequestCreation(Long petId, String apiKey) throws final MultiValueMap cookieParams = new LinkedMultiValueMap(); final MultiValueMap formParams = new LinkedMultiValueMap(); + if (apiKey != null) headerParams.add("api_key", apiClient.parameterToString(apiKey)); final String[] localVarAccepts = { }; @@ -225,7 +226,7 @@ private ResponseSpec findPetsByStatusRequestCreation(List status) throws final MultiValueMap formParams = new LinkedMultiValueMap(); queryParams.putAll(apiClient.parameterToMultiValueMap(ApiClient.CollectionFormat.valueOf("csv".toUpperCase(Locale.ROOT)), "status", status)); - + final String[] localVarAccepts = { "application/xml", "application/json" }; @@ -305,7 +306,7 @@ private ResponseSpec findPetsByTagsRequestCreation(Set tags) throws WebC final MultiValueMap formParams = new LinkedMultiValueMap(); queryParams.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/webclient/src/main/java/org/openapitools/client/api/UserApi.java b/samples/client/petstore/java/webclient/src/main/java/org/openapitools/client/api/UserApi.java index ad76ef2ea733..dab56cb7a2c0 100644 --- a/samples/client/petstore/java/webclient/src/main/java/org/openapitools/client/api/UserApi.java +++ b/samples/client/petstore/java/webclient/src/main/java/org/openapitools/client/api/UserApi.java @@ -439,7 +439,7 @@ private ResponseSpec loginUserRequestCreation(String username, String password) queryParams.putAll(apiClient.parameterToMultiValueMap(null, "username", username)); queryParams.putAll(apiClient.parameterToMultiValueMap(null, "password", password)); - + final String[] localVarAccepts = { "application/xml", "application/json" };