mirror of
https://github.com/OpenAPITools/openapi-generator.git
synced 2025-06-28 11:40:52 +00:00
Improve camelization in DefaultCodegen
This commit is contained in:
parent
fbe768bb9c
commit
80b6dd933f
@ -15,7 +15,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.openapitools.codegen;
|
||||
package org.openapitools.codegen;
|
||||
|
||||
import com.github.benmanes.caffeine.cache.Cache;
|
||||
import com.github.benmanes.caffeine.cache.Caffeine;
|
||||
@ -5910,7 +5910,13 @@ public class DefaultCodegen implements CodegenConfig {
|
||||
* @return camelized string
|
||||
*/
|
||||
protected String removeNonNameElementToCamelCase(final String name, final String nonNameElementPattern) {
|
||||
String result = Arrays.stream(name.split(nonNameElementPattern))
|
||||
String[] splitString = name.split(nonNameElementPattern);
|
||||
|
||||
if (splitString.length > 0) {
|
||||
splitString[0] = lowerStartOfWord(splitString[0]);
|
||||
}
|
||||
|
||||
String result = Arrays.stream(splitString)
|
||||
.map(StringUtils::capitalize)
|
||||
.collect(Collectors.joining(""));
|
||||
if (result.length() > 0) {
|
||||
@ -5919,6 +5925,29 @@ public class DefaultCodegen implements CodegenConfig {
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Puts the first letters to lowercase. If the word starts with multiple capital letters, all of them
|
||||
* will be converted to lowercase.
|
||||
* @param name string to be changed
|
||||
* @return string starting with lowercase
|
||||
*/
|
||||
private String lowerStartOfWord(final String name) {
|
||||
final StringBuilder result = new StringBuilder();
|
||||
boolean isStartingBlock = true;
|
||||
for (int i = 0; i < name.length(); i++) {
|
||||
char current = name.charAt(i);
|
||||
if (isStartingBlock && Character.isUpperCase(current)) {
|
||||
current = Character.toLowerCase(current);
|
||||
} else {
|
||||
isStartingBlock = false;
|
||||
}
|
||||
|
||||
result.append(current);
|
||||
}
|
||||
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String apiFilename(String templateName, String tag) {
|
||||
String suffix = apiTemplateFiles().get(templateName);
|
||||
|
@ -4722,4 +4722,15 @@ public class DefaultCodegenTest {
|
||||
|
||||
Assert.assertTrue(codegen.cliOptions.contains(expected));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRemoveNonNameElementToCamelCase() {
|
||||
final DefaultCodegen codegen = new DefaultCodegen();
|
||||
|
||||
final String alreadyCamelCase = "aVATRate";
|
||||
Assert.assertEquals(codegen.removeNonNameElementToCamelCase(alreadyCamelCase), alreadyCamelCase);
|
||||
|
||||
final String startWithCapitals = "DELETE_Invoice";
|
||||
Assert.assertEquals(codegen.removeNonNameElementToCamelCase(startWithCapitals), "deleteInvoice");
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user