forked from loafle/openapi-generator-original
[Kotlin] Add ability to use modelNamePrefix/modelNameSuffix (#2349)
This commit is contained in:
committed by
William Cheng
parent
d2244a3baf
commit
c8ecc5bf45
@@ -489,6 +489,7 @@ public abstract class AbstractKotlinCodegen extends DefaultCodegen implements Co
|
||||
*/
|
||||
@Override
|
||||
public String toModelName(final String name) {
|
||||
|
||||
// Allow for explicitly configured kotlin.* and java.* types
|
||||
if (name.startsWith("kotlin.") || name.startsWith("java.")) {
|
||||
return name;
|
||||
@@ -500,10 +501,21 @@ public abstract class AbstractKotlinCodegen extends DefaultCodegen implements Co
|
||||
}
|
||||
|
||||
String modifiedName = name.replaceAll("\\.", "");
|
||||
modifiedName = sanitizeKotlinSpecificNames(modifiedName);
|
||||
String sanitizedName = sanitizeKotlinSpecificNames(modifiedName);
|
||||
|
||||
String nameWithPrefixSuffix = sanitizedName;
|
||||
if (!StringUtils.isEmpty(modelNamePrefix)) {
|
||||
// add '_' so that model name can be camelized correctly
|
||||
nameWithPrefixSuffix = modelNamePrefix + "_" + nameWithPrefixSuffix;
|
||||
}
|
||||
|
||||
if (!StringUtils.isEmpty(modelNameSuffix)) {
|
||||
// add '_' so that model name can be camelized correctly
|
||||
nameWithPrefixSuffix = nameWithPrefixSuffix + "_" + modelNameSuffix;
|
||||
}
|
||||
|
||||
// Camelize name of nested properties
|
||||
modifiedName = camelize(modifiedName);
|
||||
modifiedName = camelize(nameWithPrefixSuffix);
|
||||
|
||||
// model name cannot use reserved keyword, e.g. return
|
||||
if (isReservedWord(modifiedName)) {
|
||||
|
||||
@@ -83,4 +83,19 @@ public class AbstractKotlinCodegenTest {
|
||||
assertTrue(codegen.isDataTypeString("kotlin.String"));
|
||||
assertTrue(codegen.isDataTypeString("String"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toModelNameShouldUseProvidedMapping() throws Exception {
|
||||
codegen.importMapping().put("json_myclass", "com.test.MyClass");
|
||||
assertEquals("com.test.MyClass", codegen.toModelName("json_myclass"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertModelName() throws Exception {
|
||||
assertEquals(codegen.toModelName("name"), "Name");
|
||||
assertEquals(codegen.toModelName("$name"), "Dollarname");
|
||||
assertEquals(codegen.toModelName("nam#e"), "NamHashe");
|
||||
assertEquals(codegen.toModelName("$another-fake?"), "DollaranotherMinusfakeQuestionMark");
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user