use additoinal properties appleyCamelizeFix instead

This commit is contained in:
William Cheng 2024-03-24 23:13:29 +08:00
parent 2cacac8aea
commit 08590cb7f3
4 changed files with 24 additions and 3 deletions

View File

@ -360,4 +360,6 @@ public interface CodegenConfig {
Set<String> getOpenAPIGeneratorIgnoreList(); Set<String> getOpenAPIGeneratorIgnoreList();
void setApplyCamelizeFix(boolean applyCamelizeFix);
} }

View File

@ -317,6 +317,9 @@ public class DefaultCodegen implements CodegenConfig {
// Whether to automatically hardcode params that are considered Constants by OpenAPI Spec // Whether to automatically hardcode params that are considered Constants by OpenAPI Spec
protected boolean autosetConstants = false; protected boolean autosetConstants = false;
// when set to true, apply camelization fix
protected boolean applyCamelizeFix = false;
public boolean getAddSuffixToDuplicateOperationNicknames() { public boolean getAddSuffixToDuplicateOperationNicknames() {
return addSuffixToDuplicateOperationNicknames; return addSuffixToDuplicateOperationNicknames;
} }
@ -6182,7 +6185,7 @@ public class DefaultCodegen implements CodegenConfig {
*/ */
protected String removeNonNameElementToCamelCase(final String name, final String nonNameElementPattern) { protected String removeNonNameElementToCamelCase(final String name, final String nonNameElementPattern) {
if (Boolean.parseBoolean(System.getProperty("openapi.generator.fix.camelize"))) { if (Boolean.parseBoolean(System.getProperty("openapi.generator.fix.camelize"))) {
// new bebahviour with fix // new behaviour with fix
String[] splitString = name.split(nonNameElementPattern); String[] splitString = name.split(nonNameElementPattern);
if (splitString.length > 0) { if (splitString.length > 0) {
@ -8519,6 +8522,8 @@ public class DefaultCodegen implements CodegenConfig {
this.autosetConstants = autosetConstants; this.autosetConstants = autosetConstants;
} }
public void setApplyCamelizeFix(boolean applyCamelizeFix) { this.applyCamelizeFix = applyCamelizeFix; }
/** /**
* This method removes all constant Query, Header and Cookie Params from allParams and sets them as constantParams in the CodegenOperation. * This method removes all constant Query, Header and Cookie Params from allParams and sets them as constantParams in the CodegenOperation.
* The definition of constant is single valued required enum params. * The definition of constant is single valued required enum params.

View File

@ -17,6 +17,7 @@
package org.openapitools.codegen; package org.openapitools.codegen;
import com.fasterxml.jackson.databind.annotation.JsonAppend;
import io.swagger.v3.core.util.Json; import io.swagger.v3.core.util.Json;
import io.swagger.v3.oas.models.OpenAPI; import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.Operation; import io.swagger.v3.oas.models.Operation;
@ -257,6 +258,13 @@ public class DefaultGenerator implements Generator {
System.out.println(SerializerUtils.toJsonString(openAPI)); 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.setApplyCamelizeFix(true);
}
config.processOpts(); config.processOpts();
if (opts != null && opts.getGeneratorSettings() != null) { if (opts != null && opts.getGeneratorSettings() != null) {
config.typeMapping().putAll(opts.getGeneratorSettings().getTypeMappings()); 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"; 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 // A cache of camelized words. The camelize() method is invoked many times with the same
// arguments, this cache is used to optimized performance. // arguments, this cache is used to optimized performance.
private static Cache<Pair<String, CamelizeOption>, String> camelizedWordsCache; private static Cache<Pair<String, CamelizeOption>, String> camelizedWordsCache;
@ -39,6 +42,8 @@ public class StringUtils {
// A cache of escaped words, used to optimize the performance of the escape() method. // A cache of escaped words, used to optimize the performance of the escape() method.
private static Cache<EscapedNameOptions, String> escapedWordsCache; private static Cache<EscapedNameOptions, String> escapedWordsCache;
static { static {
int cacheSize = Integer.parseInt(GlobalSettings.getProperty(NAME_CACHE_SIZE_PROPERTY, "200")); int cacheSize = Integer.parseInt(GlobalSettings.getProperty(NAME_CACHE_SIZE_PROPERTY, "200"));
int cacheExpiry = Integer.parseInt(GlobalSettings.getProperty(NAME_CACHE_EXPIRY_PROPERTY, "5")); int cacheExpiry = Integer.parseInt(GlobalSettings.getProperty(NAME_CACHE_EXPIRY_PROPERTY, "5"));
@ -59,6 +64,7 @@ public class StringUtils {
.expireAfterAccess(cacheExpiry, TimeUnit.SECONDS) .expireAfterAccess(cacheExpiry, TimeUnit.SECONDS)
.ticker(Ticker.systemTicker()) .ticker(Ticker.systemTicker())
.build(); .build();
} }
private static Pattern capitalLetterPattern = Pattern.compile("([A-Z]+)([A-Z][a-z][a-z]+)"); private static Pattern capitalLetterPattern = Pattern.compile("([A-Z]+)([A-Z][a-z][a-z]+)");
@ -137,9 +143,9 @@ public class StringUtils {
String word = pair.getKey(); String word = pair.getKey();
CamelizeOption option = pair.getValue(); CamelizeOption option = pair.getValue();
// Lowercase acronyms at start of word if not UPPERCASE_FIRST_CHAR
Matcher m; Matcher m;
if (Boolean.parseBoolean(System.getProperty("openapi.generator.fix.camelize"))) { // Lowercase acronyms at start of word if not UPPERCASE_FIRST_CHAR
if (applyCamelizeFix) {
m = camelizeUppercaseStartPattern.matcher(word); m = camelizeUppercaseStartPattern.matcher(word);
if (camelizeOption != UPPERCASE_FIRST_CHAR && m.find()) { if (camelizeOption != UPPERCASE_FIRST_CHAR && m.find()) {
word = m.group(1).toLowerCase(Locale.ROOT) + m.group(2); word = m.group(1).toLowerCase(Locale.ROOT) + m.group(2);