Skip to content

Commit

Permalink
fix exception when sending email has special chars (#1383)
Browse files Browse the repository at this point in the history
Co-authored-by: Carpe-Wang <[email protected]>
  • Loading branch information
Carpe-Wang and Carpe-Wang authored Dec 5, 2023
1 parent f971118 commit 716faa0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,14 @@ public static String render(String template, Map<String, Object> replaceData) {
}
try {
Matcher matcher = PATTERN.matcher(template);
StringBuilder buffer = new StringBuilder();
StringBuilder builder = new StringBuilder();
while (matcher.find()) {
Object objectValue = replaceData.getOrDefault(matcher.group(1), "NullValue");
String value = objectValue.toString();
matcher.appendReplacement(buffer, value);
matcher.appendReplacement(builder, Matcher.quoteReplacement(value));
}
matcher.appendTail(buffer);
return buffer.toString();
matcher.appendTail(builder);
return builder.toString();
} catch (Exception e) {
log.error(e.getMessage(), e);
return template;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,17 @@ void render() {
template = "${key1} ${key2} ${key3}";
assertEquals(AlertTemplateUtil.render(template, param), "Just for testing");
}

@Test
void renderSpecialCharacters() {
Map<String, Object> param = new HashMap<>();
param.put("valueWithDollar", "$100");
param.put("valueWithBackslash", "C:\\Users");

String template = "The price is ${valueWithDollar} and the path is ${valueWithBackslash}";

// Expected to handle the dollar sign and backslash correctly without throwing an exception
String expectedResult = "The price is $100 and the path is C:\\Users";
assertEquals(expectedResult, AlertTemplateUtil.render(template, param));
}
}

0 comments on commit 716faa0

Please sign in to comment.