Swift3 enum: number variable names fix (#5060)

* Swift3 enum: number variable names fix

Swift3 generator: added 'number' prefix to enum variable names that start with a number

* Fixed Swift3 enum variable names starting with number (prefixed with '_') and added test cases

* Swift3 enum var names: made sure to keep the next word after a number in lower case
This commit is contained in:
rcilia 2017-03-18 05:29:52 -04:00 committed by wing328
parent 4706f4da3e
commit d5cb70f03e
2 changed files with 16 additions and 0 deletions

View File

@ -521,6 +521,15 @@ public class Swift3Codegen extends DefaultCodegen implements CodegenConfig {
return "empty";
}
Pattern startWithNumberPattern = Pattern.compile("^\\d+");
Matcher startWithNumberMatcher = startWithNumberPattern.matcher(name);
if (startWithNumberMatcher.find()) {
String startingNumbers = startWithNumberMatcher.group(0);
String nameWithoutStartingNumbers = name.substring(startingNumbers.length());
return "_" + startingNumbers + camelize(nameWithoutStartingNumbers, true);
}
// for symbol, e.g. $, #
if (getSymbolName(name) != null) {
return camelize(WordUtils.capitalizeFully(getSymbolName(name).toUpperCase()), true);

View File

@ -63,6 +63,13 @@ public class Swift3CodegenTest {
Assert.assertEquals(swiftCodegen.toEnumVarName("entry_name", null), "entryName");
}
@Test
public void testStartingWithNumber() throws Exception {
Assert.assertEquals(swiftCodegen.toEnumVarName("123EntryName", null), "_123entryName");
Assert.assertEquals(swiftCodegen.toEnumVarName("123Entry_name", null), "_123entryName");
Assert.assertEquals(swiftCodegen.toEnumVarName("123EntryName123", null), "_123entryName123");
}
@Test(description = "returns NSData when response format is binary")
public void binaryDataTest() {
final Swagger model = new SwaggerParser().read("src/test/resources/2_0/binaryDataTest.json");