Skip to content

Commit 5c48182

Browse files
committed
fix working with regex patterns
1 parent d74b796 commit 5c48182

13 files changed

Lines changed: 20 additions & 44 deletions

modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -252,11 +252,16 @@ apiTemplateFiles are for API outputs only (controllers/handlers).
252252
/** Matches a trailing run of digits at the end of a name, used by {@link #generateNextName}. */
253253
private static final Pattern TRAILING_DIGITS = Pattern.compile("\\d+\\z");
254254
/** Matches one or more non-word characters; used in {@link #toEnumVarName} and {@link #sanitizeName}. */
255-
private static final Pattern NON_WORD_PLUS = Pattern.compile("\\W+");
255+
protected static final Pattern NON_WORD_PLUS = Pattern.compile("\\W+");
256256
/** Matches a string that starts with a digit; used in {@link #toEnumVarName}. */
257-
private static final Pattern LEADING_DIGIT = Pattern.compile("\\d.*");
257+
protected static final Pattern LEADING_DIGIT = Pattern.compile("\\d.*");
258+
259+
/** Matches a string that starts with a digit (anchored); used across language generators. */
260+
protected static final Pattern STARTS_WITH_DIGIT = Pattern.compile("^\\d.*");
261+
/** Matches a string consisting entirely of uppercase letters and underscores. */
262+
protected static final Pattern ALL_UPPER_UNDERSCORE = Pattern.compile("^[A-Z_]*$");
258263
/** Matches tab, newline, or carriage-return; used in {@link #escapeText}. */
259-
private static final Pattern CONTROL_WHITESPACE = Pattern.compile("[\\t\\n\\r]");
264+
protected static final Pattern CONTROL_WHITESPACE = Pattern.compile("[\\t\\n\\r]");
260265
/** Matches a callback path-expression parameter like {@code {$request.body#/id}}. */
261266
private static final Pattern CALLBACK_EXPRESSION_PARAM = Pattern.compile("\\{\\$.*}");
262267
// Dynamic patterns keyed by user-supplied removeCharRegEx strings are cached via PatternCache.

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractApexCodegen.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,6 @@ public abstract class AbstractApexCodegen extends DefaultCodegen implements Code
4141
private final Logger LOGGER = LoggerFactory.getLogger(AbstractApexCodegen.class);
4242

