[Python/General] Underscore Conversion Method Does not Work with Acronyms
Description
In DefaultCodegen.java the underscore method does not work with acronyms, initialisms, or any multi capital letter phrases. This is because these lines of code:
// Replace capital letter with _ plus lowercase letter.
word = word.replaceAll(firstPattern, replacementPattern);
word = word.replaceAll(secondPattern, replacementPattern);
are designed to add an underscore before each capital letter (ignoring the first letter). For example, SomeIDs would be converted to some_i_ds instead of some_ids.
I discovered this bug while generating a Python client but presumably this issue exists in other languages.
Swagger-codegen version
2.4.0.SNAPSHOT up to date with PR 8748 (September 26th).
Swagger declaration file content or url
A model class similar to:
"SomeIDs": {
"type": "object",
"properties": {
"someIDs": {
"type": "array",
"items": {
"type": "integer",
"format": "int32"
}
}
}
}
in a Swagger schema will produce the bug.
Command line used for generation
The standard python client generation command will produce this bug.
Steps to reproduce
- Generate Python Client from Swagger schema containing an acronym, initialisms, or any multi capital letter phrases
- Look at corresponding name in Python client.
Related issues/PRs
Suggest a fix/enhancement
Changing the logic to place the underscores at the end of a series of capital letters instead of after each capital letter will solve this issue.
Got around to looking at this issue. The cause of this issue is that one of the regex patterns used to add underscores to a String:
String firstPattern = "([A-Z]+)([A-Z][a-z])";
is designed to create the issue mentioned in the op. I'm not sure why there needs to be a regex pattern looking for instances of multiple upper case letters follow by an alphabetical letter but this won't work with acronyms.
From my point of view this regex pattern is a bug but the underscore method is used by multiple languages in Codegen and I'm not familiar with the naming conventions for all of the languages using this method. Maybe there's a case where this regex pattern is needed that I'm not aware of.