Skip to content
Merged
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
Expand Up @@ -988,55 +988,90 @@ public String toEnumVarName(String name, String datatype) {
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());
// Reserved Name
String nameLowercase = StringUtils.lowerCase(name);
if (isReservedWord(nameLowercase)) {
return escapeReservedWord(nameLowercase);
}

return "_" + startingNumbers + camelize(nameWithoutStartingNumbers, true);
// Prefix with underscore if name starts with number
if (name.matches("\\d.*")) {
return "_" + replaceSpecialCharacters(camelize(name, true));
}

// for symbol, e.g. $, #
if (getSymbolName(name) != null) {
return camelize(WordUtils.capitalizeFully(getSymbolName(name).toUpperCase(Locale.ROOT)), true);
}

// Camelize only when we have a structure defined below
Boolean camelized = false;
if (name.matches("[A-Z][a-z0-9]+[a-zA-Z0-9]*")) {
name = camelize(name, true);
camelized = true;
}

// Reserved Name
String nameLowercase = StringUtils.lowerCase(name);
if (isReservedWord(nameLowercase)) {
return escapeReservedWord(nameLowercase);
}

// Check for numerical conversions
if ("Int".equals(datatype) || "Int32".equals(datatype) || "Int64".equals(datatype)
|| "Float".equals(datatype) || "Double".equals(datatype)) {
String varName = "number" + camelize(name);
varName = varName.replaceAll("-", "minus");
varName = varName.replaceAll("\\+", "plus");
varName = varName.replaceAll("\\.", "dot");
return varName;
return replaceSpecialCharacters(varName);
}

// If we have already camelized the word, don't progress
// any further
if (camelized) {
return name;
return replaceSpecialCharacters(name);
}

char[] separators = {'-', '_', ' ', ':', '(', ')'};
return camelize(WordUtils.capitalizeFully(StringUtils.lowerCase(name), separators)
.replaceAll("[-_ :\\(\\)]", ""),
return camelize(replaceSpecialCharacters(WordUtils.capitalizeFully(StringUtils.lowerCase(name), separators)
.replaceAll("[-_ :\\(\\)]", "")),
true);
}

private String replaceSpecialCharacters(String name) {
for (Map.Entry<String, String> specialCharacters : specialCharReplacements.entrySet()) {
String specialChar = specialCharacters.getKey();
String replacement = specialCharacters.getValue();
// Underscore is the only special character we'll allow
if (!specialChar.equals("_") && name.contains(specialChar)) {
name = replaceCharacters(name, specialChar, replacement);
}
}

// Fallback, replace unknowns with underscore.
name = name.replaceAll("\\W+", "_");

return name;
}

private String replaceCharacters(String word, String oldValue, String newValue) {
if (!word.contains(oldValue)) {
return word;
}
if (word.equals(oldValue)) {
return newValue;
}
int i = word.indexOf(oldValue);
String start = word.substring(0, i);
String end = recurseOnEndOfWord(word, oldValue, newValue, i);
return start + newValue + end;
}

private String recurseOnEndOfWord(String word, String oldValue, String newValue, int lastReplacedValue) {
String end = word.substring(lastReplacedValue + 1);
if (!end.isEmpty()) {
end = titleCase(end);
end = replaceCharacters(end, oldValue, newValue);
}
return end;
}

private String titleCase(final String input) {
return input.substring(0, 1).toUpperCase(Locale.ROOT) + input.substring(1);
}

@Override
public String toEnumName(CodegenProperty property) {
String enumName = toModelName(property.name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,16 @@ public void testStartingWithNumber() throws Exception {
Assert.assertEquals(swiftCodegen.toEnumVarName("123EntryName123", null), "_123entryName123");
}

@Test(enabled = true)
public void testSpecialCharacters() throws Exception {
Assert.assertEquals(swiftCodegen.toEnumVarName("1:1", null), "_1Colon1");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mbruegmann the previous changes are related to the changes you make in the Swift5ClientCodegen.java.
An easy way to test it is by adding the following test case here that should pass.

Assert.assertEquals(swiftCodegen.toEnumVarName(">=", null), "greaterThanOrEqualTo");

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems to be an rare edge case. I am doing the replacement character by character now to also replace special characters inside the name. Before only in case of a match of the whole string a replacement was done.
This was the previous code and it would not work for "3=>2" for example. It would only match "=>".

if (getSymbolName(name) != null) { return camelize(WordUtils.capitalizeFully(getSymbolName(name).toUpperCase(Locale.ROOT)), true); }
The problem comes from specialCharReplacements in DefaultCodegen. It's a map with replacements that could be reversed, but the order also is not garanteed.

Assert.assertEquals(swiftCodegen.toEnumVarName("1:One", null), "_1ColonOne");
Assert.assertEquals(swiftCodegen.toEnumVarName("Apple&Swift", null), "appleAmpersandSwift");
Assert.assertEquals(swiftCodegen.toEnumVarName("$", null), "dollar");
Assert.assertEquals(swiftCodegen.toEnumVarName("+1", null), "plus1");
Assert.assertEquals(swiftCodegen.toEnumVarName(">=", null), "greaterThanOrEqualTo");
}

@Test(description = "returns Data when response format is binary", enabled = true)
public void binaryDataTest() {
// TODO update json file
Expand Down