Skip to content

Commit 848b5fc

Browse files
committed
DRY regex patterns
1 parent 55934d7 commit 848b5fc

11 files changed

Lines changed: 43 additions & 33 deletions

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -254,10 +254,13 @@ apiTemplateFiles are for API outputs only (controllers/handlers).
254254
/** Matches one or more non-word characters; used in {@link #toEnumVarName} and {@link #sanitizeName}. */
255255
protected static final Pattern NON_WORD_PLUS = Pattern.compile("\\W+");
256256
protected static final Pattern LEADING_UNDERSCORES = Pattern.compile("^_*");
257+
protected static final Pattern MULTI_UNDERSCORES = Pattern.compile("_+");
258+
protected static final Pattern MULTI_TRAILING_UNDERSCORES = Pattern.compile("_+$");
259+
protected static final Pattern FIRST_LEADING_UNDERSCORE = Pattern.compile("^_");
260+
protected static final Pattern LAST_TRAILING_UNDERSCORE = Pattern.compile("_$");
257261

258-
protected static final Pattern WHITESPACE = Pattern.compile("\\s+");
259262

260-
protected static final Pattern DOUBLE_QUOTE = Pattern.compile("\"");
263+
protected static final Pattern WHITESPACE = Pattern.compile("\\s+");
261264

262265
protected static final Pattern STARTS_WITH_SLASH = Pattern.compile("^/.*");
263266

@@ -266,16 +269,16 @@ apiTemplateFiles are for API outputs only (controllers/handlers).
266269
/** Matches a string that starts with a digit (anchored); used across language generators. */
267270
protected static final Pattern STARTS_WITH_DIGIT = Pattern.compile("^\\d.*");
268271

269-
protected static final Pattern FIRST_LEADING_UNDERSCORE = Pattern.compile("^_");
270272

271-
protected static final Pattern LAST_TRAILING_UNDERSCORE = Pattern.compile("_$");
272273
/** Matches a string consisting entirely of uppercase letters and underscores. */
273274
protected static final Pattern ALL_UPPER_UNDERSCORE = Pattern.compile("^[A-Z_]*$");
274275

275276
protected static final Pattern MINUS = Pattern.compile("-");
276277
protected static final Pattern PLUS = Pattern.compile("\\+");
277278
protected static final Pattern DOT = Pattern.compile("\\.");
278279

280+
protected static final Pattern PATH_PARAMETER = Pattern.compile("\\{(.*?)}");
281+
279282
/** Matches a string consisting entirely of uppercase letters and underscores and digits. */
280283
protected static final Pattern ALL_UPPER_UNDERSCORE_DIGITS = Pattern.compile("^[A-Z0-9_]*$");
281284
/** Matches tab, newline, or carriage-return; used in {@link #escapeText}. */

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1401,7 +1401,7 @@ public String toEnumVarName(String name, String datatype) {
14011401
if ("int".equals(datatype) || "float".equals(datatype)) {
14021402
return name;
14031403
} else {
1404-
return "\'" + name + "\'";
1404+
return "'" + name + "'";
14051405
}
14061406
}
14071407

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import java.io.File;
4747
import java.io.IOException;
4848
import java.util.*;
49+
import java.util.regex.Pattern;
4950

