Skip to content

Commit

Permalink
Merge pull request #1741 from arunans23/master
Browse files Browse the repository at this point in the history
Preserve backward compatibility with json arrays in Datamapper
  • Loading branch information
arunans23 authored Dec 18, 2024
2 parents c9f1420 + ba57c04 commit 88ff27a
Showing 1 changed file with 14 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

import java.util.AbstractList;
import java.util.HashMap;
import java.util.Map;

import static org.wso2.carbon.mediator.datamapper.engine.utils.DataMapperEngineConstants.DEFAULT_ENGINE_NAME;
Expand Down Expand Up @@ -150,6 +153,9 @@ public Model execute(MappingResource mappingResource, String inputVariable, Stri
} else {
result = scriptEngine.eval(jsFunction.getFunctionName(), bindings);
}
if (result instanceof AbstractList) {
result = convertListToMap((AbstractList) result);
}
if (result instanceof Map) {
return new MapModel((Map<String, Object>) result);
} else if (result instanceof String) {
Expand All @@ -169,4 +175,12 @@ public Model execute(MappingResource mappingResource, String inputVariable, Stri
private String getInputVariable(String inputSchemaName) throws ScriptException {
return "input" + inputSchemaName.replace(':', '_').replace('=', '_').replace(',', '_').replace(HYPHEN, ENCODE_CHAR_HYPHEN);
}

private Map convertListToMap(AbstractList polyglotList) {
Map<Object, Object> map = new HashMap<>();
for (int i = 0; i < polyglotList.size(); i++) {
map.put(i, polyglotList.get(i));
}
return map;
}
}

0 comments on commit 88ff27a

Please sign in to comment.