Skip to content

Commit 4f76b5b

Browse files
committed
fix working with regex patterns. Add duration logging to GenerateBatch
1 parent 5c48182 commit 4f76b5b

48 files changed

Lines changed: 190 additions & 185 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

modules/openapi-generator-cli/src/main/java/org/openapitools/codegen/cmd/GenerateBatch.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,8 @@ public void execute() {
148148

149149
ExecutorService executor = Executors.newFixedThreadPool(numThreads);
150150

151+
long startTime = System.currentTimeMillis();
152+
151153
// Execute each configurator on a separate pooled thread.
152154
configurators.forEach(configurator -> {
153155
GenerationRunner runner = new GenerationRunner(configurator, rootDir, Boolean.TRUE.equals(failFast), Boolean.TRUE.equals(clean));
@@ -163,12 +165,18 @@ public void execute() {
163165

164166
executor.awaitTermination(awaitFor, TimeUnit.MINUTES);
165167

168+
long elapsedMs = System.currentTimeMillis() - startTime;
169+
long minutes = TimeUnit.MILLISECONDS.toMinutes(elapsedMs);
170+
long seconds = TimeUnit.MILLISECONDS.toSeconds(elapsedMs) % 60;
171+
long millis = elapsedMs % 1000;
172+
String elapsed = String.format(Locale.ROOT, "%dm %ds %dms", minutes, seconds, millis);
173+
166174
int failCount = failures.intValue();
167175
if (failCount > 0) {
168-
System.err.println(String.format(Locale.ROOT, "[FAIL] Completed with %d failures, %d successes", failCount, successes.intValue()));
176+
System.err.println(String.format(Locale.ROOT, "[FAIL] Completed with %d failures, %d successes in %s", failCount, successes.intValue(), elapsed));
169177
System.exit(1);
170178
} else {
171-
System.out.println(String.format(Locale.ROOT, "[SUCCESS] Batch generation finished %d generators successfully.", successes.intValue()));
179+
System.out.println(String.format(Locale.ROOT, "[SUCCESS] Batch generation finished %d generators successfully in %s.", successes.intValue(), elapsed));
172180
}
173181
} catch (InterruptedException e) {
174182
e.printStackTrace();

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,18 +250,27 @@ apiTemplateFiles are for API outputs only (controllers/handlers).
250250
protected final static Pattern JSON_VENDOR_MIME_PATTERN = Pattern.compile("(?i)application/vnd.(.*)+json(;.*)?");
251251
private static final Pattern COMMON_PREFIX_ENUM_NAME = Pattern.compile("[a-zA-Z0-9]+\\z");
252252
/** Matches a trailing run of digits at the end of a name, used by {@link #generateNextName}. */
253-
private static final Pattern TRAILING_DIGITS = Pattern.compile("\\d+\\z");
253+
protected static final Pattern TRAILING_DIGITS = Pattern.compile("\\d+\\z");
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
/** Matches a string that starts with a digit; used in {@link #toEnumVarName}. */
257257
protected static final Pattern LEADING_DIGIT = Pattern.compile("\\d.*");
258+
protected static final Pattern LEADING_UNDERSCORES = Pattern.compile("^_*");
259+
260+
protected static final Pattern WHITESPACE = Pattern.compile("\\s+");
261+
262+
protected static final Pattern STARTS_WITH_SLASH = Pattern.compile("^/.*");
263+
264+
protected static final Pattern UNESCAPED_SLASH = Pattern.compile("(?<!\\\\)/");
258265

259266
/** Matches a string that starts with a digit (anchored); used across language generators. */
260267
protected static final Pattern STARTS_WITH_DIGIT = Pattern.compile("^\\d.*");
261268
/** Matches a string consisting entirely of uppercase letters and underscores. */
262269
protected static final Pattern ALL_UPPER_UNDERSCORE = Pattern.compile("^[A-Z_]*$");
263270
/** Matches tab, newline, or carriage-return; used in {@link #escapeText}. */
264271
protected static final Pattern CONTROL_WHITESPACE = Pattern.compile("[\\t\\n\\r]");
272+
273+
protected static final Pattern MULTILINE_STRING = Pattern.compile("\r\n|\r|\n");
265274
/** Matches a callback path-expression parameter like {@code {$request.body#/id}}. */
266275
private static final Pattern CALLBACK_EXPRESSION_PARAM = Pattern.compile("\\{\\$.*}");
267276
// Dynamic patterns keyed by user-supplied removeCharRegEx strings are cached via PatternCache.
@@ -6797,7 +6806,7 @@ public String sanitizeTag(String tag) {
67976806
tag = camelize(sanitizeName(tag));
67986807

67996808
// tag starts with numbers
6800-
if (tag.matches("^\\d.*")) {
6809+
if (STARTS_WITH_DIGIT.matcher(tag).matches()) {
68016810
tag = "Class" + tag;
68026811
}
68036812

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ abstract public class AbstractAdaCodegen extends DefaultCodegen implements Codeg
4747
private final Logger LOGGER = LoggerFactory.getLogger(AbstractAdaCodegen.class);
4848

4949
private static final Pattern LEADING_DIGIT = Pattern.compile("^\\d");
50-
private static final Pattern NON_WORD_PLUS = Pattern.compile("\\W+");
5150

5251
public static final String HTTP_SUPPORT_OPTION = "httpSupport";
5352
public static final String OPENAPI_PACKAGE_NAME_OPTION = "openApiName";

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ public abstract class AbstractCSharpCodegen extends DefaultCodegen {
5656

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]+$");
59-
private static final Pattern LEADING_DIGIT = Pattern.compile("^\\d");
6059
private static final Pattern UNESCAPED_DOUBLE_QUOTE = Pattern.compile("(?<!\\\\)\"");
6160

6261
protected boolean optionalAssemblyInfoFlag = true;
@@ -1425,7 +1424,7 @@ public String toOperationId(String operationId) {
14251424
}
14261425

14271426
// operationId starts with a number
1428-
if (LEADING_DIGIT.matcher(operationId).find()) {
1427+
if (STARTS_WITH_DIGIT.matcher(operationId).find()) {
14291428
LOGGER.warn("{} (starting with a number) cannot be used as method name. Renamed to {}", operationId, camelize(sanitizeName("call_" + operationId)));
14301429
operationId = "call_" + operationId;
14311430
}
@@ -1453,7 +1452,7 @@ public String toVarName(String name) {
14531452
name = camelize(name);
14541453

14551454
// for reserved word or word starting with number, append _
1456-
if (isReservedWord(name) || LEADING_DIGIT.matcher(name).find()) {
1455+
if (isReservedWord(name) || STARTS_WITH_DIGIT.matcher(name).find()) {
14571456
name = escapeReservedWord(name);
14581457
}
14591458

@@ -1500,7 +1499,7 @@ public String escapeReservedWord(CodegenModel model, String name) {
15001499
@Override
15011500
public String escapeReservedWord(String name) {
15021501
if (isReservedWord(name) ||
1503-
LEADING_DIGIT.matcher(name).find()) {
1502+
STARTS_WITH_DIGIT.matcher(name).find()) {
15041503
name = AbstractCSharpCodegen.invalidParameterNamePrefix + camelize(name);
15051504
}
15061505
return name;
@@ -1678,7 +1677,7 @@ public String toModelName(String name) {
16781677
}
16791678

16801679
// model name starts with number
1681-
if (LEADING_DIGIT.matcher(name).find()) {
1680+
if (STARTS_WITH_DIGIT.matcher(name).find()) {
16821681
LOGGER.warn("{} (model name starts with number) cannot be used as model name. Renamed to {}", name,
16831682
camelize("model_" + name));
16841683
name = camelize("model_" + name); // e.g. 200Response => Model200Response (after camelize)
@@ -1804,7 +1803,7 @@ public String toEnumVarName(String name, String datatype) {
18041803

18051804
enumName = adjustNamingStyle(enumName) + this.enumValueSuffix;
18061805

1807-
if (LEADING_DIGIT.matcher(enumName).find()) { // starts with number
1806+
if (STARTS_WITH_DIGIT.matcher(enumName).find()) { // starts with number
18081807
return "_" + enumName;
18091808
} else {
18101809
return enumName;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -459,10 +459,10 @@ public void processOpts() {
459459
convertPropertyToStringAndWriteBack(BOOLEAN_GETTER_PREFIX, this::setBooleanGetterPrefix);
460460
convertPropertyToBooleanAndWriteBack(IGNORE_ANYOF_IN_ENUM, this::setIgnoreAnyOfInEnum);
461461
convertPropertyToTypeAndWriteBack(ADDITIONAL_MODEL_TYPE_ANNOTATIONS,
462-
annotations -> Arrays.asList(annotations.trim().split("\\s*(;|\\r?\\n)\\s*")),
462+
annotations -> Arrays.asList(SPLIT_ON_SEMICOLON_OR_NEWLINE_REGEX.split(annotations.trim())),
463463
this::setAdditionalModelTypeAnnotations);
464464
convertPropertyToTypeAndWriteBack(ADDITIONAL_ONE_OF_TYPE_ANNOTATIONS,
465-
annotations -> Arrays.asList(annotations.trim().split("\\s*(;|\\r?\\n)\\s*")),
465+
annotations -> Arrays.asList(SPLIT_ON_SEMICOLON_OR_NEWLINE_REGEX.split(annotations.trim())),
466466
this::setAdditionalOneOfTypeAnnotations);
467467
convertPropertyToTypeAndWriteBack(ADDITIONAL_ENUM_TYPE_ANNOTATIONS,
468468
annotations -> Arrays.asList(annotations.split(";")),

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,7 @@
4949
public abstract class AbstractPythonCodegen extends DefaultCodegen implements CodegenConfig {
5050
private final Logger LOGGER = LoggerFactory.getLogger(AbstractPythonCodegen.class);
5151

52-
private static final Pattern MULTILINE_STRING = Pattern.compile("\r\n|\r|\n");
5352
private static final Pattern REGEX_VALUE_EXTRACTOR = Pattern.compile("^/\\^?(.+?)\\$?/.?$");
54-
private static final Pattern LEADING_UNDERSCORES = Pattern.compile("^_*");
55-
private static final Pattern WHITESPACE = Pattern.compile("\\s+");
56-
private static final Pattern STARTS_WITH_SLASH = Pattern.compile("^/.*");
57-
private static final Pattern UNESCAPED_SLASH = Pattern.compile("(?<!\\\\)\\/");
5853

5954
public static final String MAP_NUMBER_TO = "mapNumberTo";
6055

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

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,15 @@
4646
public abstract class AbstractPythonPydanticV1Codegen extends DefaultCodegen implements CodegenConfig {
4747
private final Logger LOGGER = LoggerFactory.getLogger(AbstractPythonPydanticV1Codegen.class);
4848

49-
private static final Pattern MULTILINE_STRING = Pattern.compile("\r\n|\r|\n");
5049
private static final Pattern REGEX_VALUE_EXTRACTOR = Pattern.compile("^/\\^?(.+?)\\$?/.?$");
5150

5251
public static final String MAP_NUMBER_TO = "mapNumberTo";
5352

5453
protected String packageName = "openapi_client";
55-
@Setter protected String packageVersion = "1.0.0";
56-
@Setter protected String projectName; // for setup.py, e.g. petstore-api
54+
@Setter
55+
protected String packageVersion = "1.0.0";
56+
@Setter
57+
protected String projectName; // for setup.py, e.g. petstore-api
5758
protected boolean hasModelsToImport = Boolean.FALSE;
5859
protected String mapNumberTo = "Union[StrictFloat, StrictInt]";
5960
protected Map<Character, String> regexModifiers;
@@ -226,7 +227,7 @@ public String toVarName(String name) {
226227
name = name.replace("$", "");
227228

228229
// if it's all upper case, convert to lower case
229-
if (name.matches("^[A-Z_]*$")) {
230+
if (ALL_UPPER_UNDERSCORE.matcher(name).matches()) {
230231
name = name.toLowerCase(Locale.ROOT);
231232
}
232233

@@ -235,10 +236,10 @@ public String toVarName(String name) {
235236
name = underscore(name);
236237

237238
// remove leading underscore
238-
name = name.replaceAll("^_*", "");
239+
name = LEADING_UNDERSCORES.matcher(name).replaceAll("");
239240

240241
// for reserved word or word starting with number, append _
241-
if (isReservedWord(name) || name.matches("^\\d.*")) {
242+
if (isReservedWord(name) || STARTS_WITH_DIGIT.matcher(name).matches()) {
242243
name = escapeReservedWord(name);
243244
}
244245

@@ -280,7 +281,7 @@ public String toOperationId(String operationId) {
280281
}
281282

282283
// operationId starts with a number
283-
if (operationId.matches("^\\d.*")) {
284+
if (STARTS_WITH_DIGIT.matcher(operationId).matches()) {
284285
LOGGER.warn("{} (starting with a number) cannot be used as method name. Renamed to {}", operationId, underscore(sanitizeName("call_" + operationId)));
285286
operationId = "call_" + operationId;
286287
}
@@ -704,7 +705,7 @@ public String toModelName(String name) {
704705
// remove dollar sign
705706
sanitizedName = sanitizedName.replace("$", "");
706707
// remove whitespace
707-
sanitizedName = sanitizedName.replaceAll("\\s+", "");
708+
sanitizedName = WHITESPACE.matcher(sanitizedName).replaceAll("");
708709

709710
String nameWithPrefixSuffix = sanitizedName;
710711
if (!StringUtils.isEmpty(modelNamePrefix)) {
@@ -730,7 +731,7 @@ public String toModelName(String name) {
730731
}
731732

732733
// model name starts with number
733-
if (camelizedName.matches("^\\d.*")) {
734+
if (STARTS_WITH_DIGIT.matcher(camelizedName).matches()) {
734735
String modelName = "Model" + camelizedName; // e.g. return => ModelReturn (after camelize)
735736
LOGGER.warn("{} (model name starts with number) cannot be used as model name. Renamed to {}", camelizedName, modelName);
736737
schemaKeyToModelNameCache.put(origName, modelName);
@@ -1630,7 +1631,7 @@ public String toEnumVariableName(String name, String datatype) {
16301631
name = name.replaceFirst("^_", "");
16311632
name = name.replaceFirst("_$", "");
16321633

1633-
if (name.matches("\\d.*")) {
1634+
if (LEADING_DIGIT.matcher(name).matches()) {
16341635
name = "ENUM_" + name.toUpperCase(Locale.ROOT);
16351636
}
16361637

@@ -1931,9 +1932,9 @@ public String addRegularExpressionDelimiter(String pattern) {
19311932
return pattern;
19321933
}
19331934

1934-
if (!pattern.matches("^/.*")) {
1935+
if (!STARTS_WITH_SLASH.matcher(pattern).matches()) {
19351936
// Perform a negative lookbehind on each `/` to ensure that it is escaped.
1936-
return "/" + pattern.replaceAll("(?<!\\\\)\\/", "\\\\/") + "/";
1937+
return "/" + UNESCAPED_SLASH.matcher(pattern).replaceAll("\\\\/") + "/";
19371938
}
19381939

19391940
return pattern;

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ public String toVarName(final String name) {
180180

181181
String varName = sanitizeName(name);
182182
// if it's all upper case, convert to lower case
183-
if (name.matches("^[A-Z_]*$")) {
183+
if (ALL_UPPER_UNDERSCORE.matcher(name).matches()) {
184184
varName = varName.toLowerCase(Locale.ROOT);
185185
}
186186

@@ -189,7 +189,7 @@ public String toVarName(final String name) {
189189
varName = underscore(varName);
190190

191191
// for reserved word or word starting with number, append _
192-
if (isReservedWord(varName) || varName.matches("^\\d.*")) {
192+
if (isReservedWord(varName) || STARTS_WITH_DIGIT.matcher(varName).matches()) {
193193
varName = escapeReservedWord(varName);
194194
}
195195

@@ -207,9 +207,9 @@ public String addRegularExpressionDelimiter(String pattern) {
207207
return pattern;
208208
}
209209

210-
if (!pattern.matches("^/.*")) {
210+
if (!STARTS_WITH_SLASH.matcher(pattern).matches()) {
211211
// Perform a negative lookbehind on each `/` to ensure that it is escaped.
212-
return "/" + pattern.replaceAll("(?<!\\\\)\\/", "\\\\/") + "/";
212+
return "/" + UNESCAPED_SLASH.matcher(pattern).replaceAll("\\\\/") + "/";
213213
}
214214

215215
return pattern;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ public String sanitizeIdentifier(String name, CasingType casingType, String esca
207207

208208
// If word starts with number add a prefix
209209
// Note: this must be done after casing since CamelCase will strip leading underscores
210-
if (name.matches("^\\d.*")) {
210+
if (STARTS_WITH_DIGIT.matcher(name).matches()) {
211211
nameWasModified = true;
212212
name = casingFunction.apply(escapePrefix + '_' + name);
213213
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ public String toVarName(String name) {
292292
varName = getNameUsingModelPropertyNaming(varName);
293293
}
294294

295-
if (isReservedWord(varName) || varName.matches("^\\d.*")) {
295+
if (isReservedWord(varName) || STARTS_WITH_DIGIT.matcher(varName).matches()) {
296296
varName = escapeReservedWord(varName);
297297
}
298298

@@ -502,7 +502,7 @@ public String toModelName(final String name) {
502502
}
503503

504504
// model name starts with number
505-
if (name.matches("^\\d.*")) {
505+
if (STARTS_WITH_DIGIT.matcher(name).matches()) {
506506
final String modelName = "Model" + camelizedName; // e.g. 200Response => Model200Response (after camelize)
507507
LOGGER.warn("{} (model name starts with number) cannot be used as model name. Renamed to {}", name,
508508
modelName);
@@ -601,7 +601,7 @@ public String toOperationId(String operationId) {
601601
}
602602

603603
// operationId starts with a number
604-
if (operationId.matches("^\\d.*")) {
604+
if (STARTS_WITH_DIGIT.matcher(operationId).matches()) {
605605
LOGGER.warn(operationId + " (starting with a number) cannot be used as method sname. Renamed to " + camelize("call_" + operationId), true);
606606
operationId = camelize("call_" + operationId, LOWERCASE_FIRST_LETTER);
607607
}

0 commit comments

Comments
 (0)