Skip to content

Commit

Permalink
[WIP] Fix Golang pattern validation with regex fails on commas OpenAP…
Browse files Browse the repository at this point in the history
  • Loading branch information
mlebihan committed Dec 13, 2024
1 parent 9e1afba commit bebddac
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -787,6 +787,8 @@ public ModelsMap postProcessModels(ModelsMap objs) {
}

if (cp.pattern != null) {
LOGGER.info("Received pattern: {}", cp.pattern);

// Pattern might be enclosed into /.../ that aren't wished. Remove them.
String p = cp.pattern;

Expand All @@ -797,18 +799,6 @@ public ModelsMap postProcessModels(ModelsMap objs) {
String regexp = String.format(Locale.getDefault(), "regexp=%s", p);
String validate = String.format(Locale.getDefault(), "validate:\"%s\"", regexp);

// Escape single backslash (First!)
if (validate.contains("\\")) {
/* We need to escape \ to \\
except if it is currently present in the string for the quote itself. Regexp:
\\[^"]
*/
String BACKSLASH_THAT_ISNT_AN_ESCAPED_QUOTE="\\\\[^\"]";
String ESCAPED_BACKSLASH_INTO_A_STRING_ITSELF = Matcher.quoteReplacement("\\\\");

validate = validate.replaceAll(BACKSLASH_THAT_ISNT_AN_ESCAPED_QUOTE, ESCAPED_BACKSLASH_INTO_A_STRING_ITSELF);
}

// Replace backtick by \\x60, if found
if (validate.contains("`")) {
validate = validate.replace("`", "\\x60");
Expand All @@ -819,6 +809,7 @@ public ModelsMap postProcessModels(ModelsMap objs) {
validate = validate.replace(",", "\\\\,");
}

LOGGER.info("validate clause: {}", validate);
cp.vendorExtensions.put(X_GO_CUSTOM_TAG, validate);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ components:

name:
type: string
pattern: "^[&#x9;!'&\"()*+,-./0-9:;<=>;?A-Z_a-z[]{}|^@#~]+`$"
pattern: "^/0-9$"

0 comments on commit bebddac

Please sign in to comment.