Skip to content

Commit 9fe2f4d

Browse files
authored
[Kotlin][client] Add nullable query parameter support (#4197)
* add nullable query parameter support * remove redundant bracket
1 parent 5d7bb17 commit 9fe2f4d

File tree

2 files changed

+25
-14
lines changed

2 files changed

+25
-14
lines changed

modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1893,7 +1893,7 @@ public CodegenModel fromModel(String name, Schema schema) {
18931893
}
18941894
}
18951895

1896-
if(composed.getRequired() != null) {
1896+
if (composed.getRequired() != null) {
18971897
required.addAll(composed.getRequired());
18981898
}
18991899
addVars(m, unaliasPropertySchema(properties), required, unaliasPropertySchema(allProperties), allRequired);
@@ -2002,7 +2002,7 @@ protected void addProperties(Map<String, Schema> properties, List<String> requir
20022002
addProperties(properties, required, component);
20032003
}
20042004

2005-
if(schema.getRequired() != null) {
2005+
if (schema.getRequired() != null) {
20062006
required.addAll(schema.getRequired());
20072007
}
20082008

@@ -2508,15 +2508,15 @@ protected ApiResponse findMethodResponse(ApiResponses responses) {
25082508
/**
25092509
* Set op's returnBaseType, returnType, examples etc.
25102510
*
2511-
* @param operation endpoint Operation
2512-
* @param schemas a map of the schemas in the openapi spec
2513-
* @param op endpoint CodegenOperation
2511+
* @param operation endpoint Operation
2512+
* @param schemas a map of the schemas in the openapi spec
2513+
* @param op endpoint CodegenOperation
25142514
* @param methodResponse the default ApiResponse for the endpoint
25152515
*/
25162516
protected void handleMethodResponse(Operation operation,
2517-
Map<String, Schema> schemas,
2518-
CodegenOperation op,
2519-
ApiResponse methodResponse) {
2517+
Map<String, Schema> schemas,
2518+
CodegenOperation op,
2519+
ApiResponse methodResponse) {
25202520
Schema responseSchema = ModelUtils.unaliasSchema(this.openAPI, ModelUtils.getSchemaFromResponse(methodResponse));
25212521

25222522
if (responseSchema != null) {
@@ -4101,7 +4101,7 @@ public String sanitizeName(String name, String removeCharRegEx) {
41014101
*
41024102
* @param name string to be sanitize
41034103
* @param removeCharRegEx a regex containing all char that will be removed
4104-
* @param exceptionList a list of matches which should not be sanitized (i.e expections)
4104+
* @param exceptionList a list of matches which should not be sanitized (i.e expections)
41054105
* @return sanitized string
41064106
*/
41074107
@SuppressWarnings("static-method")
@@ -4529,8 +4529,8 @@ public boolean hasFormParameter(OpenAPI openAPI, Operation operation) {
45294529

45304530
for (String consume : consumesInfo) {
45314531
if (consume != null &&
4532-
(consume.toLowerCase(Locale.ROOT).startsWith("application/x-www-form-urlencoded") ||
4533-
consume.toLowerCase(Locale.ROOT).startsWith("multipart"))) {
4532+
(consume.toLowerCase(Locale.ROOT).startsWith("application/x-www-form-urlencoded") ||
4533+
consume.toLowerCase(Locale.ROOT).startsWith("multipart"))) {
45344534
return true;
45354535
}
45364536
}
@@ -5022,7 +5022,6 @@ public CodegenParameter fromRequestBody(RequestBody body, Set<String> imports, S
50225022
codegenParameter.pattern = codegenProperty.pattern;
50235023

50245024

5025-
50265025
if (codegenProperty.complexType != null) {
50275026
imports.add(codegenProperty.complexType);
50285027
}

modules/openapi-generator/src/main/resources/kotlin-client/api.mustache

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,21 @@ import {{packageName}}.infrastructure.toMultiValue
3333
{{/hasQueryParams}}{{#hasQueryParams}}mutableMapOf<kotlin.String, List<kotlin.String>>()
3434
.apply {
3535
{{#queryParams}}
36-
{{^required}}if ({{paramName}} != null) {
36+
{{^required}}
37+
if ({{paramName}} != null) {
3738
put("{{paramName}}", {{#isContainer}}toMultiValue({{paramName}}.toList(), "{{collectionFormat}}"){{/isContainer}}{{^isContainer}}listOf({{paramName}}.toString()){{/isContainer}})
38-
}{{/required}}{{#required}}put("{{paramName}}", {{#isContainer}}toMultiValue({{paramName}}.toList(), "{{collectionFormat}}"){{/isContainer}}{{^isContainer}}listOf({{paramName}}.toString()){{/isContainer}}){{/required}}
39+
}
40+
{{/required}}
41+
{{#required}}
42+
{{#isNullable}}
43+
if ({{paramName}} != null) {
44+
put("{{paramName}}", {{#isContainer}}toMultiValue({{paramName}}.toList(), "{{collectionFormat}}"){{/isContainer}}{{^isContainer}}listOf({{paramName}}.toString()){{/isContainer}})
45+
}
46+
{{/isNullable}}
47+
{{^isNullable}}
48+
put("{{paramName}}", {{#isContainer}}toMultiValue({{paramName}}.toList(), "{{collectionFormat}}"){{/isContainer}}{{^isContainer}}listOf({{paramName}}.toString()){{/isContainer}})
49+
{{/isNullable}}
50+
{{/required}}
3951
{{/queryParams}}
4052
}
4153
{{/hasQueryParams}}

0 commit comments

Comments
 (0)