Skip to content

Commit a0172e1

Browse files
committed
refactor: introduce shared regex patterns for improved code readability
1 parent 15fc1f5 commit a0172e1

1 file changed

Lines changed: 69 additions & 73 deletions

File tree

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

Lines changed: 69 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,6 @@ public class DefaultCodegen implements CodegenConfig {
9898

9999
private final Logger LOGGER = LoggerFactory.getLogger(DefaultCodegen.class);
100100

101-
public static final Pattern SPLIT_ON_SEMICOLON_OR_NEWLINE_REGEX = Pattern.compile("\\s*(;|\\r?\\n)\\s*"); // Splits on semicolon or new line, ignoring surrounding whitespace
102-
103101
public static FeatureSet DefaultFeatureSet;
104102

105103
// A cache of sanitized words. The sanitizeName() method is invoked many times with the same
@@ -245,109 +243,107 @@ apiTemplateFiles are for API outputs only (controllers/handlers).
245243
// sort operations by default
246244
protected boolean skipSortingOperations = false;
247245

246+
// --- MIME type patterns ---
248247
protected final static Pattern XML_MIME_PATTERN = Pattern.compile("(?i)application/(.*)[+]?xml(;.*)?");
249248
protected final static Pattern JSON_MIME_PATTERN = Pattern.compile("(?i)application/json(;.*)?");
250249
protected final static Pattern JSON_VENDOR_MIME_PATTERN = Pattern.compile("(?i)application/vnd.(.*)+json(;.*)?");
251-
private static final Pattern COMMON_PREFIX_ENUM_NAME = Pattern.compile("[a-zA-Z0-9]+\\z");
250+
251+
// --- HTTP / path patterns ---
252+
protected static final Pattern STARTS_WITH_SLASH = Pattern.compile("^/.*");
253+
protected static final Pattern UNESCAPED_SLASH = Pattern.compile("(?<!\\\\)/");
254+
protected static final Pattern LEFT_CURLY_BRACE = Pattern.compile("\\{");
255+
protected static final Pattern RIGHT_CURLY_BRACE = Pattern.compile("}");
256+
protected static final Pattern PATH_PARAMETER = Pattern.compile("\\{([^}]+)}");
257+
/** Matches a path segment that is entirely a parameter placeholder, e.g. {@code {id}}. */
258+
protected static final Pattern IS_PATH_PARAM = Pattern.compile("^\\{.*}$");
252259
/**
253-
* Matches a trailing run of digits at the end of a name, used by {@link #generateNextName}.
260+
* Matches a callback path-expression parameter like {@code {$request.body#/id}}.
254261
*/
255-
protected static final Pattern TRAILING_DIGITS = Pattern.compile("\\d+\\z");
262+
protected static final Pattern CALLBACK_EXPRESSION_PARAM = Pattern.compile("\\{\\$.*}");
263+
/** Matches a 2xx HTTP success status code (e.g. {@code 200}, {@code 201}). */
264+
protected static final Pattern HTTP_2XX_CODE = Pattern.compile("2[0-9][0-9]");
265+
266+
// --- Whitespace & line-ending patterns ---
267+
protected static final Pattern NEWLINE = Pattern.compile("\n");
268+
protected static final Pattern TRAILING_NEWLINE = Pattern.compile("\n$");
269+
protected static final Pattern WHITESPACE = Pattern.compile("\\s+");
270+
/** Matches one or more trailing whitespace characters in an identifier. */
271+
protected static final Pattern TRAILING_WHITESPACE = Pattern.compile("\\s+$");
272+
/** Matches any string that ends with at least one whitespace character. */
273+
protected static final Pattern ENDS_WITH_WHITESPACE = Pattern.compile(".*\\s$");
256274
/**
257-
* Matches one or more non-word characters; used in {@link #toEnumVarName} and {@link #sanitizeName}.
275+
* Matches tab, newline, or carriage-return; used in {@link #escapeText}.
258276
*/
259-
protected static final Pattern NON_WORD_PLUS = Pattern.compile("\\W+");
277+
protected static final Pattern CONTROL_WHITESPACE = Pattern.compile("[\\t\\n\\r]");
278+
protected static final Pattern MULTILINE_STRING = Pattern.compile("\r\n|\r|\n");
279+
/** Splits on semicolon or new line, ignoring surrounding whitespace. */
280+
protected static final Pattern SPLIT_ON_SEMICOLON_OR_NEWLINE_REGEX = Pattern.compile("\\s*(;|\\r?\\n)\\s*");
281+
/** Splits on line-endings, both Windows ({@code \r\n}) and Unix ({@code \n}). */
282+
public static final Pattern SPLIT_ON_NEWLINE = Pattern.compile("\\r?\\n");
283+
284+
// --- Underscore patterns ---
260285
protected static final Pattern LEADING_UNDERSCORES = Pattern.compile("^_*");
261286
protected static final Pattern MULTI_UNDERSCORES = Pattern.compile("_+");
262287
protected static final Pattern MULTI_TRAILING_UNDERSCORES = Pattern.compile("_+$");
263288
protected static final Pattern MULTI_LEADING_UNDERSCORES = Pattern.compile("^_+");
264289
protected static final Pattern FIRST_LEADING_UNDERSCORE = Pattern.compile("^_");
265290
protected static final Pattern LAST_TRAILING_UNDERSCORE = Pattern.compile("_$");
266-
protected static final Pattern NEWLINE = Pattern.compile("\n");
267-
protected static final Pattern TRAILING_NEWLINE = Pattern.compile("\n$");
268-
protected static final Pattern LEFT_CURLY_BRACE = Pattern.compile("\\{");
269-
protected static final Pattern RIGHT_CURLY_BRACE = Pattern.compile("}");
270-
271-
272-
protected static final Pattern WHITESPACE = Pattern.compile("\\s+");
273-
274-
protected static final Pattern STARTS_WITH_SLASH = Pattern.compile("^/.*");
275-
276-
protected static final Pattern UNESCAPED_SLASH = Pattern.compile("(?<!\\\\)/");
277-
protected static final Pattern TRAILING_BACKSLASHES = Pattern.compile("\\\\+$");
278-
protected static final Pattern ENCLOSING_QUOTES = Pattern.compile("^\"|\"$");
279291

292+
// --- Hyphen & separator patterns ---
293+
protected static final Pattern MINUS = Pattern.compile("-");
294+
/** Matches two or more consecutive hyphens. */
295+
protected static final Pattern MULTI_HYPHEN = Pattern.compile("-{2,}");
296+
/** Matches one or more leading hyphens (at start of string). */
297+
protected static final Pattern LEADING_HYPHENS = Pattern.compile("^-+");
298+
/** Matches one or more trailing hyphens (at end of string). */
299+
protected static final Pattern TRAILING_HYPHENS = Pattern.compile("-+$");
300+
protected static final Pattern PLUS = Pattern.compile("\\+");
301+
protected static final Pattern DOT = Pattern.compile("\\.");
302+
/** Matches {@code .}, {@code /}, or {@code \} — for package-name-to-path conversion. */
303+
protected static final Pattern PACKAGE_SEPARATOR = Pattern.compile("[./\\\\]");
304+
/** Splits on {@code | } (the union-type pipe separator), e.g. {@code TypeA | TypeB}. */
305+
protected static final Pattern SPLIT_ON_PIPE = Pattern.compile(" \\| ");
306+
/** Splits on {@code |} used as a single-char separator in rule strings. */
307+
protected static final Pattern SPLIT_ON_PIPE_CHAR = Pattern.compile("[|]");
280308

309+
// --- Identifier & name-sanitization patterns ---
281310
/**
282311
* Matches a string that starts with a digit (anchored); used across language generators.
283312
*/
284313
protected static final Pattern STARTS_WITH_DIGIT = Pattern.compile("^\\d.*");
285-
286-
314+
/**
315+
* Matches a trailing run of digits at the end of a name, used by {@link #generateNextName}.
316+
*/
317+
protected static final Pattern TRAILING_DIGITS = Pattern.compile("\\d+\\z");
318+
/** Matches strings consisting entirely of ASCII digits. */
319+
protected static final Pattern DIGITS_ONLY = Pattern.compile("^\\d+$");
287320
/**
288321
* Matches a string consisting entirely of uppercase letters and underscores.
289322
*/
290323
protected static final Pattern ALL_UPPER_UNDERSCORE = Pattern.compile("^[A-Z_]*$");
291-
protected static final Pattern NON_WORD_CHAR = Pattern.compile("[^a-zA-Z0-9_]");
292-
protected static final Pattern DASH_UNDERSCORE_SPACE_COLON_PARENTHESES = Pattern.compile("[-_ :()]");
293-
294-
protected static final Pattern MINUS = Pattern.compile("-");
295-
protected static final Pattern PLUS = Pattern.compile("\\+");
296-
protected static final Pattern DOT = Pattern.compile("\\.");
297-
298-
protected static final Pattern PATH_PARAMETER = Pattern.compile("\\{([^}]+)}");
299-
300324
/**
301-
* Matches a string consisting entirely of uppercase letters and underscores and digits.
325+
* Matches a string consisting entirely of uppercase letters, underscores, and digits.
302326
*/
303327
protected static final Pattern ALL_UPPER_UNDERSCORE_DIGITS = Pattern.compile("^[A-Z0-9_]*$");
328+
/** Matches the camelCase word boundary. */
329+
protected static final Pattern CAMEL_CASE_BOUNDARY = Pattern.compile("([a-z0-9])([A-Z])");
330+
/** Matches a CamelCase-initial type name like {@code FooBar} (used in Swift/Kotlin). */
331+
protected static final Pattern CAMEL_INITIAL = Pattern.compile("[A-Z][a-z0-9]+[a-zA-Z0-9]*");
304332
/**
305-
* Matches tab, newline, or carriage-return; used in {@link #escapeText}.
306-
*/
307-
protected static final Pattern CONTROL_WHITESPACE = Pattern.compile("[\\t\\n\\r]");
308-
309-
protected static final Pattern MULTILINE_STRING = Pattern.compile("\r\n|\r|\n");
310-
/**
311-
* Matches a callback path-expression parameter like {@code {$request.body#/id}}.
333+
* Matches one or more non-word characters; used in {@link #toEnumVarName} and {@link #sanitizeName}.
312334
*/
313-
private static final Pattern CALLBACK_EXPRESSION_PARAM = Pattern.compile("\\{\\$.*}");
314-
315-
// --- Shared patterns reused across multiple language generators ---
316-
317-
/** Splits on {@code | } (the union-type pipe separator), e.g. {@code TypeA | TypeB}. */
318-
public static final Pattern SPLIT_ON_PIPE = Pattern.compile(" \\| ");
319-
/** Splits on {@code |} used as a single-char separator in rule strings. */
320-
protected static final Pattern SPLIT_ON_PIPE_CHAR = Pattern.compile("[|]");
321-
/** Splits on line-endings, both Windows ({@code \r\n}) and Unix ({@code \n}). */
322-
public static final Pattern SPLIT_ON_NEWLINE = Pattern.compile("\\r?\\n");
323-
/** Matches the camelCase boundary */
324-
public static final Pattern CAMEL_CASE_BOUNDARY = Pattern.compile("([a-z0-9])([A-Z])");
325-
/** Matches one or more trailing whitespace characters in an identifier. */
326-
protected static final Pattern TRAILING_WHITESPACE = Pattern.compile("\\s+$");
327-
/** Matches any string that ends with at least one whitespace character. */
328-
protected static final Pattern ENDS_WITH_WHITESPACE = Pattern.compile(".*\\s$");
329-
/** Matches strings consisting entirely of ASCII digits. */
330-
protected static final Pattern DIGITS_ONLY = Pattern.compile("^\\d+$");
335+
protected static final Pattern NON_WORD_PLUS = Pattern.compile("\\W+");
336+
protected static final Pattern NON_WORD_CHAR = Pattern.compile("[^a-zA-Z0-9_]");
331337
/** Matches one or more characters that are not ASCII alphanumeric (no underscore). */
332338
protected static final Pattern NON_ALPHANUMERIC = Pattern.compile("[^a-zA-Z0-9]+");
333339
/** Matches exactly one character that is not ASCII alphanumeric (no underscore). */
334340
protected static final Pattern NON_ALPHANUMERIC_CHAR = Pattern.compile("[^a-zA-Z0-9]");
335-
/** Matches {@code .}, {@code /}, or {@code \} — for package-name-to-path conversion. */
336-
protected static final Pattern PACKAGE_SEPARATOR = Pattern.compile("[./\\\\]");
337-
/** Matches a CamelCase-initial type name like {@code FooBar} (used in Swift/Kotlin). */
338-
protected static final Pattern CAMEL_INITIAL = Pattern.compile("[A-Z][a-z0-9]+[a-zA-Z0-9]*");
339-
/** Matches a 2xx HTTP success status code (e.g. {@code 200}, {@code 201}). */
340-
protected static final Pattern HTTP_2XX_CODE = Pattern.compile("2[0-9][0-9]");
341-
/** Matches a path segment that is entirely a parameter placeholder, e.g. {@code {id}}. */
342-
public static final Pattern IS_PATH_PARAM = Pattern.compile("^\\{.*}$");
343-
/** Matches one or more consecutive hyphens. */
344-
protected static final Pattern HYPHENS = Pattern.compile("-+");
345-
/** Matches two or more consecutive hyphens. */
346-
protected static final Pattern MULTI_HYPHEN = Pattern.compile("-{2,}");
347-
/** Matches one or more leading hyphens (at start of string). */
348-
protected static final Pattern LEADING_HYPHENS = Pattern.compile("^-+");
349-
/** Matches one or more trailing hyphens (at end of string). */
350-
protected static final Pattern TRAILING_HYPHENS = Pattern.compile("-+$");
341+
protected static final Pattern DASH_UNDERSCORE_SPACE_COLON_PARENTHESES = Pattern.compile("[-_ :()]");
342+
protected static final Pattern COMMON_PREFIX_ENUM_NAME = Pattern.compile("[a-zA-Z0-9]+\\z");
343+
344+
// --- String literal & escaping patterns ---
345+
protected static final Pattern TRAILING_BACKSLASHES = Pattern.compile("\\\\+$");
346+
protected static final Pattern ENCLOSING_QUOTES = Pattern.compile("^\"|\"$");
351347

352348
/**
353349
* True if the code generator supports multiple class inheritance.

0 commit comments

Comments
 (0)