Skip to content

Commit 28e7026

Browse files
committed
refactor: introduce shared regex patterns for improved code readability
1 parent 798b7d4 commit 28e7026

1 file changed

Lines changed: 40 additions & 3 deletions

File tree

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

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,43 @@ apiTemplateFiles are for API outputs only (controllers/handlers).
312312
*/
313313
private static final Pattern CALLBACK_EXPRESSION_PARAM = Pattern.compile("\\{\\$.*}");
314314

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+$");
331+
/** Matches one or more characters that are not ASCII alphanumeric (no underscore). */
332+
protected static final Pattern NON_ALPHANUMERIC = Pattern.compile("[^a-zA-Z0-9]+");
333+
/** Matches exactly one character that is not ASCII alphanumeric (no underscore). */
334+
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("-+$");
351+
315352
/**
316353
* True if the code generator supports multiple class inheritance.
317354
* This is used to model the parent hierarchy based on the 'allOf' composed schemas.
@@ -4413,7 +4450,7 @@ void updateDefaultToEmptyContainer(CodegenProperty cp, Schema p) {
44134450
* @param input a set of rules separated by `|`
44144451
*/
44154452
void parseDefaultToEmptyContainer(String input) {
4416-
String[] inputs = input.split("[|]");
4453+
String[] inputs = SPLIT_ON_PIPE_CHAR.split(input);
44174454
String containerType;
44184455
for (String rule : inputs) {
44194456
if (StringUtils.isEmpty(rule)) {
@@ -5915,7 +5952,7 @@ protected String getOrGenerateOperationId(Operation operation, String path, Stri
59155952
// remove prefix in operationId
59165953
if (removeOperationIdPrefix) {
59175954
// The prefix is everything before the removeOperationIdPrefixCount occurrence of removeOperationIdPrefixDelimiter
5918-
String[] components = operationId.split("[" + removeOperationIdPrefixDelimiter + "]");
5955+
String[] components = PatternCache.get("[" + removeOperationIdPrefixDelimiter + "]").split(operationId);
59195956
if (components.length > 1) {
59205957
// If removeOperationIdPrefixCount is -1 or bigger that the number of occurrences, uses the last one
59215958
int component_number = removeOperationIdPrefixCount == -1 ? components.length - 1 : removeOperationIdPrefixCount;
@@ -7755,7 +7792,7 @@ protected void addBodyModelSchema(CodegenParameter codegenParameter, String name
77557792

77567793
if (codegenProperty != null && codegenProperty.getComplexType() != null && codegenProperty.getComplexType().contains(" | ")) {
77577794
// TODO move this splitting logic to the generator that needs it only
7758-
List<String> parts = Arrays.asList(codegenProperty.getComplexType().split(" \\| "));
7795+
List<String> parts = Arrays.asList(SPLIT_ON_PIPE.split(codegenProperty.getComplexType()));
77597796
imports.addAll(parts);
77607797

77617798
String codegenModelName = codegenProperty.getComplexType();

0 commit comments

Comments
 (0)