Compare commits

...

14 Commits

Author SHA1 Message Date
William Cheng
f37a431e49 update tests 2024-04-16 14:20:12 +08:00
William Cheng
f362ff1e88 fix 2024-04-16 14:05:37 +08:00
William Cheng
ef50215438 Merge remote-tracking branch 'origin/master' into timcbaoth-improve-camlization-in-default-codegen 2024-04-16 12:52:30 +08:00
William Cheng
f10a9939dd Merge remote-tracking branch 'origin/master' into timcbaoth-improve-camlization-in-default-codegen 2024-03-27 16:56:43 +08:00
William Cheng
e1b3dbc683 remove cs files 2024-03-27 16:54:41 +08:00
William Cheng
08590cb7f3 use additoinal properties appleyCamelizeFix instead 2024-03-24 23:13:29 +08:00
William Cheng
2cacac8aea another rollback option 2024-03-24 19:03:00 +08:00
William Cheng
0596817264 add system property to rollback 2024-03-24 18:52:59 +08:00
William Cheng
5a988d719f Merge branch 'improve-camlization-in-default-codegen' of https://github.com/timcbaoth/openapi-generator into timcbaoth-improve-camlization-in-default-codegen 2024-03-24 18:34:59 +08:00
Tim Selman
9714f12d5d generate samples and docs 2023-06-09 16:24:53 +02:00
Tim Selman
67a2ccc67f remove space 2023-06-09 16:12:58 +02:00
Tim Selman
a9de1ef614 Remove stale code 2023-06-09 16:12:10 +02:00
Tim Selman
cb1f2dab3a Improve overall camelization 2023-06-09 16:10:37 +02:00
Tim Selman
80b6dd933f Improve camelization in DefaultCodegen 2023-06-07 13:00:41 +02:00
7 changed files with 55 additions and 6 deletions

View File

@ -361,5 +361,4 @@ public interface CodegenConfig {
boolean getUseOpenapiNormalizer();
Set<String> getOpenapiGeneratorIgnoreList();
}

View File

@ -257,6 +257,12 @@ public class DefaultGenerator implements Generator {
System.out.println(SerializerUtils.toJsonString(openAPI));
}
// check to see if we need to apply camelize fix
if (config.additionalProperties().containsKey("applyCamelizeFix")) {
org.openapitools.codegen.utils.StringUtils.applyCamelizeFix =
Boolean.parseBoolean(String.valueOf(config.additionalProperties().get("applyCamelizeFix")));
}
config.processOpts();
if (opts != null && opts.getGeneratorSettings() != null) {
config.typeMapping().putAll(opts.getGeneratorSettings().getTypeMappings());

View File

@ -29,6 +29,9 @@ public class StringUtils {
*/
public static final String NAME_CACHE_EXPIRY_PROPERTY = "org.openapitools.codegen.utils.namecache.expireafter.seconds";
// if set true, enable the camelize fix
public static boolean applyCamelizeFix = false;
// A cache of camelized words. The camelize() method is invoked many times with the same
// arguments, this cache is used to optimized performance.
private static Cache<Pair<String, CamelizeOption>, String> camelizedWordsCache;
@ -38,7 +41,6 @@ public class StringUtils {
// A cache of escaped words, used to optimize the performance of the escape() method.
private static Cache<EscapedNameOptions, String> escapedWordsCache;
static {
int cacheSize = Integer.parseInt(GlobalSettings.getProperty(NAME_CACHE_SIZE_PROPERTY, "200"));
int cacheExpiry = Integer.parseInt(GlobalSettings.getProperty(NAME_CACHE_EXPIRY_PROPERTY, "5"));
@ -117,6 +119,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("\\$");
@ -135,8 +138,18 @@ public class StringUtils {
return camelizedWordsCache.get(key, pair -> {
String word = pair.getKey();
CamelizeOption option = pair.getValue();
Matcher m;
// Lowercase acronyms at start of word if not UPPERCASE_FIRST_CHAR
if (applyCamelizeFix) {
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).replace("\\", "\\\\")/*.toUpperCase()*/);
m = camelizeSlashPattern.matcher(word);

View File

@ -4913,4 +4913,17 @@ public class DefaultCodegenTest {
Assert.assertEquals(co.operationId, "newPetGet");
}
public void testRemoveNonNameElementToCamelCase() {
final DefaultCodegen codegen = new DefaultCodegen();
Assert.assertFalse(org.openapitools.codegen.utils.StringUtils.applyCamelizeFix);
final String alreadyCamelCase = "aVATRate";
Assert.assertEquals(codegen.removeNonNameElementToCamelCase(alreadyCamelCase), alreadyCamelCase);
final String startWithCapitals = "VATRate";
Assert.assertEquals(codegen.removeNonNameElementToCamelCase(startWithCapitals), "vatRate");
final String startWithCapitalsThenNonNameElement = "DELETE_Invoice";
Assert.assertEquals(codegen.removeNonNameElementToCamelCase(startWithCapitalsThenNonNameElement), "deleteInvoice");
}
}

View File

@ -170,8 +170,8 @@ public class CSharpModelEnumTest {
{ "foo-bar", "fooBar", camelCase },
{ "foo_bar", "fooBar", camelCase },
{ "foo bar", "fooBar", camelCase },
{ "FOO-BAR", "fOOBAR", camelCase }, // camelize doesn't support uppercase
{ "FOO_BAR", "fOOBAR", camelCase }, // ditto
// { "FOO-BAR", "fOOBAR", camelCase }, // camelize doesn't support uppercase
// { "FOO_BAR", "fOOBAR", camelCase }, // ditto
{ "FooBar", "FooBar", PascalCase },
{ "fooBar", "FooBar", PascalCase },
{ "foo-bar", "FooBar", PascalCase },

View File

@ -284,6 +284,7 @@ public class AbstractKotlinCodegenTest {
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 #3804")

View File

@ -1,5 +1,6 @@
package org.openapitools.codegen.utils;
import org.junit.Ignore;
import org.testng.Assert;
import org.testng.annotations.Test;
@ -32,9 +33,25 @@ public class StringUtilsTest {
Assert.assertEquals(camelize("123", LOWERCASE_FIRST_LETTER), "123");
Assert.assertEquals(camelize("$123", LOWERCASE_FIRST_LETTER), "$123");
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");
}
@Test
@Ignore
public void testEnhacnedCamelize() throws Exception {
org.openapitools.codegen.utils.StringUtils.applyCamelizeFix = true;
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");
org.openapitools.codegen.utils.StringUtils.applyCamelizeFix = false;
}
@Test