Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix exception when sending email has special chars #1383

Merged
merged 1 commit into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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));
}
}
Loading