-
-
Notifications
You must be signed in to change notification settings - Fork 6.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve camelization for words starting with acronym #18218
base: master
Are you sure you want to change the base?
Changes from 9 commits
80b6dd9
cb1f2da
a9de1ef
67a2ccc
9714f12
5a988d7
0596817
2cacac8
08590cb
e1b3dbc
f10a993
ef50215
f362ff1
f37a431
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -317,6 +317,9 @@ apiTemplateFiles are for API outputs only (controllers/handlers). | |
// Whether to automatically hardcode params that are considered Constants by OpenAPI Spec | ||
protected boolean autosetConstants = false; | ||
|
||
// when set to true, apply camelization fix | ||
protected boolean applyCamelizeFix = false; | ||
|
||
public boolean getAddSuffixToDuplicateOperationNicknames() { | ||
return addSuffixToDuplicateOperationNicknames; | ||
} | ||
|
@@ -6181,13 +6184,30 @@ public String removeNonNameElementToCamelCase(String name) { | |
* @return camelized string | ||
*/ | ||
protected String removeNonNameElementToCamelCase(final String name, final String nonNameElementPattern) { | ||
String result = Arrays.stream(name.split(nonNameElementPattern)) | ||
.map(StringUtils::capitalize) | ||
.collect(Collectors.joining("")); | ||
if (result.length() > 0) { | ||
result = result.substring(0, 1).toLowerCase(Locale.ROOT) + result.substring(1); | ||
if (Boolean.parseBoolean(System.getProperty("openapi.generator.fix.camelize"))) { | ||
// new behaviour with fix | ||
String[] splitString = name.split(nonNameElementPattern); | ||
|
||
if (splitString.length > 0) { | ||
splitString[0] = camelize(splitString[0], CamelizeOption.LOWERCASE_FIRST_CHAR); | ||
} | ||
|
||
String result = Arrays.stream(splitString) | ||
.map(StringUtils::capitalize) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why would we capitalize splitString[0] when we just used camelize with lower case first? |
||
.collect(Collectors.joining("")); | ||
if (result.length() > 0) { | ||
result = result.substring(0, 1).toLowerCase(Locale.ROOT) + result.substring(1); | ||
} | ||
return result; | ||
} else { // old behaviour with bug | ||
String result = Arrays.stream(name.split(nonNameElementPattern)) | ||
.map(StringUtils::capitalize) | ||
.collect(Collectors.joining("")); | ||
if (result.length() > 0) { | ||
result = result.substring(0, 1).toLowerCase(Locale.ROOT) + result.substring(1); | ||
} | ||
return result; | ||
} | ||
return result; | ||
} | ||
|
||
@Override | ||
|
@@ -8502,6 +8522,8 @@ public void setAutosetConstants(boolean 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. | ||
* The definition of constant is single valued required enum params. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,7 @@ | |
|
||
package org.openapitools.codegen; | ||
|
||
import com.fasterxml.jackson.databind.annotation.JsonAppend; | ||
import io.swagger.v3.core.util.Json; | ||
import io.swagger.v3.oas.models.OpenAPI; | ||
import io.swagger.v3.oas.models.Operation; | ||
|
@@ -257,6 +258,13 @@ void configureGeneratorProperties() { | |
System.out.println(SerializerUtils.toJsonString(openAPI)); | ||
} | ||
|
||
// check to see if we need to apply camelize fix | ||
if (config.additionalProperties().containsKey("applyCamelizeFix")) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we think the new method is better, then we should make it enabled by default with an option to opt out and use the legacy method. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i want to let users to try it out first to see if we've other edge cases not yet handled. plan to flip the switch in the upcoming v8.0.0 release. |
||
org.openapitools.codegen.utils.StringUtils.applyCamelizeFix = | ||
Boolean.parseBoolean(String.valueOf(config.additionalProperties().get("applyCamelizeFix"))); | ||
config.setApplyCamelizeFix(true); | ||
} | ||
|
||
config.processOpts(); | ||
if (opts != null && opts.getGeneratorSettings() != null) { | ||
config.typeMapping().putAll(opts.getGeneratorSettings().getTypeMappings()); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you change it to this so that it does not mutate the string in unintended ways? We just want to change casing, not strip whitespace.
String[] splitString = name.split(nonNameElementPattern, -1);