diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/KotlinClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/KotlinClientCodegen.java index a0a927780581..0543ff391ebf 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/KotlinClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/KotlinClientCodegen.java @@ -19,25 +19,17 @@ import org.openapitools.codegen.CliOption; import org.openapitools.codegen.CodegenConstants; -import org.openapitools.codegen.CodegenModel; import org.openapitools.codegen.CodegenOperation; -import org.openapitools.codegen.CodegenProperty; import org.openapitools.codegen.CodegenType; import org.openapitools.codegen.SupportingFile; import java.io.File; import java.util.HashMap; -import java.util.HashSet; -import java.util.Iterator; import java.util.List; import java.util.Map; -import java.util.Set; -import java.util.regex.Pattern; public class KotlinClientCodegen extends AbstractKotlinCodegen { - protected static final String VENDOR_EXTENSION_ESCAPED_NAME = "x-escapedName"; - protected static final String JVM = "jvm"; protected static final String MULTIPLATFORM = "multiplatform"; @@ -47,14 +39,6 @@ public class KotlinClientCodegen extends AbstractKotlinCodegen { protected String dateLibrary = DateLibrary.JAVA8.value; protected String collectionType = CollectionType.ARRAY.value; - // https://kotlinlang.org/docs/reference/grammar.html#Identifier - protected static final Pattern IDENTIFIER_PATTERN = - Pattern.compile("[\\p{Ll}\\p{Lm}\\p{Lo}\\p{Lt}\\p{Lu}\\p{Nl}_][\\p{Ll}\\p{Lm}\\p{Lo}\\p{Lt}\\p{Lu}\\p{Nl}\\p{Nd}_]*"); - - // https://kotlinlang.org/docs/reference/grammar.html#Identifier - protected static final String IDENTIFIER_REPLACEMENTS = - "[.;:/\\[\\]<>]"; - public enum DateLibrary { STRING("string"), THREETENBP("threetenbp"), @@ -212,7 +196,6 @@ public void processOpts() { supportingFiles.add(new SupportingFile("infrastructure/HttpResponse.kt.mustache", infrastructureFolder, "HttpResponse.kt")); // multiplatform specific testing files - final String testFolder = (sourceFolder + File.separator + packageName + File.separator + "infrastructure").replace(".", "/"); supportingFiles.add(new SupportingFile("commonTest/coroutine.mustache", "src/commonTest/kotlin/util", "Coroutine.kt")); supportingFiles.add(new SupportingFile("iosTest/coroutine.mustache", "src/iosTest/kotlin/util", "Coroutine.kt")); supportingFiles.add(new SupportingFile("jvmTest/coroutine.mustache", "src/jvmTest/kotlin/util", "Coroutine.kt")); @@ -253,58 +236,6 @@ public void processOpts() { } } - @Override - public Map postProcessModels(Map objs) { - objs = super.postProcessModels(objs); - return postProcessModelsEscapeNames(objs); - } - - @SuppressWarnings("unchecked") - private static Map postProcessModelsEscapeNames(Map objs) { - List models = (List) objs.get("models"); - for (Object _mo : models) { - Map mo = (Map) _mo; - CodegenModel cm = (CodegenModel) mo.get("model"); - - if (cm.vars != null) { - for (CodegenProperty var : cm.vars) { - var.vendorExtensions.put(VENDOR_EXTENSION_ESCAPED_NAME, escapeIdentifier(var.name)); - } - } - if (cm.requiredVars != null) { - for (CodegenProperty var : cm.requiredVars) { - var.vendorExtensions.put(VENDOR_EXTENSION_ESCAPED_NAME, escapeIdentifier(var.name)); - } - } - if (cm.optionalVars != null) { - for (CodegenProperty var : cm.optionalVars) { - var.vendorExtensions.put(VENDOR_EXTENSION_ESCAPED_NAME, escapeIdentifier(var.name)); - } - } - } - return objs; - } - - private static String escapeIdentifier(String identifier) { - - // the kotlin grammar permits a wider set of characters in their identifiers that all target - // platforms permit (namely jvm). in order to remain compatible with target platforms, we - // initially replace all illegal target characters before escaping the identifier if required. - identifier = identifier.replaceAll(IDENTIFIER_REPLACEMENTS, "_"); - if (IDENTIFIER_PATTERN.matcher(identifier).matches()) return identifier; - return '`' + identifier + '`'; - } - - private static void removeDuplicates(List list) { - Set set = new HashSet<>(); - Iterator iterator = list.iterator(); - while (iterator.hasNext()) { - CodegenProperty item = iterator.next(); - if (set.contains(item.name)) iterator.remove(); - else set.add(item.name); - } - } - @Override @SuppressWarnings("unchecked") public Map postProcessOperationsWithModels(Map objs, List allModels) { diff --git a/modules/openapi-generator/src/main/resources/kotlin-client/data_class.mustache b/modules/openapi-generator/src/main/resources/kotlin-client/data_class.mustache index 0cece22bb0b7..abf77457445b 100644 --- a/modules/openapi-generator/src/main/resources/kotlin-client/data_class.mustache +++ b/modules/openapi-generator/src/main/resources/kotlin-client/data_class.mustache @@ -18,7 +18,7 @@ import kotlinx.serialization.internal.CommonEnumSerializer /** * {{{description}}} {{#vars}} - * @param {{{vendorExtensions.x-escapedName}}} {{{description}}} + * @param {{{name}}} {{{description}}} {{/vars}} */ {{#parcelizeModels}} diff --git a/modules/openapi-generator/src/main/resources/kotlin-client/data_class_opt_var.mustache b/modules/openapi-generator/src/main/resources/kotlin-client/data_class_opt_var.mustache index e77b8b2b2bac..5ec0214d6c9a 100644 --- a/modules/openapi-generator/src/main/resources/kotlin-client/data_class_opt_var.mustache +++ b/modules/openapi-generator/src/main/resources/kotlin-client/data_class_opt_var.mustache @@ -9,4 +9,4 @@ @SerializedName("{{name}}") {{/gson}} {{/jvm}} - {{#multiplatform}}@SerialName(value = "{{name}}") {{/multiplatform}}val {{{vendorExtensions.x-escapedName}}}: {{#isEnum}}{{#isListContainer}}{{#isList}}kotlin.collections.List{{/isList}}{{^isList}}kotlin.Array{{/isList}}<{{classname}}.{{{nameInCamelCase}}}>{{/isListContainer}}{{^isListContainer}}{{classname}}.{{{nameInCamelCase}}}{{/isListContainer}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}}? = {{#defaultvalue}}{{defaultvalue}}{{/defaultvalue}}{{^defaultvalue}}null{{/defaultvalue}} \ No newline at end of file + {{#multiplatform}}@SerialName(value = "{{baseName}}") {{/multiplatform}}val {{{name}}}: {{#isEnum}}{{#isListContainer}}{{#isList}}kotlin.collections.List{{/isList}}{{^isList}}kotlin.Array{{/isList}}<{{classname}}.{{{nameInCamelCase}}}>{{/isListContainer}}{{^isListContainer}}{{classname}}.{{{nameInCamelCase}}}{{/isListContainer}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}}? = {{#defaultvalue}}{{defaultvalue}}{{/defaultvalue}}{{^defaultvalue}}null{{/defaultvalue}} \ No newline at end of file diff --git a/modules/openapi-generator/src/main/resources/kotlin-client/data_class_req_var.mustache b/modules/openapi-generator/src/main/resources/kotlin-client/data_class_req_var.mustache index 0097d0702a9f..a60f1baf24a5 100644 --- a/modules/openapi-generator/src/main/resources/kotlin-client/data_class_req_var.mustache +++ b/modules/openapi-generator/src/main/resources/kotlin-client/data_class_req_var.mustache @@ -9,4 +9,4 @@ @SerializedName("{{name}}") {{/gson}} {{/jvm}} - {{#multiplatform}}@SerialName(value = "{{name}}") @Required {{/multiplatform}}val {{{vendorExtensions.x-escapedName}}}: {{#isEnum}}{{#isListContainer}}{{#isList}}kotlin.collections.List{{/isList}}{{^isList}}kotlin.Array{{/isList}}<{{classname}}.{{{nameInCamelCase}}}>{{/isListContainer}}{{^isListContainer}}{{classname}}.{{{nameInCamelCase}}}{{/isListContainer}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}} \ No newline at end of file + {{#multiplatform}}@SerialName(value = "{{baseName}}") @Required {{/multiplatform}}val {{{name}}}: {{#isEnum}}{{#isListContainer}}{{#isList}}kotlin.collections.List{{/isList}}{{^isList}}kotlin.Array{{/isList}}<{{classname}}.{{{nameInCamelCase}}}>{{/isListContainer}}{{^isListContainer}}{{classname}}.{{{nameInCamelCase}}}{{/isListContainer}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}} \ No newline at end of file