Skip to content

Improve camelization for words starting with acronym #15798

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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
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;
Expand Down Expand Up @@ -5910,7 +5911,13 @@ public String removeNonNameElementToCamelCase(String name) {
* @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] = camelize(splitString[0], CamelizeOption.LOWERCASE_FIRST_CHAR);
}

String result = Arrays.stream(splitString)
.map(StringUtils::capitalize)
.collect(Collectors.joining(""));
if (result.length() > 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ public static String camelize(String word) {

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("\\$");
Expand All @@ -136,8 +137,15 @@ public static String camelize(final String inputWord, CamelizeOption camelizeOpt
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4722,4 +4722,18 @@ public void testAddOptionEnumValuesNull() {

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 = "VATRate";
Assert.assertEquals(codegen.removeNonNameElementToCamelCase(startWithCapitals), "vatRate");

final String startWithCapitalsThenNonNameElement = "DELETE_Invoice";
Assert.assertEquals(codegen.removeNonNameElementToCamelCase(startWithCapitalsThenNonNameElement), "deleteInvoice");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ public void testEnumPropertyWithDefaultValue() {
// 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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ public void testCamelize() throws Exception {

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
Expand Down
2 changes: 1 addition & 1 deletion samples/client/echo_api/java/apache-httpclient/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ Class | Method | HTTP request | Description
*BodyApi* | [**testEchoBodyTagResponseString**](docs/BodyApi.md#testEchoBodyTagResponseString) | **POST** /echo/body/Tag/response_string | Test empty json (request body)
*FormApi* | [**testFormIntegerBooleanString**](docs/FormApi.md#testFormIntegerBooleanString) | **POST** /form/integer/boolean/string | Test form parameter(s)
*HeaderApi* | [**testHeaderIntegerBooleanString**](docs/HeaderApi.md#testHeaderIntegerBooleanString) | **GET** /header/integer/boolean/string | Test header parameter(s)
*PathApi* | [**testsPathStringPathStringIntegerPathInteger**](docs/PathApi.md#testsPathStringPathStringIntegerPathInteger) | **GET** /path/string/{path_string}/integer/{path_integer} | Test path parameter(s)
*PathApi* | [**testsPathStringpathStringIntegerPathInteger**](docs/PathApi.md#testsPathStringpathStringIntegerPathInteger) | **GET** /path/string/{path_string}/integer/{path_integer} | Test path parameter(s)
*QueryApi* | [**testEnumRefString**](docs/QueryApi.md#testEnumRefString) | **GET** /query/enum_ref_string | Test query parameter(s)
*QueryApi* | [**testQueryDatetimeDateString**](docs/QueryApi.md#testQueryDatetimeDateString) | **GET** /query/datetime/date/string | Test query parameter(s)
*QueryApi* | [**testQueryIntegerBooleanString**](docs/QueryApi.md#testQueryIntegerBooleanString) | **GET** /query/integer/boolean/string | Test query parameter(s)
Expand Down
10 changes: 5 additions & 5 deletions samples/client/echo_api/java/apache-httpclient/docs/PathApi.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ All URIs are relative to *http://localhost:3000*

| Method | HTTP request | Description |
|------------- | ------------- | -------------|
| [**testsPathStringPathStringIntegerPathInteger**](PathApi.md#testsPathStringPathStringIntegerPathInteger) | **GET** /path/string/{path_string}/integer/{path_integer} | Test path parameter(s) |
| [**testsPathStringpathStringIntegerPathInteger**](PathApi.md#testsPathStringpathStringIntegerPathInteger) | **GET** /path/string/{path_string}/integer/{path_integer} | Test path parameter(s) |



## testsPathStringPathStringIntegerPathInteger
## testsPathStringpathStringIntegerPathInteger

> String testsPathStringPathStringIntegerPathInteger(pathString, pathInteger)
> String testsPathStringpathStringIntegerPathInteger(pathString, pathInteger)

Test path parameter(s)

Expand All @@ -35,10 +35,10 @@ public class Example {
String pathString = "pathString_example"; // String |
Integer pathInteger = 56; // Integer |
try {
String result = apiInstance.testsPathStringPathStringIntegerPathInteger(pathString, pathInteger);
String result = apiInstance.testsPathStringpathStringIntegerPathInteger(pathString, pathInteger);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling PathApi#testsPathStringPathStringIntegerPathInteger");
System.err.println("Exception when calling PathApi#testsPathStringpathStringIntegerPathInteger");
System.err.println("Status code: " + e.getCode());
System.err.println("Reason: " + e.getResponseBody());
System.err.println("Response headers: " + e.getResponseHeaders());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ public void setApiClient(ApiClient apiClient) {
* @return String
* @throws ApiException if fails to make API call
*/
public String testsPathStringPathStringIntegerPathInteger(String pathString, Integer pathInteger) throws ApiException {
return this.testsPathStringPathStringIntegerPathInteger(pathString, pathInteger, Collections.emptyMap());
public String testsPathStringpathStringIntegerPathInteger(String pathString, Integer pathInteger) throws ApiException {
return this.testsPathStringpathStringIntegerPathInteger(pathString, pathInteger, Collections.emptyMap());
}


Expand All @@ -73,17 +73,17 @@ public String testsPathStringPathStringIntegerPathInteger(String pathString, Int
* @return String
* @throws ApiException if fails to make API call
*/
public String testsPathStringPathStringIntegerPathInteger(String pathString, Integer pathInteger, Map<String, String> additionalHeaders) throws ApiException {
public String testsPathStringpathStringIntegerPathInteger(String pathString, Integer pathInteger, Map<String, String> additionalHeaders) throws ApiException {
Object localVarPostBody = null;

// verify the required parameter 'pathString' is set
if (pathString == null) {
throw new ApiException(400, "Missing the required parameter 'pathString' when calling testsPathStringPathStringIntegerPathInteger");
throw new ApiException(400, "Missing the required parameter 'pathString' when calling testsPathStringpathStringIntegerPathInteger");
}

// verify the required parameter 'pathInteger' is set
if (pathInteger == null) {
throw new ApiException(400, "Missing the required parameter 'pathInteger' when calling testsPathStringPathStringIntegerPathInteger");
throw new ApiException(400, "Missing the required parameter 'pathInteger' when calling testsPathStringpathStringIntegerPathInteger");
}

// create path and map variables
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ public interface PathApi extends ApiClient.Api {
@Headers({
"Accept: text/plain",
})
String testsPathStringPathStringIntegerPathInteger(@Param("pathString") String pathString, @Param("pathInteger") Integer pathInteger);
String testsPathStringpathStringIntegerPathInteger(@Param("pathString") String pathString, @Param("pathInteger") Integer pathInteger);

/**
* Test path parameter(s)
* Similar to <code>testsPathStringPathStringIntegerPathInteger</code> but it also returns the http response headers .
* Similar to <code>testsPathStringpathStringIntegerPathInteger</code> but it also returns the http response headers .
* Test path parameter(s)
* @param pathString (required)
* @param pathInteger (required)
Expand All @@ -40,7 +40,7 @@ public interface PathApi extends ApiClient.Api {
@Headers({
"Accept: text/plain",
})
ApiResponse<String> testsPathStringPathStringIntegerPathIntegerWithHttpInfo(@Param("pathString") String pathString, @Param("pathInteger") Integer pathInteger);
ApiResponse<String> testsPathStringpathStringIntegerPathIntegerWithHttpInfo(@Param("pathString") String pathString, @Param("pathInteger") Integer pathInteger);


}
4 changes: 2 additions & 2 deletions samples/client/echo_api/java/native/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@ Class | Method | HTTP request | Description
*FormApi* | [**testFormIntegerBooleanStringWithHttpInfo**](docs/FormApi.md#testFormIntegerBooleanStringWithHttpInfo) | **POST** /form/integer/boolean/string | Test form parameter(s)
*HeaderApi* | [**testHeaderIntegerBooleanString**](docs/HeaderApi.md#testHeaderIntegerBooleanString) | **GET** /header/integer/boolean/string | Test header parameter(s)
*HeaderApi* | [**testHeaderIntegerBooleanStringWithHttpInfo**](docs/HeaderApi.md#testHeaderIntegerBooleanStringWithHttpInfo) | **GET** /header/integer/boolean/string | Test header parameter(s)
*PathApi* | [**testsPathStringPathStringIntegerPathInteger**](docs/PathApi.md#testsPathStringPathStringIntegerPathInteger) | **GET** /path/string/{path_string}/integer/{path_integer} | Test path parameter(s)
*PathApi* | [**testsPathStringPathStringIntegerPathIntegerWithHttpInfo**](docs/PathApi.md#testsPathStringPathStringIntegerPathIntegerWithHttpInfo) | **GET** /path/string/{path_string}/integer/{path_integer} | Test path parameter(s)
*PathApi* | [**testsPathStringpathStringIntegerPathInteger**](docs/PathApi.md#testsPathStringpathStringIntegerPathInteger) | **GET** /path/string/{path_string}/integer/{path_integer} | Test path parameter(s)
*PathApi* | [**testsPathStringpathStringIntegerPathIntegerWithHttpInfo**](docs/PathApi.md#testsPathStringpathStringIntegerPathIntegerWithHttpInfo) | **GET** /path/string/{path_string}/integer/{path_integer} | Test path parameter(s)
*QueryApi* | [**testEnumRefString**](docs/QueryApi.md#testEnumRefString) | **GET** /query/enum_ref_string | Test query parameter(s)
*QueryApi* | [**testEnumRefStringWithHttpInfo**](docs/QueryApi.md#testEnumRefStringWithHttpInfo) | **GET** /query/enum_ref_string | Test query parameter(s)
*QueryApi* | [**testQueryDatetimeDateString**](docs/QueryApi.md#testQueryDatetimeDateString) | **GET** /query/datetime/date/string | Test query parameter(s)
Expand Down
20 changes: 10 additions & 10 deletions samples/client/echo_api/java/native/docs/PathApi.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ All URIs are relative to *http://localhost:3000*

| Method | HTTP request | Description |
|------------- | ------------- | -------------|
| [**testsPathStringPathStringIntegerPathInteger**](PathApi.md#testsPathStringPathStringIntegerPathInteger) | **GET** /path/string/{path_string}/integer/{path_integer} | Test path parameter(s) |
| [**testsPathStringPathStringIntegerPathIntegerWithHttpInfo**](PathApi.md#testsPathStringPathStringIntegerPathIntegerWithHttpInfo) | **GET** /path/string/{path_string}/integer/{path_integer} | Test path parameter(s) |
| [**testsPathStringpathStringIntegerPathInteger**](PathApi.md#testsPathStringpathStringIntegerPathInteger) | **GET** /path/string/{path_string}/integer/{path_integer} | Test path parameter(s) |
| [**testsPathStringpathStringIntegerPathIntegerWithHttpInfo**](PathApi.md#testsPathStringpathStringIntegerPathIntegerWithHttpInfo) | **GET** /path/string/{path_string}/integer/{path_integer} | Test path parameter(s) |



## testsPathStringPathStringIntegerPathInteger
## testsPathStringpathStringIntegerPathInteger

> String testsPathStringPathStringIntegerPathInteger(pathString, pathInteger)
> String testsPathStringpathStringIntegerPathInteger(pathString, pathInteger)

Test path parameter(s)

Expand All @@ -36,10 +36,10 @@ public class Example {
String pathString = "pathString_example"; // String |
Integer pathInteger = 56; // Integer |
try {
String result = apiInstance.testsPathStringPathStringIntegerPathInteger(pathString, pathInteger);
String result = apiInstance.testsPathStringpathStringIntegerPathInteger(pathString, pathInteger);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling PathApi#testsPathStringPathStringIntegerPathInteger");
System.err.println("Exception when calling PathApi#testsPathStringpathStringIntegerPathInteger");
System.err.println("Status code: " + e.getCode());
System.err.println("Reason: " + e.getResponseBody());
System.err.println("Response headers: " + e.getResponseHeaders());
Expand Down Expand Up @@ -76,9 +76,9 @@ No authorization required
|-------------|-------------|------------------|
| **200** | Successful operation | - |

## testsPathStringPathStringIntegerPathIntegerWithHttpInfo
## testsPathStringpathStringIntegerPathIntegerWithHttpInfo

> ApiResponse<String> testsPathStringPathStringIntegerPathInteger testsPathStringPathStringIntegerPathIntegerWithHttpInfo(pathString, pathInteger)
> ApiResponse<String> testsPathStringpathStringIntegerPathInteger testsPathStringpathStringIntegerPathIntegerWithHttpInfo(pathString, pathInteger)

Test path parameter(s)

Expand All @@ -104,12 +104,12 @@ public class Example {
String pathString = "pathString_example"; // String |
Integer pathInteger = 56; // Integer |
try {
ApiResponse<String> response = apiInstance.testsPathStringPathStringIntegerPathIntegerWithHttpInfo(pathString, pathInteger);
ApiResponse<String> response = apiInstance.testsPathStringpathStringIntegerPathIntegerWithHttpInfo(pathString, pathInteger);
System.out.println("Status code: " + response.getStatusCode());
System.out.println("Response headers: " + response.getHeaders());
System.out.println("Response body: " + response.getData());
} catch (ApiException e) {
System.err.println("Exception when calling PathApi#testsPathStringPathStringIntegerPathInteger");
System.err.println("Exception when calling PathApi#testsPathStringpathStringIntegerPathInteger");
System.err.println("Status code: " + e.getCode());
System.err.println("Response headers: " + e.getResponseHeaders());
System.err.println("Reason: " + e.getResponseBody());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ private String formatExceptionMessage(String operationId, int statusCode, String
* @return String
* @throws ApiException if fails to make API call
*/
public String testsPathStringPathStringIntegerPathInteger(String pathString, Integer pathInteger) throws ApiException {
ApiResponse<String> localVarResponse = testsPathStringPathStringIntegerPathIntegerWithHttpInfo(pathString, pathInteger);
public String testsPathStringpathStringIntegerPathInteger(String pathString, Integer pathInteger) throws ApiException {
ApiResponse<String> localVarResponse = testsPathStringpathStringIntegerPathIntegerWithHttpInfo(pathString, pathInteger);
return localVarResponse.getData();
}

Expand All @@ -107,8 +107,8 @@ public String testsPathStringPathStringIntegerPathInteger(String pathString, Int
* @return ApiResponse&lt;String&gt;
* @throws ApiException if fails to make API call
*/
public ApiResponse<String> testsPathStringPathStringIntegerPathIntegerWithHttpInfo(String pathString, Integer pathInteger) throws ApiException {
HttpRequest.Builder localVarRequestBuilder = testsPathStringPathStringIntegerPathIntegerRequestBuilder(pathString, pathInteger);
public ApiResponse<String> testsPathStringpathStringIntegerPathIntegerWithHttpInfo(String pathString, Integer pathInteger) throws ApiException {
HttpRequest.Builder localVarRequestBuilder = testsPathStringpathStringIntegerPathIntegerRequestBuilder(pathString, pathInteger);
try {
HttpResponse<InputStream> localVarResponse = memberVarHttpClient.send(
localVarRequestBuilder.build(),
Expand All @@ -118,7 +118,7 @@ public ApiResponse<String> testsPathStringPathStringIntegerPathIntegerWithHttpIn
}
try {
if (localVarResponse.statusCode()/ 100 != 2) {
throw getApiException("testsPathStringPathStringIntegerPathInteger", localVarResponse);
throw getApiException("testsPathStringpathStringIntegerPathInteger", localVarResponse);
}
// for plain text response
if (localVarResponse.headers().map().containsKey("Content-Type") &&
Expand All @@ -144,14 +144,14 @@ public ApiResponse<String> testsPathStringPathStringIntegerPathIntegerWithHttpIn
}
}

private HttpRequest.Builder testsPathStringPathStringIntegerPathIntegerRequestBuilder(String pathString, Integer pathInteger) throws ApiException {
private HttpRequest.Builder testsPathStringpathStringIntegerPathIntegerRequestBuilder(String pathString, Integer pathInteger) throws ApiException {
// verify the required parameter 'pathString' is set
if (pathString == null) {
throw new ApiException(400, "Missing the required parameter 'pathString' when calling testsPathStringPathStringIntegerPathInteger");
throw new ApiException(400, "Missing the required parameter 'pathString' when calling testsPathStringpathStringIntegerPathInteger");
}
// verify the required parameter 'pathInteger' is set
if (pathInteger == null) {
throw new ApiException(400, "Missing the required parameter 'pathInteger' when calling testsPathStringPathStringIntegerPathInteger");
throw new ApiException(400, "Missing the required parameter 'pathInteger' when calling testsPathStringpathStringIntegerPathInteger");
}

HttpRequest.Builder localVarRequestBuilder = HttpRequest.newBuilder();
Expand Down
Loading