4343
private static final Pattern LEADING_DIGIT = Pattern.compile("^\\d");
44-
private static final Pattern LEADING_DIGITS_ONLY = Pattern.compile("^\\d.*");
45-
private static final Pattern ALL_UPPER_UNDERSCORE = Pattern.compile("^[A-Z_]*$");
46-
private static final Pattern NON_WORD_PLUS = Pattern.compile("\\W+");
4744
private static final Pattern DATE_FORMAT = Pattern.compile("^\\d{4}(-\\d{2}){2}");
4845
private static final Pattern DATETIME_FORMAT = Pattern.compile("^\\d{4}([-T:]\\d{2}){5}.+");
4946
private static final Pattern NON_NUMERIC = Pattern.compile("[^-0-9.]");
@@ -94,7 +91,7 @@ public String sanitizeName(String name) {
9491
name = name.replace("__", "_");
9592
}
9693
if (LEADING_DIGIT.matcher(name).find()) { // Prevent named credentials with leading number
97-
name = LEADING_DIGITS_ONLY.matcher(name).replaceAll("");
94+
name = STARTS_WITH_DIGIT.matcher(name).replaceAll("");
9895
}
9996
return name;
10097
}

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractCSharpCodegen.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ public abstract class AbstractCSharpCodegen extends DefaultCodegen {
5757
/** Matches a regex literal that carries modifier flags after the closing slash, e.g. {@code /foo/i}. */
5858
private static final Pattern HAS_MODIFIERS = Pattern.compile(".*/[gmiyuvsdlnx]+$");
5959
private static final Pattern LEADING_DIGIT = Pattern.compile("^\\d");
60-
private static final Pattern ALL_UPPER_UNDERSCORE = Pattern.compile("^[A-Z_]*$");
6160
private static final Pattern UNESCAPED_DOUBLE_QUOTE = Pattern.compile("(?<!\\\\)\"");
6261

6362
protected boolean optionalAssemblyInfoFlag = true;

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractCppCodegen.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,10 @@
3838
import java.io.File;
3939
import java.net.URL;
4040
import java.util.*;
41-
import java.util.regex.Pattern;
4241

4342
abstract public class AbstractCppCodegen extends DefaultCodegen implements CodegenConfig {
4443
private final Logger LOGGER = LoggerFactory.getLogger(AbstractCppCodegen.class);
4544

46-
private static final Pattern STARTS_WITH_DIGIT = Pattern.compile("^\\d.*");
4745

4846
protected static final String RESERVED_WORD_PREFIX_OPTION = "reservedWordPrefix";
4947
protected static final String RESERVED_WORD_PREFIX_DESC = "Prefix to prepend to reserved words in order to avoid conflicts";

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractDartCodegen.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@ public abstract class AbstractDartCodegen extends DefaultCodegen {
3737
private final Logger LOGGER = LoggerFactory.getLogger(AbstractDartCodegen.class);
3838

3939
private static final Pattern LEADING_UNDERSCORE = Pattern.compile("^_");
40-
private static final Pattern ALL_UPPER_UNDERSCORE = Pattern.compile("^[A-Z_]*$");
41-
private static final Pattern STARTS_WITH_DIGIT = Pattern.compile("^\\d.*");
4240
private static final Pattern STARTS_WITH_OPT_NEG_DIGIT = Pattern.compile("^-?\\d.*");
4341

4442
protected static final List<String> DEFAULT_SUPPORTED_CONTENT_TYPES = Arrays.asList(

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractEiffelCodegen.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,6 @@
4141
public abstract class AbstractEiffelCodegen extends DefaultCodegen implements CodegenConfig {
4242
private final Logger LOGGER = LoggerFactory.getLogger(AbstractEiffelCodegen.class);
4343

44-
private static final Pattern STARTS_WITH_DIGIT = Pattern.compile("^\\d.*");
45-
private static final Pattern ALL_UPPER_UNDERSCORE = Pattern.compile("^[A-Z_]*$");
4644
private static final Pattern UNCAMELIZE_UPPER = Pattern.compile("(.)(\\p{Upper})");
4745

4846
private final Set<String> parentModels = new HashSet<>();

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractFSharpCodegen.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636

3737
import java.io.File;
3838
import java.util.*;
39-
import java.util.regex.Pattern;
4039

4140
import static org.openapitools.codegen.CodegenConstants.X_ENUM_BYTE;
4241
import static org.openapitools.codegen.utils.CamelizeOption.LOWERCASE_FIRST_LETTER;
@@ -47,9 +46,6 @@ public abstract class AbstractFSharpCodegen extends DefaultCodegen implements Co
4746

4847
// ...existing code...
4948

50-
private static final Pattern STARTS_WITH_DIGIT = Pattern.compile("^\\d.*");
51-
private static final Pattern ALL_UPPER_UNDERSCORE = Pattern.compile("^[A-Z_]*$");
52-
private static final Pattern STARTS_WITH_DIGIT_NO_ANCHOR = Pattern.compile("\\d.*");
5349

5450
protected boolean optionalAssemblyInfoFlag = true;
5551
protected boolean optionalProjectFileFlag = true;
@@ -971,7 +967,7 @@ public String toEnumVarName(String name, String datatype) {
971967

972968
enumName = camelize(enumName) + "Enum";
973969

974-
if (STARTS_WITH_DIGIT_NO_ANCHOR.matcher(enumName).matches()) { // starts with number
970+
if (STARTS_WITH_DIGIT.matcher(enumName).matches()) { // starts with number
975971
return "_" + enumName;
976972
} else {
977973
return enumName;

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractGoCodegen.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ public abstract class AbstractGoCodegen extends DefaultCodegen implements Codege
4141
private static final String NUMERIC_ENUM_PREFIX = "_";
4242

4343
private static final Pattern LEADING_DIGIT = Pattern.compile("^\\d");
44-
private static final Pattern ALL_UPPER_UNDERSCORE = Pattern.compile("^[A-Z_]*$");
4544
private static final Pattern LEADING_TRAILING_SLASH = Pattern.compile("^/|/$");
4645

4746
@Setter

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractGraphQLCodegen.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929

3030
import java.io.File;
3131
import java.util.*;
32-
import java.util.regex.Pattern;
3332

3433
import static org.openapitools.codegen.utils.CamelizeOption.LOWERCASE_FIRST_LETTER;
3534
import static org.openapitools.codegen.utils.StringUtils.camelize;
@@ -39,9 +38,6 @@ public abstract class AbstractGraphQLCodegen extends DefaultCodegen implements C
3938

4039
private final Logger LOGGER = LoggerFactory.getLogger(AbstractGraphQLCodegen.class);
4140

42-
private static final Pattern STARTS_WITH_DIGIT = Pattern.compile("^\\d.*");
43-
private static final Pattern ALL_UPPER_UNDERSCORE = Pattern.compile("^[A-Z_]*$");
44-
private static final Pattern STARTS_WITH_DIGIT_NO_ANCHOR = Pattern.compile("\\d.*");
4541

4642
protected String specFolder = "spec";
4743
@Setter protected String packageName = "openapi2graphql";
@@ -392,7 +388,7 @@ public String toEnumVarName(String name, String datatype) {
392388
enumName = enumName.replaceFirst("^_", "");
393389
enumName = enumName.replaceFirst("_$", "");
394390

395-
if (isReservedWord(enumName) || STARTS_WITH_DIGIT_NO_ANCHOR.matcher(enumName).matches()) { // reserved word or starts with number
391+
if (isReservedWord(enumName) || STARTS_WITH_DIGIT.matcher(enumName).matches()) { // reserved word or starts with number
396392
return escapeReservedWord(enumName);
397393
} else {
398394
return enumName;
@@ -406,7 +402,7 @@ public String toEnumName(CodegenProperty property) {
406402
// remove [] for array or map of enum
407403
enumName = enumName.replace("[]", "");
408404

409-
if (STARTS_WITH_DIGIT_NO_ANCHOR.matcher(enumName).matches()) { // starts with number
405+
if (STARTS_WITH_DIGIT.matcher(enumName).matches()) { // starts with number
410406
return "_" + enumName;
411407
} else {
412408
return enumName;

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractJavaCodegen.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -86,14 +86,10 @@ public abstract class AbstractJavaCodegen extends DefaultCodegen implements Code
8686
private static final Pattern LOMBOK_ANNOTATION = Pattern.compile("@lombok.(\\w+\\.)*(?<ClassName>\\w+)(\\(.*?\\))?");
8787
private static final Pattern JAVA_UTIL_IMPORT = Pattern.compile("java\\.util\\.(List|ArrayList|Map|HashMap)");
8888
private static final Pattern STARTS_WITH_UNDERSCORE_CLASS = Pattern.compile("^_*class$");
89-
private static final Pattern STARTS_WITH_DIGIT = Pattern.compile("^\\d.*");
9089
private static final Pattern ALL_UPPER_CASE_DIGITS_UNDERSCORE = Pattern.compile("^[A-Z0-9_]*$");
91-
private static final Pattern TAB_NEWLINE_RETURN = Pattern.compile("[\\t\\n\\r]");
9290
private static final Pattern ANNOTATION_IN_TYPE = Pattern.compile("(?:(?i)@[a-z0-9]*+([(].*[)]|\\s*))*+");
9391
private static final Pattern NON_ALPHANUMERIC = Pattern.compile("\\P{Alnum}");
94-
private static final Pattern NON_WORD_CHARS = Pattern.compile("\\W+");
9592
private static final Pattern INVALID_PACKAGE_CHARS = Pattern.compile("[^a-zA-Z0-9_.]");
96-
private static final Pattern STARTS_WITH_DIGIT_NO_ANCHOR = Pattern.compile("\\d.*");
9793

9894
public static final String DEFAULT_LIBRARY = "<default>";
9995
public static final String DATE_LIBRARY = "dateLibrary";
@@ -1220,7 +1216,7 @@ private String getStringBeanValidation(Schema<?> items) {
12201216

12211217
if (StringUtils.isNotEmpty(items.getPattern())) {
12221218
final String pattern = escapeUnsafeCharacters(
1223-
TAB_NEWLINE_RETURN.matcher(
1219+
CONTROL_WHITESPACE.matcher(
12241220
StringEscapeUtils.unescapeJava(
12251221
StringEscapeUtils.escapeJava(items.getPattern())
12261222
.replace("\\/", "/"))).replaceAll(" ")
@@ -2273,7 +2269,7 @@ public String toEnumVarName(String value, String datatype) {
22732269
switch (getEnumPropertyNaming()) {
22742270
case legacy:
22752271
// legacy ,e.g. WITHNUMBER1
2276-
var = NON_WORD_CHARS.matcher(value).replaceAll("_").toUpperCase(Locale.ROOT);
2272+
var = NON_WORD_PLUS.matcher(value).replaceAll("_").toUpperCase(Locale.ROOT);
22772273
break;
22782274
case original:
22792275
// keep value as it is, if meets language naming convention
@@ -2286,10 +2282,10 @@ public String toEnumVarName(String value, String datatype) {
22862282
break;
22872283
default:
22882284
// default to MACRO_CASE, e.g. WITH_NUMBER1
2289-
var = underscore(NON_WORD_CHARS.matcher(value).replaceAll("_")).toUpperCase(Locale.ROOT);
2285+
var = underscore(NON_WORD_PLUS.matcher(value).replaceAll("_")).toUpperCase(Locale.ROOT);
22902286
break;
22912287
}
2292-
if (STARTS_WITH_DIGIT_NO_ANCHOR.matcher(var).matches()) {
2288+
if (STARTS_WITH_DIGIT.matcher(var).matches()) {
22932289
var = "_" + var;
22942290
}
22952291
return this.toVarName(var);

0 commit comments

Comments
 (0)