Skip to content

Commit 72afbbc

Browse files
authored
Merge pull request #518 from crazyk2/enum_naming
Enum naming
2 parents 152df38 + 3f8b8be commit 72afbbc

File tree

4 files changed

+33
-72
lines changed

4 files changed

+33
-72
lines changed

src/main/java/io/swagger/codegen/v3/generators/kotlin/AbstractKotlinCodegen.java

Lines changed: 3 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@ public abstract class AbstractKotlinCodegen extends DefaultCodegenConfig {
3333
protected String apiDocPath = "docs/";
3434
protected String modelDocPath = "docs/";
3535

36-
protected CodegenConstants.ENUM_PROPERTY_NAMING_TYPE enumPropertyNaming = CodegenConstants.ENUM_PROPERTY_NAMING_TYPE.camelCase;
37-
3836
public AbstractKotlinCodegen() {
3937
super();
4038
supportsInheritance = true;
@@ -197,16 +195,14 @@ public AbstractKotlinCodegen() {
197195
importMapping.put("LocalTime", "java.time.LocalTime");
198196

199197
specialCharReplacements.put(";", "Semicolon");
198+
specialCharReplacements.put(":", "Colon");
200199

201200
cliOptions.clear();
202201
addOption(CodegenConstants.SOURCE_FOLDER, CodegenConstants.SOURCE_FOLDER_DESC, sourceFolder);
203202
addOption(CodegenConstants.PACKAGE_NAME, "Generated artifact package name (e.g. io.swagger).", packageName);
204203
addOption(CodegenConstants.GROUP_ID, "Generated artifact package's organization (i.e. maven groupId).", groupId);
205204
addOption(CodegenConstants.ARTIFACT_ID, "Generated artifact id (name of jar).", artifactId);
206205
addOption(CodegenConstants.ARTIFACT_VERSION, "Generated artifact's package version.", artifactVersion);
207-
208-
CliOption enumPropertyNamingOpt = new CliOption(CodegenConstants.ENUM_PROPERTY_NAMING, CodegenConstants.ENUM_PROPERTY_NAMING_DESC);
209-
cliOptions.add(enumPropertyNamingOpt.defaultValue(enumPropertyNaming.name()));
210206
}
211207

212208
protected void addOption(String key, String description) {
@@ -257,27 +253,6 @@ public String escapeUnsafeCharacters(String input) {
257253
return input.replace("*/", "*_/").replace("/*", "/_*");
258254
}
259255

260-
public CodegenConstants.ENUM_PROPERTY_NAMING_TYPE getEnumPropertyNaming() {
261-
return this.enumPropertyNaming;
262-
}
263-
264-
/**
265-
* Sets the naming convention for Kotlin enum properties
266-
*
267-
* @param enumPropertyNamingType The string representation of the naming convention, as defined by {@link CodegenConstants.ENUM_PROPERTY_NAMING_TYPE}
268-
*/
269-
public void setEnumPropertyNaming(final String enumPropertyNamingType) {
270-
try {
271-
this.enumPropertyNaming = CodegenConstants.ENUM_PROPERTY_NAMING_TYPE.valueOf(enumPropertyNamingType);
272-
} catch (IllegalArgumentException ex) {
273-
StringBuilder sb = new StringBuilder(enumPropertyNamingType + " is an invalid enum property naming option. Please choose from:");
274-
for (CodegenConstants.ENUM_PROPERTY_NAMING_TYPE t : CodegenConstants.ENUM_PROPERTY_NAMING_TYPE.values()) {
275-
sb.append("\n ").append(t.name());
276-
}
277-
throw new RuntimeException(sb.toString());
278-
}
279-
}
280-
281256
/**
282257
* Output the type declaration of the property
283258
*
@@ -351,10 +326,6 @@ public Map<String, Object> postProcessModels(Map<String, Object> objs) {
351326
public void processOpts() {
352327
super.processOpts();
353328

354-
if (additionalProperties.containsKey(CodegenConstants.ENUM_PROPERTY_NAMING)) {
355-
setEnumPropertyNaming((String) additionalProperties.get(CodegenConstants.ENUM_PROPERTY_NAMING));
356-
}
357-
358329
if (additionalProperties.containsKey(CodegenConstants.SOURCE_FOLDER)) {
359330
this.setSourceFolder((String) additionalProperties.get(CodegenConstants.SOURCE_FOLDER));
360331
} else {
@@ -435,29 +406,7 @@ public String toEnumVarName(String value, String datatype) {
435406
} else {
436407
modified = value;
437408
modified = sanitizeKotlinSpecificNames(modified);
438-
}
439-
440-
switch (getEnumPropertyNaming()) {
441-
case original:
442-
// NOTE: This is provided as a last-case allowance, but will still result in reserved words being escaped.
443-
modified = value;
444-
break;
445-
case camelCase:
446-
// NOTE: Removes hyphens and underscores
447-
modified = camelize(modified, true);
448-
break;
449-
case PascalCase:
450-
// NOTE: Removes hyphens and underscores
451-
String result = camelize(modified);
452-
modified = titleCase(result);
453-
break;
454-
case snake_case:
455-
// NOTE: Removes hyphens
456-
modified = underscore(modified);
457-
break;
458-
case UPPERCASE:
459-
modified = modified.toUpperCase();
460-
break;
409+
modified = modified.toUpperCase();
461410
}
462411

463412
if (isReservedWord(modified)) {
@@ -623,4 +572,4 @@ protected boolean needToImport(String type) {
623572

624573
return imports;
625574
}
626-
}
575+
}

src/test/java/io/swagger/codegen/v3/generators/kotlin/KotlinClientCodegenModelTest.java

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,34 +31,34 @@ public class KotlinClientCodegenModelTest {
3131

3232
private Schema getArrayTestSchema() {
3333
final Schema propertySchema = new ArraySchema()
34-
.items(new StringSchema())
35-
.description("an array property");
34+
.items(new StringSchema())
35+
.description("an array property");
3636
return new Schema()
37-
.type("object")
38-
.description("a sample model")
39-
.addProperties("examples", propertySchema);
37+
.type("object")
38+
.description("a sample model")
39+
.addProperties("examples", propertySchema);
4040
}
4141

4242
private Schema getSimpleSchema() {
4343
return new Schema()
4444
.type("object")
4545
.description("a sample model")
4646
.addProperties("id", new IntegerSchema()
47-
.format(SchemaTypeUtil.INTEGER64_FORMAT))
47+
.format(SchemaTypeUtil.INTEGER64_FORMAT))
4848
.addProperties("first-name", new StringSchema()
49-
.example("Tony"))
49+
.example("Tony"))
5050
.addProperties("createdAt", new DateTimeSchema())
5151
.addRequiredItem("id")
5252
.addRequiredItem("first-name");
5353
}
5454

5555
private Schema getMapSchema() {
5656
return new Schema()
57-
.type("object")
58-
.description("a sample model")
59-
.addProperties("mapping", new MapSchema()
60-
.additionalProperties(new StringSchema()))
61-
.addRequiredItem("id");
57+
.type("object")
58+
.description("a sample model")
59+
.addProperties("mapping", new MapSchema()
60+
.additionalProperties(new StringSchema()))
61+
.addRequiredItem("id");
6262
}
6363

6464
private Schema getComplexSchema() {
@@ -266,6 +266,23 @@ public void sanitizeModelNames(final String name, final ModelNameTest testCase)
266266
Assert.assertEquals(cm.classname, testCase.expectedClassName);
267267
}
268268

269+
@DataProvider
270+
public static Object[][] enumNames() {
271+
return new Object[][]{
272+
{"VALUE1", "VALUE1"},
273+
{"1", "_1"},
274+
{"1X2", "_1X2"},
275+
{"1x2", "_1X2"}
276+
};
277+
}
278+
279+
@Test(dataProvider = "enumNames", description = "sanitize Enum var names")
280+
public void sanitizeEnumVarNames(final String name, final String expectedName) {
281+
final KotlinClientCodegen codegen = new KotlinClientCodegen();
282+
Assert.assertEquals(codegen.toEnumVarName(name, "String"), expectedName);
283+
284+
}
285+
269286
private static class ModelNameTest {
270287
private String expectedName;
271288
private String expectedClassName;
@@ -280,5 +297,4 @@ private ModelNameTest(String expectedName, String expectedClassName) {
280297
this.expectedClassName = expectedClassName;
281298
}
282299
}
283-
}
284-
300+
}

src/test/java/io/swagger/codegen/v3/generators/kotlin/KotlinClientCodegenOptionsTest.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@ protected void setExpectations() {
3434
times = 1;
3535
codegen.setSourceFolder(KotlinClientCodegenOptionsProvider.SOURCE_FOLDER);
3636
times = 1;
37-
codegen.setEnumPropertyNaming(KotlinClientCodegenOptionsProvider.ENUM_PROPERTY_NAMING);
38-
times = 1;
3937
codegen.setDateLibrary(KotlinClientCodegenOptionsProvider.DATE_LIBRARY);
4038
}};
4139
}

src/test/java/io/swagger/codegen/v3/generators/options/KotlinClientCodegenOptionsProvider.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ public class KotlinClientCodegenOptionsProvider implements OptionsProvider {
1212
public static final String ARTIFACT_ID = "swagger-kotlin-test";
1313
public static final String GROUP_ID = "io.swagger.tests";
1414
public static final String SOURCE_FOLDER = "./generated/kotlin";
15-
public static final String ENUM_PROPERTY_NAMING = "camelCase";
1615
public static final String DATE_LIBRARY = KotlinClientCodegen.DateLibrary.JAVA8.value;
1716

1817
@Override
@@ -29,7 +28,6 @@ public Map<String, String> createOptions() {
2928
.put(CodegenConstants.ARTIFACT_ID, ARTIFACT_ID)
3029
.put(CodegenConstants.GROUP_ID, GROUP_ID)
3130
.put(CodegenConstants.SOURCE_FOLDER, SOURCE_FOLDER)
32-
.put(CodegenConstants.ENUM_PROPERTY_NAMING, ENUM_PROPERTY_NAMING)
3331
.put(KotlinClientCodegen.DATE_LIBRARY, DATE_LIBRARY)
3432
.build();
3533
}

0 commit comments

Comments
 (0)