Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG][KOTLIN] Fix for reserved enum name #1196

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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")
Expand Down Expand Up @@ -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;
Expand All @@ -297,4 +312,4 @@ private ModelNameTest(String expectedName, String expectedClassName) {
this.expectedClassName = expectedClassName;
}
}
}
}