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

Commit

Permalink
Fix the issue with ParametersUtils where input map is mutated
Browse files Browse the repository at this point in the history
  • Loading branch information
cyzhao committed May 6, 2020
1 parent 232eddd commit 96f2bf6
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public class CassandraEventHandlerDAO extends CassandraBaseDAO implements EventH
private static final Logger LOGGER = LoggerFactory.getLogger(CassandraEventHandlerDAO.class);
private static final String CLASS_NAME = CassandraEventHandlerDAO.class.getSimpleName();

private Map<String, EventHandler> eventHandlerCache = new HashMap<>();
private volatile Map<String, EventHandler> eventHandlerCache = new HashMap<>();

private final PreparedStatement insertEventHandlerStatement;
private final PreparedStatement selectAllEventHandlersStatement;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ public Map<String, Object> replace(Map<String, Object> input, Object json) {
}
Configuration option = Configuration.defaultConfiguration().addOptions(Option.SUPPRESS_EXCEPTIONS);
DocumentContext documentContext = JsonPath.parse(doc, option);
return replace(new HashMap<>(input), documentContext, null);
return replace(input, documentContext, null);
}

public Object replace(String paramString) {
Expand All @@ -161,23 +161,23 @@ public Object replace(String paramString) {

@SuppressWarnings("unchecked")
private Map<String, Object> replace(Map<String, Object> input, DocumentContext documentContext, String taskId) {
Map<String, Object> result = new HashMap<>();
for (Entry<String, Object> e : input.entrySet()) {
Object newValue;
Object value = e.getValue();
if (value instanceof String) {
Object replaced = replaceVariables(value.toString(), documentContext, taskId);
e.setValue(replaced);
newValue = replaceVariables(value.toString(), documentContext, taskId);
} else if (value instanceof Map) {
//recursive call
Object replaced = replace((Map<String, Object>) value, documentContext, taskId);
e.setValue(replaced);
newValue = replace((Map<String, Object>) value, documentContext, taskId);
} else if (value instanceof List) {
Object replaced = replaceList((List<?>) value, taskId, documentContext);
e.setValue(replaced);
newValue = replaceList((List<?>) value, taskId, documentContext);
} else {
e.setValue(value);
newValue = value;
}
result.put(e.getKey(), newValue);
}
return input;
return result;
}

@SuppressWarnings("unchecked")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.junit.Before;
import org.junit.Test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
Expand Down Expand Up @@ -141,4 +142,62 @@ public void testReplaceConcurrent() throws ExecutionException, InterruptedExcept

executorService.shutdown();
}

// Tests ParametersUtils with Map and List input values, and verifies input map is not mutated by ParametersUtils.
@Test
public void testReplaceInputWithMapAndList() throws Exception {
Map<String, Object> map = new HashMap<>();
map.put("name", "conductor");
map.put("version", 2);
map.put("externalId", "{\"taskRefName\":\"t001\",\"workflowId\":\"w002\"}");

Map<String, Object> input = new HashMap<>();
input.put("k1", "${$.externalId}");
input.put("k2", "${name}");
input.put("k3", "${version}");

Map<String, String> mapValue = new HashMap<>();
mapValue.put("name", "${name}");
mapValue.put("version", "${version}");
input.put("map", mapValue);

List<String> listValue = new ArrayList<>();
listValue.add("${name}");
listValue.add("${version}");
input.put("list", listValue);

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

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

// Verify that values are replaced correctly.
assertEquals("{\"taskRefName\":\"t001\",\"workflowId\":\"w002\"}", replaced.get("k1"));
assertEquals("conductor", replaced.get("k2"));
assertEquals(2, replaced.get("k3"));

Map replacedMap = (Map) replaced.get("map");
assertEquals("conductor", replacedMap.get("name"));
assertEquals(2, replacedMap.get("version"));

List replacedList = (List) replaced.get("list");
assertEquals(2, replacedList.size());
assertEquals("conductor", replacedList.get(0));
assertEquals(2, replacedList.get(1));


// Verify that input map is not mutated
assertEquals("${$.externalId}", input.get("k1"));
assertEquals("${name}", input.get("k2"));
assertEquals("${version}", input.get("k3"));

Map inputMap = (Map) input.get("map");
assertEquals("${name}", inputMap.get("name"));
assertEquals("${version}", inputMap.get("version"));

List inputList = (List) input.get("list");
assertEquals(2, inputList.size());
assertEquals("${name}", inputList.get(0));
assertEquals("${version}", inputList.get(1));
}
}

0 comments on commit 96f2bf6

Please sign in to comment.