5051
import static org.openapitools.codegen.CodegenConstants.X_MODIFIERS;
5152
import static org.openapitools.codegen.CodegenConstants.X_REGEX;
@@ -71,6 +72,8 @@ public void serialize(Boolean value, JsonGenerator gen, SerializerProvider seria
7172
public static final String MOVE_TESTS_UNDER_PYTHON_SRC_ROOT = "testsUsePythonSrcRoot";
7273
static final String MEDIA_TYPE = "mediaType";
7374

75+
Pattern TRAILING_SLASHES_PATTERN = Pattern.compile("[/\\\\]+$");
76+
7477
// An object mapper that is used to convert an example string to
7578
// a "python-compliant" example string (boolean as True/False).
7679
final ObjectMapper MAPPER = new ObjectMapper();
@@ -251,7 +254,7 @@ public void setPythonSrcRoot(String val) {
251254
if (val == null) {
252255
pySrcRoot = "";
253256
} else {
254-
pySrcRoot = val.replaceAll("[/\\\\]+$", "");
257+
pySrcRoot = TRAILING_SLASHES_PATTERN.matcher(val).replaceAll("");
255258
}
256259

257260
if (pySrcRoot.isEmpty() || ".".equals(pySrcRoot)) {

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -961,17 +961,17 @@ public String toEnumVarName(String name, String datatype) {
961961
if ("number".equals(datatype)) {
962962
varName = "NUMBER_" + varName;
963963

964-
varName = varName.replaceAll("-", "MINUS_");
965-
varName = varName.replaceAll("\\+", "PLUS_");
966-
varName = varName.replaceAll("\\.", "_DOT_");
964+
varName = MINUS.matcher(varName).replaceAll("MINUS_");
965+
varName = PLUS.matcher(varName).replaceAll("PLUS_");
966+
varName = DOT.matcher(varName).replaceAll("_DOT_");
967967
return varName;
968968
}
969969

970970
// string
971971
if (isEnumPropertyNamingReplaceSpecialChar()) {
972-
varName = varName.replaceAll("-", "_minus_");
973-
varName = varName.replaceAll("\\+", "_plus_");
974-
varName = varName.replaceAll("_+", "_");
972+
varName = MINUS.matcher(varName).replaceAll("_minus_");
973+
varName = PLUS.matcher(varName).replaceAll("_plus_");
974+
varName = MULTI_UNDERSCORES.matcher(varName).replaceAll("_");
975975
}
976976
varName = sanitizeName(varName);
977977
varName = FIRST_LEADING_UNDERSCORE.matcher(varName).replaceFirst("");

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ public CodegenOperation fromOperation(String path, String httpMethod, Operation
219219
}
220220
}
221221

222-
String pathForOatpp = path.replaceAll("\\{(.*?)}", "{$1}");
222+
String pathForOatpp = PATH_PARAMETER.matcher(path).replaceAll("{$1}");
223223
op.vendorExtensions.put("x-codegen-oatpp-path", pathForOatpp);
224224

225225
return op;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ public CodegenOperation fromOperation(String path, String httpMethod, Operation
222222
}
223223
}
224224

225-
String pathForOatpp = path.replaceAll("\\{(.*?)}", "{$1}");
225+
String pathForOatpp = PATH_PARAMETER.matcher(path).replaceAll("{$1}");
226226
op.vendorExtensions.put("x-codegen-oatpp-path", pathForOatpp);
227227

228228
return op;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ public CodegenOperation fromOperation(String path, String httpMethod, Operation
309309
}
310310
}
311311

312-
String pathForPistache = path.replaceAll("\\{(.*?)}", ":$1");
312+
String pathForPistache = PATH_PARAMETER.matcher(path).replaceAll(":$1");
313313
op.vendorExtensions.put("x-codegen-pistache-path", pathForPistache);
314314

315315
return op;

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

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,10 @@ public class ErlangClientCodegen extends DefaultCodegen implements CodegenConfig
4646

4747
private static final Pattern PATH_TEMPLATE_PATTERN = Pattern.compile("\\{([^\\}]+)\\}");
4848

49-
@Setter protected String packageName = "openapi";
50-
@Setter protected String packageVersion = "1.0.0";
49+
@Setter
50+
protected String packageName = "openapi";
51+
@Setter
52+
protected String packageVersion = "1.0.0";
5153
protected String sourceFolder = "src";
5254

5355
@Override
@@ -258,7 +260,7 @@ public String toVarName(String name) {
258260
}
259261

260262
// replace - with _ e.g. created-at => created_at
261-
name = sanitizeName(name.replaceAll("-", "_"));
263+
name = sanitizeName(MINUS.matcher(name).replaceAll("_"));
262264
// for reserved word or word starting with number, append _
263265
if (isReservedWord(name))
264266
name = escapeReservedWord(name);
@@ -291,24 +293,24 @@ public String toArrayModelParamName(String name) {
291293

292294
@Override
293295
public String toModelName(String name) {
294-
return this.packageName + "_" + underscore(name.replaceAll("-", "_").replaceAll("\\.", "_"));
296+
return this.packageName + "_" + underscore(DOT.matcher(MINUS.matcher(name).replaceAll("_")).replaceAll("_"));
295297
}
296298

297299
@Override
298300
public String toApiName(String name) {
299-
return this.packageName + "_" + underscore(name.replaceAll("-", "_").replaceAll("\\.", "_"));
301+
return this.packageName + "_" + underscore(DOT.matcher(MINUS.matcher(name).replaceAll("_")).replaceAll("_"));
300302
}
301303

302304
@Override
303305
public String toModelFilename(String name) {
304-
return this.packageName + "_" + underscore(name.replaceAll("\\.", "_"));
306+
return this.packageName + "_" + underscore(DOT.matcher(name).replaceAll("_"));
305307
}
306308

307309
@Override
308310
public String toApiFilename(String name) {
309311
// replace - with _ e.g. created-at => created_at
310312
// FIXME: a parameter should not be assigned. Also declare the methods parameters as 'final'.
311-
name = name.replaceAll("-", "_").replaceAll("\\.", "_");
313+
name = DOT.matcher(MINUS.matcher(name).replaceAll("_")).replaceAll("_");
312314

313315
// e.g. PetApi.erl => pet_api.erl
314316
return this.packageName + "_" + underscore(name) + "_api";
@@ -318,11 +320,11 @@ public String toApiFilename(String name) {
318320
public String toOperationId(String operationId) {
319321
// method name cannot use reserved keyword, e.g. if
320322
if (isReservedWord(operationId)) {
321-
LOGGER.warn("{} (reserved word) cannot be used as method name. Renamed to {}", operationId, underscore(sanitizeName("call_" + operationId)).replaceAll("\\.", "_"));
323+
LOGGER.warn("{} (reserved word) cannot be used as method name. Renamed to {}", operationId, DOT.matcher(underscore(sanitizeName("call_" + operationId))).replaceAll("_"));
322324
operationId = "call_" + operationId;
323325
}
324326

325-
return underscore(operationId.replaceAll("\\.", "_"));
327+
return underscore(DOT.matcher(operationId).replaceAll("_"));
326328
}
327329

328330
@Override
@@ -403,9 +405,11 @@ public String escapeUnsafeCharacters(String input) {
403405
}
404406

405407
class ExtendedCodegenOperation extends CodegenOperation {
406-
@Getter @Setter
408+
@Getter
409+
@Setter
407410
private List<String> pathTemplateNames = new ArrayList<String>();
408-
@Getter @Setter
411+
@Getter
412+
@Setter
409413
private String replacedPathName;
410414
String arityRequired;
411415
String arityOptional;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<Mo
282282
List<CodegenOperation> operationList = operations.getOperation();
283283
for (CodegenOperation op : operationList) {
284284
if (op.path != null) {
285-
op.path = op.path.replaceAll("\\{(.*?)\\}", ":$1");
285+
op.path = PATH_PARAMETER.matcher(op.path).replaceAll(":$1");
286286
}
287287
}
288288
return objs;

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -505,8 +505,8 @@ private void processComposedSchemaVariants(CodegenModel mdl, List<CodegenPropert
505505
// Sanitize baseName to remove underscores and properly format for Nim
506506
if (newVariant.baseName != null) {
507507
// Remove trailing underscores and convert to proper format
508-
String sanitizedBase = newVariant.baseName.replaceAll("_+$", ""); // Remove trailing underscores
509-
if (sanitizedBase.length() > 0 && Character.isUpperCase(sanitizedBase.charAt(0))) {
508+
String sanitizedBase = MULTI_TRAILING_UNDERSCORES.matcher(newVariant.baseName).replaceAll(""); // Remove trailing underscores
509+
if (!sanitizedBase.isEmpty() && Character.isUpperCase(sanitizedBase.charAt(0))) {
510510
newVariant.baseName = toModelName(sanitizedBase);
511511
} else {
512512
newVariant.baseName = sanitizeNimIdentifier(sanitizedBase);
@@ -517,7 +517,7 @@ private void processComposedSchemaVariants(CodegenModel mdl, List<CodegenPropert
517517
// For model types (not primitives), use toModelName to get the proper type name
518518
if (newVariant.dataType != null) {
519519
// Check if this is a model type (starts with uppercase) vs primitive
520-
if (newVariant.dataType.length() > 0 && Character.isUpperCase(newVariant.dataType.charAt(0))) {
520+
if (!newVariant.dataType.isEmpty() && Character.isUpperCase(newVariant.dataType.charAt(0))) {
521521
// This is likely a model type, use toModelName to properly format it
522522
newVariant.dataType = toModelName(newVariant.dataType);
523523
} else {
@@ -730,9 +730,9 @@ private String sanitizeNimIdentifier(String name) {
730730
return name;
731731
}
732732
// Remove trailing underscores (Nim identifiers cannot end with underscore)
733-
name = name.replaceAll("_+$", "");
733+
name = MULTI_TRAILING_UNDERSCORES.matcher(name).replaceAll("");
734734
// Collapse multiple consecutive underscores to single underscore
735-
name = name.replaceAll("_+", "_");
735+
name = MULTI_UNDERSCORES.matcher(name).replaceAll("_");
736736
return name;
737737
}
738738

0 commit comments

Comments
 (0)