Skip to content

Commit

Permalink
Fixing recursivity
Browse files Browse the repository at this point in the history
  • Loading branch information
fjtirado committed Jan 3, 2023
1 parent 58bbac7 commit 0cd7194
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

public class JacksonConverter<T> implements Function<String, T> {

private Class<T> clazz;
private final Class<T> clazz;

public JacksonConverter(Class<T> clazz) {
this.clazz = clazz;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
package org.kie.kogito.serverless.workflow.utils;

import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
Expand Down Expand Up @@ -67,11 +66,10 @@ private static Stream<Variable> getEvalVariables(ContextContainer container) {
}

public static JsonNodeContext from(JsonNode jsonNode, KogitoProcessContext context) {
Map<String, JsonNode> map = Collections.emptyMap();
Map<String, JsonNode> map = new HashMap<>();
if (jsonNode.isObject()) {
ObjectNode objectNode = (ObjectNode) jsonNode;
map = addVariablesFromContext(context);
map.forEach(objectNode::set);
addVariablesFromContext(objectNode, context, map);
}
return new JsonNodeContext(jsonNode, map.keySet());
}
Expand All @@ -85,31 +83,34 @@ private JsonNodeContext(JsonNode jsonNode, Set<String> keys) {
this.keys = keys;
}

private static Map<String, JsonNode> addVariablesFromContext(KogitoProcessContext processInfo) {
private static void addVariablesFromContext(ObjectNode jsonNode, KogitoProcessContext processInfo, Map<String, JsonNode> variables) {
KogitoNodeInstance nodeInstance = processInfo.getNodeInstance();
Map<String, JsonNode> variables = new HashMap<>();
if (nodeInstance != null) {
NodeInstanceContainer container = nodeInstance instanceof NodeInstanceContainer ? (NodeInstanceContainer) nodeInstance : nodeInstance.getNodeInstanceContainer();
while (container instanceof ContextableInstance) {
getVariablesFromContext(variables, (ContextableInstance) container);
getVariablesFromContext(jsonNode, (ContextableInstance) container, variables);
container = container instanceof KogitoNodeInstance ? ((KogitoNodeInstance) container).getNodeInstanceContainer() : null;
}
}
return variables;
variables.forEach(jsonNode::set);
}

private static void getVariablesFromContext(Map<String, JsonNode> variables, ContextableInstance node) {
private static void getVariablesFromContext(ObjectNode jsonNode, ContextableInstance node, Map<String, JsonNode> variables) {
VariableScopeInstance variableScope = (VariableScopeInstance) node.getContextInstance(VariableScope.VARIABLE_SCOPE);
if (variableScope != null) {
Collection<String> evalVariables = getEvalVariables(node).map(Variable::getName).collect(Collectors.toList());
for (Entry<String, Object> e : variableScope.getVariables().entrySet()) {
if (evalVariables.contains(e.getKey()) || node instanceof WorkflowProcessInstance && !e.getKey().equals("workflowdata")) {
if (evalVariables.contains(e.getKey()) || shouldAdd(e, jsonNode, node)) {
variables.putIfAbsent(e.getKey(), JsonObjectUtils.fromValue(e.getValue()));
}
}
}
}

private static boolean shouldAdd(Entry<String, Object> e, ObjectNode jsonNode, ContextableInstance node) {
return jsonNode != e.getValue() && node instanceof WorkflowProcessInstance && !e.getKey().equals("workflowdata");
}

@Override
public void close() {
if (!keys.isEmpty()) {
Expand Down

0 comments on commit 0cd7194

Please sign in to comment.