From 9fe2f4d3efdd182c9a99e87e1b683a334fabd66a Mon Sep 17 00:00:00 2001 From: William Cheng Date: Tue, 22 Oct 2019 15:14:14 +0800 Subject: [PATCH] [Kotlin][client] Add nullable query parameter support (#4197) * add nullable query parameter support * remove redundant bracket --- .../openapitools/codegen/DefaultCodegen.java | 23 +++++++++---------- .../main/resources/kotlin-client/api.mustache | 16 +++++++++++-- 2 files changed, 25 insertions(+), 14 deletions(-) diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java index eb3abe73b00..a6bcbdcefc1 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java @@ -1893,7 +1893,7 @@ public class DefaultCodegen implements CodegenConfig { } } - if(composed.getRequired() != null) { + if (composed.getRequired() != null) { required.addAll(composed.getRequired()); } addVars(m, unaliasPropertySchema(properties), required, unaliasPropertySchema(allProperties), allRequired); @@ -2002,7 +2002,7 @@ public class DefaultCodegen implements CodegenConfig { addProperties(properties, required, component); } - if(schema.getRequired() != null) { + if (schema.getRequired() != null) { required.addAll(schema.getRequired()); } @@ -2508,15 +2508,15 @@ public class DefaultCodegen implements CodegenConfig { /** * Set op's returnBaseType, returnType, examples etc. * - * @param operation endpoint Operation - * @param schemas a map of the schemas in the openapi spec - * @param op endpoint CodegenOperation + * @param operation endpoint Operation + * @param schemas a map of the schemas in the openapi spec + * @param op endpoint CodegenOperation * @param methodResponse the default ApiResponse for the endpoint */ protected void handleMethodResponse(Operation operation, - Map schemas, - CodegenOperation op, - ApiResponse methodResponse) { + Map schemas, + CodegenOperation op, + ApiResponse methodResponse) { Schema responseSchema = ModelUtils.unaliasSchema(this.openAPI, ModelUtils.getSchemaFromResponse(methodResponse)); if (responseSchema != null) { @@ -4101,7 +4101,7 @@ public class DefaultCodegen implements CodegenConfig { * * @param name string to be sanitize * @param removeCharRegEx a regex containing all char that will be removed - * @param exceptionList a list of matches which should not be sanitized (i.e expections) + * @param exceptionList a list of matches which should not be sanitized (i.e expections) * @return sanitized string */ @SuppressWarnings("static-method") @@ -4529,8 +4529,8 @@ public class DefaultCodegen implements CodegenConfig { for (String consume : consumesInfo) { if (consume != null && - (consume.toLowerCase(Locale.ROOT).startsWith("application/x-www-form-urlencoded") || - consume.toLowerCase(Locale.ROOT).startsWith("multipart"))) { + (consume.toLowerCase(Locale.ROOT).startsWith("application/x-www-form-urlencoded") || + consume.toLowerCase(Locale.ROOT).startsWith("multipart"))) { return true; } } @@ -5022,7 +5022,6 @@ public class DefaultCodegen implements CodegenConfig { codegenParameter.pattern = codegenProperty.pattern; - if (codegenProperty.complexType != null) { imports.add(codegenProperty.complexType); } diff --git a/modules/openapi-generator/src/main/resources/kotlin-client/api.mustache b/modules/openapi-generator/src/main/resources/kotlin-client/api.mustache index c79a02968b2..477cf9fface 100644 --- a/modules/openapi-generator/src/main/resources/kotlin-client/api.mustache +++ b/modules/openapi-generator/src/main/resources/kotlin-client/api.mustache @@ -33,9 +33,21 @@ import {{packageName}}.infrastructure.toMultiValue {{/hasQueryParams}}{{#hasQueryParams}}mutableMapOf>() .apply { {{#queryParams}} - {{^required}}if ({{paramName}} != null) { + {{^required}} + if ({{paramName}} != null) { put("{{paramName}}", {{#isContainer}}toMultiValue({{paramName}}.toList(), "{{collectionFormat}}"){{/isContainer}}{{^isContainer}}listOf({{paramName}}.toString()){{/isContainer}}) - }{{/required}}{{#required}}put("{{paramName}}", {{#isContainer}}toMultiValue({{paramName}}.toList(), "{{collectionFormat}}"){{/isContainer}}{{^isContainer}}listOf({{paramName}}.toString()){{/isContainer}}){{/required}} + } + {{/required}} + {{#required}} + {{#isNullable}} + if ({{paramName}} != null) { + put("{{paramName}}", {{#isContainer}}toMultiValue({{paramName}}.toList(), "{{collectionFormat}}"){{/isContainer}}{{^isContainer}}listOf({{paramName}}.toString()){{/isContainer}}) + } + {{/isNullable}} + {{^isNullable}} + put("{{paramName}}", {{#isContainer}}toMultiValue({{paramName}}.toList(), "{{collectionFormat}}"){{/isContainer}}{{^isContainer}}listOf({{paramName}}.toString()){{/isContainer}}) + {{/isNullable}} + {{/required}} {{/queryParams}} } {{/hasQueryParams}}