Skip to content

Commit ae35597

Browse files
committed
implement suggested fixes from CR. fix some general nitpicks.
1 parent cecf84f commit ae35597

1 file changed

Lines changed: 14 additions & 3 deletions

File tree

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

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979
import java.nio.charset.StandardCharsets;
8080
import java.util.*;
8181
import java.util.Map.Entry;
82+
import java.util.concurrent.ConcurrentHashMap;
8283
import java.util.concurrent.ConcurrentSkipListSet;
8384
import java.util.concurrent.TimeUnit;
8485
import java.util.function.Consumer;
@@ -248,6 +249,14 @@ apiTemplateFiles are for API outputs only (controllers/handlers).
248249
protected final static Pattern JSON_MIME_PATTERN = Pattern.compile("(?i)application/json(;.*)?");
249250
protected final static Pattern JSON_VENDOR_MIME_PATTERN = Pattern.compile("(?i)application/vnd.(.*)+json(;.*)?");
250251
private static final Pattern COMMON_PREFIX_ENUM_NAME = Pattern.compile("[a-zA-Z0-9]+\\z");
252+
/** 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");
254+
/**
255+
* Cache of removeCharRegEx string → compiled {@link Pattern} with {@link Pattern#UNICODE_CHARACTER_CLASS}.
256+
* {@code sanitizeName} is called once per unique (name, regex, exceptions) tuple, so the regex string
257+
* (almost always {@code "\\W"}) would otherwise be recompiled for every unique property/model name.
258+
*/
259+
private static final ConcurrentHashMap<String, Pattern> REMOVE_CHAR_UNICODE_PATTERN_CACHE = new ConcurrentHashMap<>();
251260

252261
/**
253262
* True if the code generator supports multiple class inheritance.
@@ -6009,8 +6018,7 @@ protected void addParentContainer(CodegenModel model, String name, Schema schema
60096018
* @return The next name for the base name
60106019
*/
60116020
private static String generateNextName(String name) {
6012-
Pattern pattern = Pattern.compile("\\d+\\z");
6013-
Matcher matcher = pattern.matcher(name);
6021+
Matcher matcher = TRAILING_DIGITS.matcher(name);
60146022
if (matcher.find()) {
60156023
String numStr = matcher.group();
60166024
int num = Integer.parseInt(numStr) + 1;
@@ -6755,7 +6763,10 @@ public String sanitizeName(final String name, String removeCharRegEx, ArrayList<
67556763
// remove everything else other than word, number and _
67566764
// $php_variable => php_variable
67576765
if (allowUnicodeIdentifiers) { //could be converted to a single line with ?: operator
6758-
modifiable = Pattern.compile(sanitizeNameOptions.getRemoveCharRegEx(), Pattern.UNICODE_CHARACTER_CLASS).matcher(modifiable).replaceAll("");
6766+
modifiable = REMOVE_CHAR_UNICODE_PATTERN_CACHE
6767+
.computeIfAbsent(sanitizeNameOptions.getRemoveCharRegEx(),
6768+
regex -> Pattern.compile(regex, Pattern.UNICODE_CHARACTER_CLASS))
6769+
.matcher(modifiable).replaceAll("");
67596770
} else {
67606771
modifiable = modifiable.replaceAll(sanitizeNameOptions.getRemoveCharRegEx(), "");
67616772
}

0 commit comments

Comments
 (0)