From 2fdf3e313b2319a37818401f8f8acb538a2ff091 Mon Sep 17 00:00:00 2001 From: Artem Tarynin Date: Wed, 11 Oct 2023 12:12:32 +0300 Subject: [PATCH 1/5] issue-12243: fixed reserved word handling in enum kotlin --- .../kotlin/AbstractKotlinCodegen.java | 259 ++++++++++-------- .../kotlin/KotlinClientCodegenModelTest.java | 24 +- 2 files changed, 160 insertions(+), 123 deletions(-) diff --git a/src/main/java/io/swagger/codegen/v3/generators/kotlin/AbstractKotlinCodegen.java b/src/main/java/io/swagger/codegen/v3/generators/kotlin/AbstractKotlinCodegen.java index d42ae0a90c..af40cf1050 100644 --- a/src/main/java/io/swagger/codegen/v3/generators/kotlin/AbstractKotlinCodegen.java +++ b/src/main/java/io/swagger/codegen/v3/generators/kotlin/AbstractKotlinCodegen.java @@ -1,6 +1,10 @@ package io.swagger.codegen.v3.generators.kotlin; +import com.github.jknack.handlebars.Handlebars; import com.github.jknack.handlebars.helper.ConditionalHelpers; +import com.github.jknack.handlebars.helper.StringHelpers; +import com.samskivert.mustache.Escapers; +import com.samskivert.mustache.Mustache; import io.swagger.codegen.v3.CliOption; import io.swagger.codegen.v3.CodegenConstants; import io.swagger.codegen.v3.CodegenModel; @@ -13,15 +17,9 @@ import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.github.jknack.handlebars.helper.StringHelpers; -import com.github.jknack.handlebars.Handlebars; import java.io.File; -import java.util.Arrays; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Map; -import java.util.Set; +import java.util.*; public abstract class AbstractKotlinCodegen extends DefaultCodegenConfig { @@ -44,126 +42,126 @@ public AbstractKotlinCodegen() { supportsInheritance = true; languageSpecificPrimitives = new HashSet<>(Arrays.asList( - "kotlin.Any", - "kotlin.Byte", - "kotlin.Short", - "kotlin.Int", - "kotlin.Long", - "kotlin.Float", - "kotlin.Double", - "kotlin.Boolean", - "kotlin.Char", - "kotlin.String", - "kotlin.Array", - "kotlin.collections.List", - "kotlin.collections.Map", - "kotlin.collections.Set", - "kotlin.ByteArray", - "kotlin.CharArray", - "kotlin.ShortArray", - "kotlin.IntArray", - "kotlin.LongArray", - "kotlin.FloatArray", - "kotlin.DoubleArray", - "kotlin.BooleanArray" + "kotlin.Any", + "kotlin.Byte", + "kotlin.Short", + "kotlin.Int", + "kotlin.Long", + "kotlin.Float", + "kotlin.Double", + "kotlin.Boolean", + "kotlin.Char", + "kotlin.String", + "kotlin.Array", + "kotlin.collections.List", + "kotlin.collections.Map", + "kotlin.collections.Set", + "kotlin.ByteArray", + "kotlin.CharArray", + "kotlin.ShortArray", + "kotlin.IntArray", + "kotlin.LongArray", + "kotlin.FloatArray", + "kotlin.DoubleArray", + "kotlin.BooleanArray" )); // this includes hard reserved words defined by https://github.com/JetBrains/kotlin/blob/master/core/descriptors/src/org/jetbrains/kotlin/renderer/KeywordStringsGenerated.java // as well as keywords from https://kotlinlang.org/docs/reference/keyword-reference.html reservedWords = new HashSet<>(Arrays.asList( - "abstract", - "actual", - "annotation", - "as", - "break", - "case", - "catch", - "class", - "companion", - "const", - "constructor", - "continue", - "crossinline", - "data", - "delegate", - "do", - "else", - "enum", - "expect", - "external", - "false", - "final", - "finally", - "for", - "fun", - "if", - "in", - "infix", - "init", - "inline", - "inner", - "interface", - "internal", - "is", - "it", - "lateinit", - "lazy", - "noinline", - "null", - "object", - "open", - "operator", - "out", - "override", - "package", - "private", - "protected", - "public", - "reified", - "return", - "sealed", - "super", - "suspend", - "tailrec", - "this", - "throw", - "true", - "try", - "typealias", - "typeof", - "val", - "var", - "vararg", - "when", - "while" + "abstract", + "actual", + "annotation", + "as", + "break", + "case", + "catch", + "class", + "companion", + "const", + "constructor", + "continue", + "crossinline", + "data", + "delegate", + "do", + "else", + "enum", + "expect", + "external", + "false", + "final", + "finally", + "for", + "fun", + "if", + "in", + "infix", + "init", + "inline", + "inner", + "interface", + "internal", + "is", + "it", + "lateinit", + "lazy", + "noinline", + "null", + "object", + "open", + "operator", + "out", + "override", + "package", + "private", + "protected", + "public", + "reified", + "return", + "sealed", + "super", + "suspend", + "tailrec", + "this", + "throw", + "true", + "try", + "typealias", + "typeof", + "val", + "var", + "vararg", + "when", + "while" )); defaultIncludes = new HashSet<>(Arrays.asList( - "kotlin.Byte", - "kotlin.Short", - "kotlin.Int", - "kotlin.Long", - "kotlin.Float", - "kotlin.Double", - "kotlin.Boolean", - "kotlin.Char", - "kotlin.Array", - "kotlin.collections.List", - "kotlin.collections.Set", - "kotlin.collections.Map", - "kotlin.ByteArray", - "kotlin.CharArray", - "kotlin.ShortArray", - "kotlin.IntArray", - "kotlin.LongArray", - "kotlin.FloatArray", - "kotlin.DoubleArray", - "kotlin.BooleanArray" + "kotlin.Byte", + "kotlin.Short", + "kotlin.Int", + "kotlin.Long", + "kotlin.Float", + "kotlin.Double", + "kotlin.Boolean", + "kotlin.Char", + "kotlin.Array", + "kotlin.collections.List", + "kotlin.collections.Set", + "kotlin.collections.Map", + "kotlin.ByteArray", + "kotlin.CharArray", + "kotlin.ShortArray", + "kotlin.IntArray", + "kotlin.LongArray", + "kotlin.FloatArray", + "kotlin.DoubleArray", + "kotlin.BooleanArray" )); instantiationLibraryFunction = new HashSet<>(Arrays.asList( - "arrayOf", - "mapOf" + "arrayOf", + "mapOf" )); typeMapping = new HashMap<>(); @@ -513,9 +511,28 @@ public String toVarName(String name) { @Override public String toEnumName(CodegenProperty property) { + //Need to handle case when property name is escaped (possible reserved word) + if (property.name.startsWith("`")) { + return String.format("`%s", StringUtils.capitalize(property.name.substring(1))); + } return StringUtils.capitalize(property.name); } + @Override + public Mustache.Compiler processCompiler(Mustache.Compiler compiler) { + Mustache.Escaper KOTLIN = new Mustache.Escaper() { + @Override + public String escape(String text) { + if (text.startsWith("`") && text.endsWith("`")) { + String unescaped = text.substring(1, text.length() - 1); + return "`" + Escapers.HTML.escape(unescaped) + "`"; + } + return Escapers.HTML.escape(text); + } + }; + return compiler.withEscaper(KOTLIN); + } + @Override public void addHandlebarHelpers(Handlebars handlebars) { super.addHandlebarHelpers(handlebars); @@ -591,11 +608,11 @@ protected boolean isReservedWord(String word) { protected boolean needToImport(String type) { // provides extra protection against improperly trying to import language primitives and java types boolean imports = - !type.startsWith("kotlin.") && - !type.startsWith("java.") && - !defaultIncludes.contains(type) && - !languageSpecificPrimitives.contains(type) && - !instantiationLibraryFunction.contains(type); + !type.startsWith("kotlin.") && + !type.startsWith("java.") && + !defaultIncludes.contains(type) && + !languageSpecificPrimitives.contains(type) && + !instantiationLibraryFunction.contains(type); return imports; } diff --git a/src/test/java/io/swagger/codegen/v3/generators/kotlin/KotlinClientCodegenModelTest.java b/src/test/java/io/swagger/codegen/v3/generators/kotlin/KotlinClientCodegenModelTest.java index e783e52c97..5077986515 100644 --- a/src/test/java/io/swagger/codegen/v3/generators/kotlin/KotlinClientCodegenModelTest.java +++ b/src/test/java/io/swagger/codegen/v3/generators/kotlin/KotlinClientCodegenModelTest.java @@ -272,7 +272,8 @@ public static Object[][] enumNames() { {"VALUE1", "VALUE1"}, {"1", "_1"}, {"1X2", "_1X2"}, - {"1x2", "_1X2"} + {"1x2", "_1X2"}, + {"`return`", "`Return`"}, }; } @@ -283,6 +284,25 @@ public void sanitizeEnumVarNames(final String name, final String expectedName) { } + @DataProvider + public static Object[][] enumClassNames() { + return new Object[][]{ + {"name1", "Name1"}, + {"A", "A"}, + {"NAME2", "NAME2"}, + {"`return`", "`Return`"}, + }; + } + + @Test(dataProvider = "enumClassNames", description = "sanitize Enum class names") + public void capitalizeEnumName(final String name, final String expectedName) { + final KotlinClientCodegen codegen = new KotlinClientCodegen(); + CodegenProperty property = new CodegenProperty(); + property.setName(name); + Assert.assertEquals(codegen.toEnumName(property), expectedName); + + } + private static class ModelNameTest { private String expectedName; private String expectedClassName; @@ -297,4 +317,4 @@ private ModelNameTest(String expectedName, String expectedClassName) { this.expectedClassName = expectedClassName; } } -} \ No newline at end of file +} From 676ae877a680ca6248058c75422e548e0fbb6e3f Mon Sep 17 00:00:00 2001 From: Artem Tarynin Date: Wed, 11 Oct 2023 12:27:29 +0300 Subject: [PATCH 2/5] issue-12243: fix checktyle --- .../kotlin/AbstractKotlinCodegen.java | 62 +++++++++++++------ .../kotlin/KotlinClientCodegenModelTest.java | 18 +++--- 2 files changed, 49 insertions(+), 31 deletions(-) diff --git a/src/main/java/io/swagger/codegen/v3/generators/kotlin/AbstractKotlinCodegen.java b/src/main/java/io/swagger/codegen/v3/generators/kotlin/AbstractKotlinCodegen.java index af40cf1050..87d61d99a7 100644 --- a/src/main/java/io/swagger/codegen/v3/generators/kotlin/AbstractKotlinCodegen.java +++ b/src/main/java/io/swagger/codegen/v3/generators/kotlin/AbstractKotlinCodegen.java @@ -14,13 +14,16 @@ import io.swagger.v3.oas.models.media.MapSchema; import io.swagger.v3.oas.models.media.ObjectSchema; import io.swagger.v3.oas.models.media.Schema; +import java.io.File; +import java.util.Arrays; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.io.File; -import java.util.*; - public abstract class AbstractKotlinCodegen extends DefaultCodegenConfig { private static Logger LOGGER = LoggerFactory.getLogger(AbstractKotlinCodegen.class); @@ -203,11 +206,15 @@ public AbstractKotlinCodegen() { specialCharReplacements.put(":", "Colon"); cliOptions.clear(); - addOption(CodegenConstants.SOURCE_FOLDER, CodegenConstants.SOURCE_FOLDER_DESC, sourceFolder); - addOption(CodegenConstants.PACKAGE_NAME, "Generated artifact package name (e.g. io.swagger).", packageName); - addOption(CodegenConstants.GROUP_ID, "Generated artifact package's organization (i.e. maven groupId).", groupId); + addOption(CodegenConstants.SOURCE_FOLDER, CodegenConstants.SOURCE_FOLDER_DESC, + sourceFolder); + addOption(CodegenConstants.PACKAGE_NAME, + "Generated artifact package name (e.g. io.swagger).", packageName); + addOption(CodegenConstants.GROUP_ID, + "Generated artifact package's organization (i.e. maven groupId).", groupId); addOption(CodegenConstants.ARTIFACT_ID, "Generated artifact id (name of jar).", artifactId); - addOption(CodegenConstants.ARTIFACT_VERSION, "Generated artifact's package version.", artifactVersion); + addOption(CodegenConstants.ARTIFACT_VERSION, "Generated artifact's package version.", + artifactVersion); } protected void addOption(String key, String description) { @@ -216,13 +223,17 @@ protected void addOption(String key, String description) { protected void addOption(String key, String description, String defaultValue) { CliOption option = new CliOption(key, description); - if (defaultValue != null) option.defaultValue(defaultValue); + if (defaultValue != null) { + option.defaultValue(defaultValue); + } cliOptions.add(option); } protected void addSwitch(String key, String description, Boolean defaultValue) { CliOption option = CliOption.newBoolean(key, description); - if (defaultValue != null) option.defaultValue(defaultValue.toString()); + if (defaultValue != null) { + option.defaultValue(defaultValue.toString()); + } cliOptions.add(option); } @@ -238,7 +249,8 @@ public String apiDocFileFolder() { @Override public String apiFileFolder() { - return outputFolder + File.separator + sourceFolder + File.separator + apiPackage().replace('.', File.separatorChar); + return outputFolder + File.separator + sourceFolder + File.separator + + apiPackage().replace('.', File.separatorChar); } @Override @@ -278,15 +290,19 @@ public String getTypeDeclaration(Schema propertySchema) { } else if (propertySchema instanceof MapSchema && hasSchemaProperties(propertySchema)) { Schema inner = (Schema) propertySchema.getAdditionalProperties(); if (inner == null) { - LOGGER.warn(propertySchema.getName() + "(map property) does not have a proper inner type defined"); + LOGGER.warn(propertySchema.getName() + + "(map property) does not have a proper inner type defined"); // TODO maybe better defaulting to StringProperty than returning null return null; } // Maps will be keyed only by primitive Kotlin string - return String.format("%s", getSchemaType(propertySchema), getTypeDeclaration(inner)); - } else if (propertySchema instanceof MapSchema && hasTrueAdditionalProperties(propertySchema)) { + return String.format("%s", getSchemaType(propertySchema), + getTypeDeclaration(inner)); + } else if (propertySchema instanceof MapSchema && + hasTrueAdditionalProperties(propertySchema)) { Schema inner = new ObjectSchema(); - return String.format("%s", getSchemaType(propertySchema), getTypeDeclaration(inner)); + return String.format("%s", getSchemaType(propertySchema), + getTypeDeclaration(inner)); } return super.getTypeDeclaration(propertySchema); } @@ -326,7 +342,8 @@ public String modelDocFileFolder() { @Override public String modelFileFolder() { - return outputFolder + File.separator + sourceFolder + File.separator + modelPackage().replace('.', File.separatorChar); + return outputFolder + File.separator + sourceFolder + File.separator + + modelPackage().replace('.', File.separatorChar); } @Override @@ -346,10 +363,12 @@ public void processOpts() { if (additionalProperties.containsKey(CodegenConstants.PACKAGE_NAME)) { this.setPackageName((String) additionalProperties.get(CodegenConstants.PACKAGE_NAME)); - if (!additionalProperties.containsKey(CodegenConstants.MODEL_PACKAGE)) + if (!additionalProperties.containsKey(CodegenConstants.MODEL_PACKAGE)) { this.setModelPackage(packageName + ".models"); - if (!additionalProperties.containsKey(CodegenConstants.API_PACKAGE)) + } + if (!additionalProperties.containsKey(CodegenConstants.API_PACKAGE)) { this.setApiPackage(packageName + ".apis"); + } } else { additionalProperties.put(CodegenConstants.PACKAGE_NAME, packageName); } @@ -367,13 +386,15 @@ public void processOpts() { } if (additionalProperties.containsKey(CodegenConstants.ARTIFACT_VERSION)) { - this.setArtifactVersion((String) additionalProperties.get(CodegenConstants.ARTIFACT_VERSION)); + this.setArtifactVersion( + (String) additionalProperties.get(CodegenConstants.ARTIFACT_VERSION)); } else { additionalProperties.put(CodegenConstants.ARTIFACT_VERSION, artifactVersion); } if (additionalProperties.containsKey(CodegenConstants.INVOKER_PACKAGE)) { - LOGGER.warn(CodegenConstants.INVOKER_PACKAGE + " with " + this.getName() + " generator is ignored. Use " + CodegenConstants.PACKAGE_NAME + "."); + LOGGER.warn(CodegenConstants.INVOKER_PACKAGE + " with " + this.getName() + + " generator is ignored. Use " + CodegenConstants.PACKAGE_NAME + "."); } additionalProperties.put(CodegenConstants.API_PACKAGE, apiPackage()); @@ -570,7 +591,8 @@ private String sanitizeKotlinSpecificNames(final String name) { for (Map.Entry specialCharacters : specialCharReplacements.entrySet()) { // Underscore is the only special character we'll allow if (!specialCharacters.getKey().equals("_")) { - word = word.replaceAll("\\Q" + specialCharacters.getKey() + "\\E", specialCharacters.getValue()); + word = word.replaceAll("\\Q" + specialCharacters.getKey() + "\\E", + specialCharacters.getValue()); } } diff --git a/src/test/java/io/swagger/codegen/v3/generators/kotlin/KotlinClientCodegenModelTest.java b/src/test/java/io/swagger/codegen/v3/generators/kotlin/KotlinClientCodegenModelTest.java index 5077986515..04946483fb 100644 --- a/src/test/java/io/swagger/codegen/v3/generators/kotlin/KotlinClientCodegenModelTest.java +++ b/src/test/java/io/swagger/codegen/v3/generators/kotlin/KotlinClientCodegenModelTest.java @@ -1,10 +1,11 @@ package io.swagger.codegen.v3.generators.kotlin; +import static io.swagger.codegen.v3.generators.handlebars.ExtensionHelper.getBooleanValue; + import io.swagger.codegen.v3.CodegenConfig; import io.swagger.codegen.v3.CodegenConstants; import io.swagger.codegen.v3.CodegenModel; import io.swagger.codegen.v3.CodegenProperty; -import io.swagger.codegen.v3.generators.AbstractCodegenTest; import io.swagger.v3.oas.models.Components; import io.swagger.v3.oas.models.OpenAPI; import io.swagger.v3.oas.models.media.ArraySchema; @@ -20,10 +21,6 @@ import org.testng.annotations.DataProvider; import org.testng.annotations.Test; -import java.util.HashMap; - -import static io.swagger.codegen.v3.generators.handlebars.ExtensionHelper.getBooleanValue; - @SuppressWarnings("static-method") public class KotlinClientCodegenModelTest { @@ -286,11 +283,11 @@ public void sanitizeEnumVarNames(final String name, final String expectedName) { @DataProvider public static Object[][] enumClassNames() { - return new Object[][]{ - {"name1", "Name1"}, - {"A", "A"}, - {"NAME2", "NAME2"}, - {"`return`", "`Return`"}, + return new Object[][] { + {"name1", "Name1"}, + {"A", "A"}, + {"NAME2", "NAME2"}, + {"`return`", "`Return`"}, }; } @@ -300,7 +297,6 @@ public void capitalizeEnumName(final String name, final String expectedName) { CodegenProperty property = new CodegenProperty(); property.setName(name); Assert.assertEquals(codegen.toEnumName(property), expectedName); - } private static class ModelNameTest { From e82d6c1f9648436726f6b96522263be5d8ed5d04 Mon Sep 17 00:00:00 2001 From: Artem Tarynin Date: Wed, 11 Oct 2023 12:30:14 +0300 Subject: [PATCH 3/5] issue-12243: fix checktyle2 --- .../kotlin/AbstractKotlinCodegen.java | 292 ++++++++---------- 1 file changed, 137 insertions(+), 155 deletions(-) diff --git a/src/main/java/io/swagger/codegen/v3/generators/kotlin/AbstractKotlinCodegen.java b/src/main/java/io/swagger/codegen/v3/generators/kotlin/AbstractKotlinCodegen.java index 87d61d99a7..04869d1899 100644 --- a/src/main/java/io/swagger/codegen/v3/generators/kotlin/AbstractKotlinCodegen.java +++ b/src/main/java/io/swagger/codegen/v3/generators/kotlin/AbstractKotlinCodegen.java @@ -1,8 +1,6 @@ package io.swagger.codegen.v3.generators.kotlin; -import com.github.jknack.handlebars.Handlebars; import com.github.jknack.handlebars.helper.ConditionalHelpers; -import com.github.jknack.handlebars.helper.StringHelpers; import com.samskivert.mustache.Escapers; import com.samskivert.mustache.Mustache; import io.swagger.codegen.v3.CliOption; @@ -14,15 +12,18 @@ import io.swagger.v3.oas.models.media.MapSchema; import io.swagger.v3.oas.models.media.ObjectSchema; import io.swagger.v3.oas.models.media.Schema; +import org.apache.commons.lang3.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import com.github.jknack.handlebars.helper.StringHelpers; +import com.github.jknack.handlebars.Handlebars; + import java.io.File; import java.util.Arrays; import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Set; -import org.apache.commons.lang3.StringUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; public abstract class AbstractKotlinCodegen extends DefaultCodegenConfig { @@ -45,126 +46,126 @@ public AbstractKotlinCodegen() { supportsInheritance = true; languageSpecificPrimitives = new HashSet<>(Arrays.asList( - "kotlin.Any", - "kotlin.Byte", - "kotlin.Short", - "kotlin.Int", - "kotlin.Long", - "kotlin.Float", - "kotlin.Double", - "kotlin.Boolean", - "kotlin.Char", - "kotlin.String", - "kotlin.Array", - "kotlin.collections.List", - "kotlin.collections.Map", - "kotlin.collections.Set", - "kotlin.ByteArray", - "kotlin.CharArray", - "kotlin.ShortArray", - "kotlin.IntArray", - "kotlin.LongArray", - "kotlin.FloatArray", - "kotlin.DoubleArray", - "kotlin.BooleanArray" + "kotlin.Any", + "kotlin.Byte", + "kotlin.Short", + "kotlin.Int", + "kotlin.Long", + "kotlin.Float", + "kotlin.Double", + "kotlin.Boolean", + "kotlin.Char", + "kotlin.String", + "kotlin.Array", + "kotlin.collections.List", + "kotlin.collections.Map", + "kotlin.collections.Set", + "kotlin.ByteArray", + "kotlin.CharArray", + "kotlin.ShortArray", + "kotlin.IntArray", + "kotlin.LongArray", + "kotlin.FloatArray", + "kotlin.DoubleArray", + "kotlin.BooleanArray" )); // this includes hard reserved words defined by https://github.com/JetBrains/kotlin/blob/master/core/descriptors/src/org/jetbrains/kotlin/renderer/KeywordStringsGenerated.java // as well as keywords from https://kotlinlang.org/docs/reference/keyword-reference.html reservedWords = new HashSet<>(Arrays.asList( - "abstract", - "actual", - "annotation", - "as", - "break", - "case", - "catch", - "class", - "companion", - "const", - "constructor", - "continue", - "crossinline", - "data", - "delegate", - "do", - "else", - "enum", - "expect", - "external", - "false", - "final", - "finally", - "for", - "fun", - "if", - "in", - "infix", - "init", - "inline", - "inner", - "interface", - "internal", - "is", - "it", - "lateinit", - "lazy", - "noinline", - "null", - "object", - "open", - "operator", - "out", - "override", - "package", - "private", - "protected", - "public", - "reified", - "return", - "sealed", - "super", - "suspend", - "tailrec", - "this", - "throw", - "true", - "try", - "typealias", - "typeof", - "val", - "var", - "vararg", - "when", - "while" + "abstract", + "actual", + "annotation", + "as", + "break", + "case", + "catch", + "class", + "companion", + "const", + "constructor", + "continue", + "crossinline", + "data", + "delegate", + "do", + "else", + "enum", + "expect", + "external", + "false", + "final", + "finally", + "for", + "fun", + "if", + "in", + "infix", + "init", + "inline", + "inner", + "interface", + "internal", + "is", + "it", + "lateinit", + "lazy", + "noinline", + "null", + "object", + "open", + "operator", + "out", + "override", + "package", + "private", + "protected", + "public", + "reified", + "return", + "sealed", + "super", + "suspend", + "tailrec", + "this", + "throw", + "true", + "try", + "typealias", + "typeof", + "val", + "var", + "vararg", + "when", + "while" )); defaultIncludes = new HashSet<>(Arrays.asList( - "kotlin.Byte", - "kotlin.Short", - "kotlin.Int", - "kotlin.Long", - "kotlin.Float", - "kotlin.Double", - "kotlin.Boolean", - "kotlin.Char", - "kotlin.Array", - "kotlin.collections.List", - "kotlin.collections.Set", - "kotlin.collections.Map", - "kotlin.ByteArray", - "kotlin.CharArray", - "kotlin.ShortArray", - "kotlin.IntArray", - "kotlin.LongArray", - "kotlin.FloatArray", - "kotlin.DoubleArray", - "kotlin.BooleanArray" + "kotlin.Byte", + "kotlin.Short", + "kotlin.Int", + "kotlin.Long", + "kotlin.Float", + "kotlin.Double", + "kotlin.Boolean", + "kotlin.Char", + "kotlin.Array", + "kotlin.collections.List", + "kotlin.collections.Set", + "kotlin.collections.Map", + "kotlin.ByteArray", + "kotlin.CharArray", + "kotlin.ShortArray", + "kotlin.IntArray", + "kotlin.LongArray", + "kotlin.FloatArray", + "kotlin.DoubleArray", + "kotlin.BooleanArray" )); instantiationLibraryFunction = new HashSet<>(Arrays.asList( - "arrayOf", - "mapOf" + "arrayOf", + "mapOf" )); typeMapping = new HashMap<>(); @@ -206,15 +207,11 @@ public AbstractKotlinCodegen() { specialCharReplacements.put(":", "Colon"); cliOptions.clear(); - addOption(CodegenConstants.SOURCE_FOLDER, CodegenConstants.SOURCE_FOLDER_DESC, - sourceFolder); - addOption(CodegenConstants.PACKAGE_NAME, - "Generated artifact package name (e.g. io.swagger).", packageName); - addOption(CodegenConstants.GROUP_ID, - "Generated artifact package's organization (i.e. maven groupId).", groupId); + addOption(CodegenConstants.SOURCE_FOLDER, CodegenConstants.SOURCE_FOLDER_DESC, sourceFolder); + addOption(CodegenConstants.PACKAGE_NAME, "Generated artifact package name (e.g. io.swagger).", packageName); + addOption(CodegenConstants.GROUP_ID, "Generated artifact package's organization (i.e. maven groupId).", groupId); addOption(CodegenConstants.ARTIFACT_ID, "Generated artifact id (name of jar).", artifactId); - addOption(CodegenConstants.ARTIFACT_VERSION, "Generated artifact's package version.", - artifactVersion); + addOption(CodegenConstants.ARTIFACT_VERSION, "Generated artifact's package version.", artifactVersion); } protected void addOption(String key, String description) { @@ -223,17 +220,13 @@ protected void addOption(String key, String description) { protected void addOption(String key, String description, String defaultValue) { CliOption option = new CliOption(key, description); - if (defaultValue != null) { - option.defaultValue(defaultValue); - } + if (defaultValue != null) option.defaultValue(defaultValue); cliOptions.add(option); } protected void addSwitch(String key, String description, Boolean defaultValue) { CliOption option = CliOption.newBoolean(key, description); - if (defaultValue != null) { - option.defaultValue(defaultValue.toString()); - } + if (defaultValue != null) option.defaultValue(defaultValue.toString()); cliOptions.add(option); } @@ -249,8 +242,7 @@ public String apiDocFileFolder() { @Override public String apiFileFolder() { - return outputFolder + File.separator + sourceFolder + File.separator + - apiPackage().replace('.', File.separatorChar); + return outputFolder + File.separator + sourceFolder + File.separator + apiPackage().replace('.', File.separatorChar); } @Override @@ -290,19 +282,15 @@ public String getTypeDeclaration(Schema propertySchema) { } else if (propertySchema instanceof MapSchema && hasSchemaProperties(propertySchema)) { Schema inner = (Schema) propertySchema.getAdditionalProperties(); if (inner == null) { - LOGGER.warn(propertySchema.getName() + - "(map property) does not have a proper inner type defined"); + LOGGER.warn(propertySchema.getName() + "(map property) does not have a proper inner type defined"); // TODO maybe better defaulting to StringProperty than returning null return null; } // Maps will be keyed only by primitive Kotlin string - return String.format("%s", getSchemaType(propertySchema), - getTypeDeclaration(inner)); - } else if (propertySchema instanceof MapSchema && - hasTrueAdditionalProperties(propertySchema)) { + return String.format("%s", getSchemaType(propertySchema), getTypeDeclaration(inner)); + } else if (propertySchema instanceof MapSchema && hasTrueAdditionalProperties(propertySchema)) { Schema inner = new ObjectSchema(); - return String.format("%s", getSchemaType(propertySchema), - getTypeDeclaration(inner)); + return String.format("%s", getSchemaType(propertySchema), getTypeDeclaration(inner)); } return super.getTypeDeclaration(propertySchema); } @@ -342,8 +330,7 @@ public String modelDocFileFolder() { @Override public String modelFileFolder() { - return outputFolder + File.separator + sourceFolder + File.separator + - modelPackage().replace('.', File.separatorChar); + return outputFolder + File.separator + sourceFolder + File.separator + modelPackage().replace('.', File.separatorChar); } @Override @@ -363,12 +350,10 @@ public void processOpts() { if (additionalProperties.containsKey(CodegenConstants.PACKAGE_NAME)) { this.setPackageName((String) additionalProperties.get(CodegenConstants.PACKAGE_NAME)); - if (!additionalProperties.containsKey(CodegenConstants.MODEL_PACKAGE)) { + if (!additionalProperties.containsKey(CodegenConstants.MODEL_PACKAGE)) this.setModelPackage(packageName + ".models"); - } - if (!additionalProperties.containsKey(CodegenConstants.API_PACKAGE)) { + if (!additionalProperties.containsKey(CodegenConstants.API_PACKAGE)) this.setApiPackage(packageName + ".apis"); - } } else { additionalProperties.put(CodegenConstants.PACKAGE_NAME, packageName); } @@ -386,15 +371,13 @@ public void processOpts() { } if (additionalProperties.containsKey(CodegenConstants.ARTIFACT_VERSION)) { - this.setArtifactVersion( - (String) additionalProperties.get(CodegenConstants.ARTIFACT_VERSION)); + this.setArtifactVersion((String) additionalProperties.get(CodegenConstants.ARTIFACT_VERSION)); } else { additionalProperties.put(CodegenConstants.ARTIFACT_VERSION, artifactVersion); } if (additionalProperties.containsKey(CodegenConstants.INVOKER_PACKAGE)) { - LOGGER.warn(CodegenConstants.INVOKER_PACKAGE + " with " + this.getName() + - " generator is ignored. Use " + CodegenConstants.PACKAGE_NAME + "."); + LOGGER.warn(CodegenConstants.INVOKER_PACKAGE + " with " + this.getName() + " generator is ignored. Use " + CodegenConstants.PACKAGE_NAME + "."); } additionalProperties.put(CodegenConstants.API_PACKAGE, apiPackage()); @@ -591,8 +574,7 @@ private String sanitizeKotlinSpecificNames(final String name) { for (Map.Entry specialCharacters : specialCharReplacements.entrySet()) { // Underscore is the only special character we'll allow if (!specialCharacters.getKey().equals("_")) { - word = word.replaceAll("\\Q" + specialCharacters.getKey() + "\\E", - specialCharacters.getValue()); + word = word.replaceAll("\\Q" + specialCharacters.getKey() + "\\E", specialCharacters.getValue()); } } @@ -630,11 +612,11 @@ protected boolean isReservedWord(String word) { protected boolean needToImport(String type) { // provides extra protection against improperly trying to import language primitives and java types boolean imports = - !type.startsWith("kotlin.") && - !type.startsWith("java.") && - !defaultIncludes.contains(type) && - !languageSpecificPrimitives.contains(type) && - !instantiationLibraryFunction.contains(type); + !type.startsWith("kotlin.") && + !type.startsWith("java.") && + !defaultIncludes.contains(type) && + !languageSpecificPrimitives.contains(type) && + !instantiationLibraryFunction.contains(type); return imports; } From 4b6b8271af4da7c1719059bd682903a4242b740a Mon Sep 17 00:00:00 2001 From: Artem Tarynin Date: Wed, 11 Oct 2023 12:37:05 +0300 Subject: [PATCH 4/5] issue-12243: remove test staff --- .../v3/generators/kotlin/KotlinClientCodegenModelTest.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/test/java/io/swagger/codegen/v3/generators/kotlin/KotlinClientCodegenModelTest.java b/src/test/java/io/swagger/codegen/v3/generators/kotlin/KotlinClientCodegenModelTest.java index 04946483fb..e9bd753933 100644 --- a/src/test/java/io/swagger/codegen/v3/generators/kotlin/KotlinClientCodegenModelTest.java +++ b/src/test/java/io/swagger/codegen/v3/generators/kotlin/KotlinClientCodegenModelTest.java @@ -269,8 +269,7 @@ public static Object[][] enumNames() { {"VALUE1", "VALUE1"}, {"1", "_1"}, {"1X2", "_1X2"}, - {"1x2", "_1X2"}, - {"`return`", "`Return`"}, + {"1x2", "_1X2"} }; } From 7246fb9e834ae0df50b39d5c3e84355be40dbad0 Mon Sep 17 00:00:00 2001 From: Artem Tarynin Date: Wed, 11 Oct 2023 12:39:50 +0300 Subject: [PATCH 5/5] issue-12243: import rearrange --- .../v3/generators/kotlin/KotlinClientCodegenModelTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/java/io/swagger/codegen/v3/generators/kotlin/KotlinClientCodegenModelTest.java b/src/test/java/io/swagger/codegen/v3/generators/kotlin/KotlinClientCodegenModelTest.java index e9bd753933..73bf8d7355 100644 --- a/src/test/java/io/swagger/codegen/v3/generators/kotlin/KotlinClientCodegenModelTest.java +++ b/src/test/java/io/swagger/codegen/v3/generators/kotlin/KotlinClientCodegenModelTest.java @@ -1,7 +1,5 @@ package io.swagger.codegen.v3.generators.kotlin; -import static io.swagger.codegen.v3.generators.handlebars.ExtensionHelper.getBooleanValue; - import io.swagger.codegen.v3.CodegenConfig; import io.swagger.codegen.v3.CodegenConstants; import io.swagger.codegen.v3.CodegenModel; @@ -21,6 +19,8 @@ import org.testng.annotations.DataProvider; import org.testng.annotations.Test; +import static io.swagger.codegen.v3.generators.handlebars.ExtensionHelper.getBooleanValue; + @SuppressWarnings("static-method") public class KotlinClientCodegenModelTest {