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..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,6 +1,8 @@ package io.swagger.codegen.v3.generators.kotlin; import com.github.jknack.handlebars.helper.ConditionalHelpers; +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; @@ -513,9 +515,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); 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..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 @@ -4,7 +4,6 @@ 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,8 +19,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") @@ -283,6 +280,24 @@ 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 +312,4 @@ private ModelNameTest(String expectedName, String expectedClassName) { this.expectedClassName = expectedClassName; } } -} \ No newline at end of file +}