Skip to content

Commit

Permalink
Improved quote replacement to prevent errors (#635)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanlukas authored Dec 22, 2023
1 parent 9c571d2 commit ca66297
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ public String decorate(MessageTemplate template, Map<String, String> context) {
if (value == null) {
missingVariables.add(variableName);
} else {
value = value.replaceAll("\\$", "\\\\\\$");
LOG.debug("Replacing {} with {}", variableName, value);
templateString = templateString.replaceAll(PREFIX + variableName + SUFFIX, value);
templateString =
templateString.replaceAll(
PREFIX + variableName + SUFFIX, Matcher.quoteReplacement(value));
LOG.debug("Template after replacement: {}", templateString);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.camunda.community.migration.converter.message;

import static org.assertj.core.api.Assertions.*;
import static org.camunda.community.migration.converter.message.MessageFactory.*;
import static org.junit.jupiter.api.Assertions.*;

import java.util.Collections;
Expand Down Expand Up @@ -52,4 +53,18 @@ void shouldReplaceAllVariablesWithSameName() {
String message = MESSAGE_TEMPLATE_PROCESSOR.decorate(messageTemplate, context);
assertEquals("Hello Tim, this is Tim", message);
}

@Test
void shouldEscapeStuff() {
MessageTemplate messageTemplate =
new MessageTemplate(
Severity.INFO,
null,
"Hello {{ world }}, this is {{ template }}",
List.of("world", "template"));
Map<String, String> context =
ContextBuilder.builder().entry("world", "Tim").entry("template", "\\$$").build();
String message = MESSAGE_TEMPLATE_PROCESSOR.decorate(messageTemplate, context);
assertEquals("Hello Tim, this is \\$$", message);
}
}

0 comments on commit ca66297

Please sign in to comment.