From c6170f178a1047af49e74436877d42eecf3f659b Mon Sep 17 00:00:00 2001 From: Lino Lendi Date: Thu, 11 Jul 2024 18:29:18 +0200 Subject: [PATCH] [Java][OkHttp-Gson] fix: free form query parameters for okhttp-gson --- .../libraries/okhttp-gson/ApiClient.mustache | 22 +++++++++++++++++++ .../Java/libraries/okhttp-gson/api.mustache | 2 +- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/modules/openapi-generator/src/main/resources/Java/libraries/okhttp-gson/ApiClient.mustache b/modules/openapi-generator/src/main/resources/Java/libraries/okhttp-gson/ApiClient.mustache index 53dddf3269cd0..72d07fb0f08a7 100644 --- a/modules/openapi-generator/src/main/resources/Java/libraries/okhttp-gson/ApiClient.mustache +++ b/modules/openapi-generator/src/main/resources/Java/libraries/okhttp-gson/ApiClient.mustache @@ -987,6 +987,28 @@ public class ApiClient { } {{/dynamicOperations}} + /** + * Formats the specified free-form query parameters to a list of {@code Pair} objects. + * + * @param value The free-form query parameters. + * @return A list of {@code Pair} objects. + */ + public List freeFormParameterToPairs(Map value) { + List params = new ArrayList<>(); + + // preconditions + if (value == null || value.isEmpty()) { + return params; + } + + for (Map.Entry entry : value.entrySet()) { + params.add(new Pair(entry.getKey(), parameterToString(entry.getValue()))); + } + + return params; + } + + /** * Formats the specified collection path parameter to a string value. * diff --git a/modules/openapi-generator/src/main/resources/Java/libraries/okhttp-gson/api.mustache b/modules/openapi-generator/src/main/resources/Java/libraries/okhttp-gson/api.mustache index ada640379e31f..bd932126ae637 100644 --- a/modules/openapi-generator/src/main/resources/Java/libraries/okhttp-gson/api.mustache +++ b/modules/openapi-generator/src/main/resources/Java/libraries/okhttp-gson/api.mustache @@ -169,7 +169,7 @@ public class {{classname}} { {{^dynamicOperations}} {{#queryParams}} if ({{paramName}} != null) { - {{#collectionFormat}}localVarCollectionQueryParams.addAll(localVarApiClient.parameterToPairs("{{{.}}}", {{/collectionFormat}}{{^collectionFormat}}localVarQueryParams.addAll(localVarApiClient.parameterToPair({{/collectionFormat}}"{{baseName}}", {{paramName}})); + {{#collectionFormat}}localVarCollectionQueryParams.addAll(localVarApiClient.parameterToPairs("{{{.}}}", {{/collectionFormat}}{{^collectionFormat}}{{#isFreeFormObject}}localVarQueryParams.addAll(localVarApiClient.freeFormParameterToPairs({{paramName}}));{{/isFreeFormObject}}{{^isFreeFormObject}}localVarQueryParams.addAll(localVarApiClient.parameterToPair("{{baseName}}", {{paramName}}));{{/isFreeFormObject}}{{/collectionFormat}} } {{/queryParams}}