Improve overall camelization

This commit is contained in:
Tim Selman 2023-06-09 16:10:37 +02:00
parent 80b6dd933f
commit cb1f2dab3a
5 changed files with 28 additions and 5 deletions

View File

@ -60,6 +60,7 @@ import org.openapitools.codegen.model.OperationsMap;
import org.openapitools.codegen.serializer.SerializerUtils;
import org.openapitools.codegen.templating.MustacheEngineAdapter;
import org.openapitools.codegen.templating.mustache.*;
import org.openapitools.codegen.utils.CamelizeOption;
import org.openapitools.codegen.utils.ModelUtils;
import org.openapitools.codegen.utils.OneOfImplementorAdditionalData;
import org.slf4j.Logger;
@ -5913,7 +5914,7 @@ public class DefaultCodegen implements CodegenConfig {
String[] splitString = name.split(nonNameElementPattern);
if (splitString.length > 0) {
splitString[0] = lowerStartOfWord(splitString[0]);
splitString[0] = camelize(splitString[0], CamelizeOption.LOWERCASE_FIRST_CHAR);
}
String result = Arrays.stream(splitString)
@ -5925,6 +5926,9 @@ public class DefaultCodegen implements CodegenConfig {
return result;
}
private static Pattern capitalLetterPattern = Pattern.compile("([A-Z]+)([A-Z][a-z][a-z]+)");
/**
* Puts the first letters to lowercase. If the word starts with multiple capital letters, all of them
* will be converted to lowercase.

View File

@ -118,6 +118,7 @@ public class StringUtils {
private static Pattern camelizeSlashPattern = Pattern.compile("\\/(.?)");
private static Pattern camelizeUppercasePattern = Pattern.compile("(\\.?)(\\w)([^\\.]*)$");
private static Pattern camelizeUppercaseStartPattern = Pattern.compile("^([A-Z]+)(([A-Z][a-z].*)|([^a-zA-Z].*)|$)$");
private static Pattern camelizeUnderscorePattern = Pattern.compile("(_)(.)");
private static Pattern camelizeHyphenPattern = Pattern.compile("(-)(.)");
private static Pattern camelizeDollarPattern = Pattern.compile("\\$");
@ -136,8 +137,15 @@ public class StringUtils {
return camelizedWordsCache.get(key, pair -> {
String word = pair.getKey();
CamelizeOption option = pair.getValue();
// Lowercase acronyms at start of word if not UPPERCASE_FIRST_CHAR
Matcher m = camelizeUppercaseStartPattern.matcher(word);
if (camelizeOption != UPPERCASE_FIRST_CHAR && m.find()) {
word = m.group(1).toLowerCase(Locale.ROOT) + m.group(2);
}
// Replace all slashes with dots (package separator)
Matcher m = camelizeSlashPattern.matcher(word);
m = camelizeSlashPattern.matcher(word);
while (m.find()) {
word = m.replaceFirst("." + m.group(1)/*.toUpperCase()*/);
m = camelizeSlashPattern.matcher(word);

View File

@ -4730,7 +4730,10 @@ public class DefaultCodegenTest {
final String alreadyCamelCase = "aVATRate";
Assert.assertEquals(codegen.removeNonNameElementToCamelCase(alreadyCamelCase), alreadyCamelCase);
final String startWithCapitals = "DELETE_Invoice";
Assert.assertEquals(codegen.removeNonNameElementToCamelCase(startWithCapitals), "deleteInvoice");
final String startWithCapitals = "VATRate";
Assert.assertEquals(codegen.removeNonNameElementToCamelCase(startWithCapitals), "vatRate");
final String startWithCapitalsThenNonNameElement = "DELETE_Invoice";
Assert.assertEquals(codegen.removeNonNameElementToCamelCase(startWithCapitalsThenNonNameElement), "deleteInvoice");
}
}

View File

@ -278,7 +278,7 @@ public class AbstractKotlinCodegenTest {
// Assert the enum default value is properly generated
CodegenProperty cp1 = cm1.vars.get(0);
Assert.assertEquals(cp1.getEnumName(), "PropertyName");
Assert.assertEquals(cp1.getDefaultValue(), "PropertyName.vALUE");
Assert.assertEquals(cp1.getDefaultValue(), "PropertyName.`value`");
}
@Test(description = "Issue #10792")

View File

@ -35,6 +35,14 @@ public class StringUtilsTest {
Assert.assertEquals(camelize("some-value", LOWERCASE_FIRST_CHAR), "someValue");
Assert.assertEquals(camelize("$type", LOWERCASE_FIRST_CHAR), "$Type");
Assert.assertEquals(camelize("aVATRate", LOWERCASE_FIRST_CHAR), "aVATRate");
Assert.assertEquals(camelize("VATRate", LOWERCASE_FIRST_CHAR), "vatRate");
Assert.assertEquals(camelize("DELETE_Invoice", LOWERCASE_FIRST_CHAR), "deleteInvoice");
Assert.assertEquals(camelize("aVATRate"), "AVATRate");
Assert.assertEquals(camelize("VATRate"), "VATRate");
Assert.assertEquals(camelize("DELETE_Invoice"), "DELETEInvoice");
}
@Test