diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/StringUtils.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/StringUtils.java index 9bca83d638bb..9ad3082b8828 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/StringUtils.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/StringUtils.java @@ -117,8 +117,7 @@ public static String camelize(String word) { private static Pattern camelizeSlashPattern = Pattern.compile("\\/(.?)"); private static Pattern camelizeUppercasePattern = Pattern.compile("(\\.?)(\\w)([^\\.]*)$"); - private static Pattern camelizeUnderscorePattern = Pattern.compile("(_)(.)"); - private static Pattern camelizeHyphenPattern = Pattern.compile("(-)(.)"); + private static Pattern camelizeCharsPattern = Pattern.compile("([_ -])(.)"); private static Pattern camelizeDollarPattern = Pattern.compile("\\$"); private static Pattern camelizeSimpleUnderscorePattern = Pattern.compile("_"); @@ -167,7 +166,8 @@ public static String camelize(final String inputWord, boolean lowercaseFirstLett } // Remove all underscores (underscore_case to camelCase) - m = camelizeUnderscorePattern.matcher(word); + // Remove all hyphens (hyphen-case to camelCase) + m = camelizeCharsPattern.matcher(word); while (m.find()) { String original = m.group(2); String upperCase = original.toUpperCase(Locale.ROOT); @@ -176,14 +176,7 @@ public static String camelize(final String inputWord, boolean lowercaseFirstLett } else { word = m.replaceFirst(upperCase); } - m = camelizeUnderscorePattern.matcher(word); - } - - // Remove all hyphens (hyphen-case to camelCase) - m = camelizeHyphenPattern.matcher(word); - while (m.find()) { - word = m.replaceFirst(m.group(2).toUpperCase(Locale.ROOT)); - m = camelizeHyphenPattern.matcher(word); + m = camelizeCharsPattern.matcher(word); } if (lowerFirstLetter && word.length() > 0) { @@ -266,9 +259,9 @@ public static String escape(final String name, final Map replace return character; } }).reduce( (c1, c2) -> "" + c1 + c2).orElse(null); - + if (result != null) return result; - throw new RuntimeException("Word '" + name + "' could not be escaped."); + throw new RuntimeException("Word '" + name + "' could not be escaped."); }); } } diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/utils/StringUtilsTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/utils/StringUtilsTest.java index d4a8efebae59..b554080591f7 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/utils/StringUtilsTest.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/utils/StringUtilsTest.java @@ -19,12 +19,18 @@ public void testCamelize() throws Exception { Assert.assertEquals(camelize("some-value"), "SomeValue"); Assert.assertEquals(camelize("some_value"), "SomeValue"); Assert.assertEquals(camelize("$type"), "$Type"); + Assert.assertEquals(camelize("Upper lower"), "UpperLower"); + Assert.assertEquals(camelize("Upper Upper"), "UpperUpper"); + Assert.assertEquals(camelize("lower Upper"), "LowerUpper"); Assert.assertEquals(camelize("abcd", true), "abcd"); Assert.assertEquals(camelize("some-value", true), "someValue"); Assert.assertEquals(camelize("some_value", true), "someValue"); Assert.assertEquals(camelize("Abcd", true), "abcd"); Assert.assertEquals(camelize("$type", true), "$type"); + Assert.assertEquals(camelize("Upper lower", true), "upperLower"); + Assert.assertEquals(camelize("Upper Upper", true), "upperUpper"); + Assert.assertEquals(camelize("lower Upper", true), "lowerUpper"); Assert.assertEquals(camelize("123", true), "123"); Assert.assertEquals(camelize("$123", true), "$123");