Skip to content

Commit 38ebf0b

Browse files
authored
[Java][OkHttp-Gson] fix: free form query parameters for okhttp-gson (#19226)
1 parent 05c1093 commit 38ebf0b

20 files changed

Lines changed: 561 additions & 1 deletion

File tree

modules/openapi-generator/src/main/resources/Java/libraries/okhttp-gson/ApiClient.mustache

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -987,6 +987,30 @@ public class ApiClient {
987987
}
988988
{{/dynamicOperations}}
989989
990+
/**
991+
* Formats the specified free-form query parameters to a list of {@code Pair} objects.
992+
*
993+
* @param value The free-form query parameters.
994+
* @return A list of {@code Pair} objects.
995+
*/
996+
public List<Pair> freeFormParameterToPairs(Object value) {
997+
List<Pair> params = new ArrayList<>();
998+
999+
// preconditions
1000+
if (value == null || !(value instanceof Map )) {
1001+
return params;
1002+
}
1003+
1004+
final Map<String, Object> valuesMap = (Map<String, Object>) value;
1005+
1006+
for (Map.Entry<String, Object> entry : valuesMap.entrySet()) {
1007+
params.add(new Pair(entry.getKey(), parameterToString(entry.getValue())));
1008+
}
1009+
1010+
return params;
1011+
}
1012+
1013+
9901014
/**
9911015
* Formats the specified collection path parameter to a string value.
9921016
*

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ public class {{classname}} {
169169
{{^dynamicOperations}}
170170
{{#queryParams}}
171171
if ({{paramName}} != null) {
172-
{{#collectionFormat}}localVarCollectionQueryParams.addAll(localVarApiClient.parameterToPairs("{{{.}}}", {{/collectionFormat}}{{^collectionFormat}}localVarQueryParams.addAll(localVarApiClient.parameterToPair({{/collectionFormat}}"{{baseName}}", {{paramName}}));
172+
{{#isFreeFormObject}}localVarQueryParams.addAll(localVarApiClient.freeFormParameterToPairs({{paramName}}));{{/isFreeFormObject}}{{^isFreeFormObject}}{{#collectionFormat}}localVarCollectionQueryParams.addAll(localVarApiClient.parameterToPairs("{{{.}}}", {{/collectionFormat}}{{^collectionFormat}}localVarQueryParams.addAll(localVarApiClient.parameterToPair({{/collectionFormat}}"{{baseName}}", {{paramName}}));{{/isFreeFormObject}}
173173
}
174174

175175
{{/queryParams}}

modules/openapi-generator/src/test/resources/3_0/java/petstore-with-fake-endpoints-models-for-testing-okhttp-gson.yaml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1092,6 +1092,21 @@ paths:
10921092
application/json:
10931093
schema:
10941094
$ref: '#/components/schemas/FreeFormObjectTestClass'
1095+
/fake/free-form-query-parameters:
1096+
get:
1097+
parameters:
1098+
- in: "query"
1099+
name: "fixed"
1100+
schema:
1101+
type: "string"
1102+
- in: "query"
1103+
name: "free-form"
1104+
schema:
1105+
type: "object"
1106+
style: "form"
1107+
responses:
1108+
200:
1109+
description: OK
10951110
/fake/test-query-parameters:
10961111
put:
10971112
tags:

samples/client/echo_api/java/okhttp-gson-user-defined-templates/src/main/java/org/openapitools/client/ApiClient.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -829,6 +829,30 @@ public List<Pair> parameterToPairs(String collectionFormat, String name, Collect
829829
return params;
830830
}
831831

832+
/**
833+
* Formats the specified free-form query parameters to a list of {@code Pair} objects.
834+
*
835+
* @param value The free-form query parameters.
836+
* @return A list of {@code Pair} objects.
837+
*/
838+
public List<Pair> freeFormParameterToPairs(Object value) {
839+
List<Pair> params = new ArrayList<>();
840+
841+
// preconditions
842+
if (value == null || !(value instanceof Map )) {
843+
return params;
844+
}
845+
846+
final Map<String, Object> valuesMap = (Map<String, Object>) value;
847+
848+
for (Map.Entry<String, Object> entry : valuesMap.entrySet()) {
849+
params.add(new Pair(entry.getKey(), parameterToString(entry.getValue())));
850+
}
851+
852+
return params;
853+
}
854+
855+
832856
/**
833857
* Formats the specified collection path parameter to a string value.
834858
*

samples/client/echo_api/java/okhttp-gson/src/main/java/org/openapitools/client/ApiClient.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -759,6 +759,30 @@ public List<Pair> parameterToPairs(String collectionFormat, String name, Collect
759759
return params;
760760
}
761761

762+
/**
763+
* Formats the specified free-form query parameters to a list of {@code Pair} objects.
764+
*
765+
* @param value The free-form query parameters.
766+
* @return A list of {@code Pair} objects.
767+
*/
768+
public List<Pair> freeFormParameterToPairs(Object value) {
769+
List<Pair> params = new ArrayList<>();
770+
771+
// preconditions
772+
if (value == null || !(value instanceof Map )) {
773+
return params;
774+
}
775+
776+
final Map<String, Object> valuesMap = (Map<String, Object>) value;
777+
778+
for (Map.Entry<String, Object> entry : valuesMap.entrySet()) {
779+
params.add(new Pair(entry.getKey(), parameterToString(entry.getValue())));
780+
}
781+
782+
return params;
783+
}
784+
785+
762786
/**
763787
* Formats the specified collection path parameter to a string value.
764788
*

samples/client/others/java/okhttp-gson-oneOf/src/main/java/org/openapitools/client/ApiClient.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -733,6 +733,30 @@ public List<Pair> parameterToPairs(String collectionFormat, String name, Collect
733733
return params;
734734
}
735735

736+
/**
737+
* Formats the specified free-form query parameters to a list of {@code Pair} objects.
738+
*
739+
* @param value The free-form query parameters.
740+
* @return A list of {@code Pair} objects.
741+
*/
742+
public List<Pair> freeFormParameterToPairs(Object value) {
743+
List<Pair> params = new ArrayList<>();
744+
745+
// preconditions
746+
if (value == null || !(value instanceof Map )) {
747+
return params;
748+
}
749+
750+
final Map<String, Object> valuesMap = (Map<String, Object>) value;
751+
752+
for (Map.Entry<String, Object> entry : valuesMap.entrySet()) {
753+
params.add(new Pair(entry.getKey(), parameterToString(entry.getValue())));
754+
}
755+
756+
return params;
757+
}
758+
759+
736760
/**
737761
* Formats the specified collection path parameter to a string value.
738762
*

samples/client/others/java/okhttp-gson-streaming/src/main/java/org/openapitools/client/ApiClient.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -733,6 +733,30 @@ public List<Pair> parameterToPairs(String collectionFormat, String name, Collect
733733
return params;
734734
}
735735

736+
/**
737+
* Formats the specified free-form query parameters to a list of {@code Pair} objects.
738+
*
739+
* @param value The free-form query parameters.
740+
* @return A list of {@code Pair} objects.
741+
*/
742+
public List<Pair> freeFormParameterToPairs(Object value) {
743+
List<Pair> params = new ArrayList<>();
744+
745+
// preconditions
746+
if (value == null || !(value instanceof Map )) {
747+
return params;
748+
}
749+
750+
final Map<String, Object> valuesMap = (Map<String, Object>) value;
751+
752+
for (Map.Entry<String, Object> entry : valuesMap.entrySet()) {
753+
params.add(new Pair(entry.getKey(), parameterToString(entry.getValue())));
754+
}
755+
756+
return params;
757+
}
758+
759+
736760
/**
737761
* Formats the specified collection path parameter to a string value.
738762
*

samples/client/petstore/java/okhttp-gson-3.1/src/main/java/org/openapitools/client/ApiClient.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -829,6 +829,30 @@ public List<Pair> parameterToPairs(String collectionFormat, String name, Collect
829829
return params;
830830
}
831831

832+
/**
833+
* Formats the specified free-form query parameters to a list of {@code Pair} objects.
834+
*
835+
* @param value The free-form query parameters.
836+
* @return A list of {@code Pair} objects.
837+
*/
838+
public List<Pair> freeFormParameterToPairs(Object value) {
839+
List<Pair> params = new ArrayList<>();
840+
841+
// preconditions
842+
if (value == null || !(value instanceof Map )) {
843+
return params;
844+
}
845+
846+
final Map<String, Object> valuesMap = (Map<String, Object>) value;
847+
848+
for (Map.Entry<String, Object> entry : valuesMap.entrySet()) {
849+
params.add(new Pair(entry.getKey(), parameterToString(entry.getValue())));
850+
}
851+
852+
return params;
853+
}
854+
855+
832856
/**
833857
* Formats the specified collection path parameter to a string value.
834858
*

samples/client/petstore/java/okhttp-gson-awsv4signature/src/main/java/org/openapitools/client/ApiClient.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -849,6 +849,30 @@ public List<Pair> parameterToPairs(String collectionFormat, String name, Collect
849849
return params;
850850
}
851851

852+
/**
853+
* Formats the specified free-form query parameters to a list of {@code Pair} objects.
854+
*
855+
* @param value The free-form query parameters.
856+
* @return A list of {@code Pair} objects.
857+
*/
858+
public List<Pair> freeFormParameterToPairs(Object value) {
859+
List<Pair> params = new ArrayList<>();
860+
861+
// preconditions
862+
if (value == null || !(value instanceof Map )) {
863+
return params;
864+
}
865+
866+
final Map<String, Object> valuesMap = (Map<String, Object>) value;
867+
868+
for (Map.Entry<String, Object> entry : valuesMap.entrySet()) {
869+
params.add(new Pair(entry.getKey(), parameterToString(entry.getValue())));
870+
}
871+
872+
return params;
873+
}
874+
875+
852876
/**
853877
* Formats the specified collection path parameter to a string value.
854878
*

samples/client/petstore/java/okhttp-gson-dynamicOperations/src/main/java/org/openapitools/client/ApiClient.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -834,6 +834,30 @@ public List<Pair> parameterToPairs(Parameter param, Collection value) {
834834
return params;
835835
}
836836

837+
/**
838+
* Formats the specified free-form query parameters to a list of {@code Pair} objects.
839+
*
840+
* @param value The free-form query parameters.
841+
* @return A list of {@code Pair} objects.
842+
*/
843+
public List<Pair> freeFormParameterToPairs(Object value) {
844+
List<Pair> params = new ArrayList<>();
845+
846+
// preconditions
847+
if (value == null || !(value instanceof Map )) {
848+
return params;
849+
}
850+
851+
final Map<String, Object> valuesMap = (Map<String, Object>) value;
852+
853+
for (Map.Entry<String, Object> entry : valuesMap.entrySet()) {
854+
params.add(new Pair(entry.getKey(), parameterToString(entry.getValue())));
855+
}
856+
857+
return params;
858+
}
859+
860+
837861
/**
838862
* Formats the specified collection path parameter to a string value.
839863
*

0 commit comments

Comments
 (0)