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

Fix JSONPath replace failed with line terminator #3810

Merged
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 @@ -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
Loading