Skip to content
This repository has been archived by the owner on Dec 13, 2023. It is now read-only.

Commit

Permalink
Fix JSONPath replace failed with line terminator (#3810)
Browse files Browse the repository at this point in the history
  • Loading branch information
Young-Zen authored Nov 3, 2023
1 parent d164531 commit 8981878
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ public class ParametersUtils {
private static final Logger LOGGER = LoggerFactory.getLogger(ParametersUtils.class);
private static final Pattern PATTERN =
Pattern.compile(
"(?=(?<!\\$)\\$\\{)(?:(?=.*?\\{(?!.*?\\1)(.*\\}(?!.*\\2).*))(?=.*?\\}(?!.*?\\2)(.*)).)+?.*?(?=\\1)[^{]*(?=\\2$)");
"(?=(?<!\\$)\\$\\{)(?:(?=.*?\\{(?!.*?\\1)(.*\\}(?!.*\\2).*))(?=.*?\\}(?!.*?\\2)(.*)).)+?.*?(?=\\1)[^{]*(?=\\2$)",
Pattern.DOTALL);

private final ObjectMapper objectMapper;
private final TypeReference<Map<String, Object>> map = new TypeReference<>() {};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,30 @@ public void testNestedPathExpressions() throws Exception {
assertEquals(5, replaced.get("k3"));
}

@Test
public void testReplaceWithLineTerminators() throws Exception {
Map<String, Object> map = new HashMap<>();
map.put("name", "conductor");
map.put("version", 2);

Map<String, Object> input = new HashMap<>();
input.put("k1", "Name: ${name}; Version: ${version};");
input.put("k2", "Name: ${name};\nVersion: ${version};");
input.put("k3", "Name: ${name};\rVersion: ${version};");
input.put("k4", "Name: ${name};\r\nVersion: ${version};");

Object jsonObj = objectMapper.readValue(objectMapper.writeValueAsString(map), Object.class);

Map<String, Object> replaced = parametersUtils.replace(input, jsonObj);

assertNotNull(replaced);

assertEquals("Name: conductor; Version: 2;", replaced.get("k1"));
assertEquals("Name: conductor;\nVersion: 2;", replaced.get("k2"));
assertEquals("Name: conductor;\rVersion: 2;", replaced.get("k3"));
assertEquals("Name: conductor;\r\nVersion: 2;", replaced.get("k4"));
}

@Test
public void testReplaceWithEscapedTags() throws Exception {
Map<String, Object> map = new HashMap<>();
Expand Down

0 comments on commit 8981878

Please sign in to comment.