Skip to content

Commit

Permalink
Serialization fix
Browse files Browse the repository at this point in the history
  • Loading branch information
ThuF committed Feb 3, 2025
1 parent c4b8ce9 commit 0ee6f88
Showing 1 changed file with 68 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -275,12 +275,15 @@ class ExecutionContext {
this.execution.setMultiInstanceRoot(isMultiInstanceRoot);
}


/**
* Returns all variables. This will include all variables of parent scopes too.
*/
public getVariables(): Map<string, any> {
return this.execution.getVariables();
const variables = this.execution.getVariables();
for (const [key, value] of variables) {
variables.set(key, this.parseValue(value));
}
return variables;
}

/**
Expand All @@ -294,7 +297,11 @@ class ExecutionContext {
* Returns the variable local to this scope only. So, in contrary to {@link #getVariables()}, the variables from the parent scope won't be returned.
*/
public getVariablesLocal(): Map<string, any> {
return this.execution.getVariablesLocal();
const variablesLocal = this.execution.getVariablesLocal();
for (const [key, value] of variablesLocal) {
variablesLocal.set(key, this.parseValue(value));
}
return variablesLocal;
}

/**
Expand All @@ -308,7 +315,7 @@ class ExecutionContext {
* Returns the variable value for one specific variable. Will look in parent scopes when the variable does not exist on this particular scope.
*/
public getVariable(variableName: string): any {
return this.execution.getVariable(variableName);
return this.parseValue(this.execution.getVariable(variableName));
}

/**
Expand All @@ -318,12 +325,11 @@ class ExecutionContext {
return this.execution.getVariableInstance(variableName);
}


/**
* Returns the value for the specific variable and only checks this scope and not any parent scope.
*/
public getVariableLocal(variableName: string): any {
return this.execution.getVariableLocal(variableName);
return this.parseValue(this.execution.getVariableLocal(variableName));
}

/**
Expand All @@ -337,14 +343,22 @@ class ExecutionContext {
* Returns all the names of the variables for this scope and all parent scopes.
*/
public getVariableNames(): Set<string> {
return this.execution.getVariableNames();
const variableNames = new Set<string>();
for (const next of this.execution.getVariableNames().toArray()) {
variableNames.add(next);
}
return variableNames;
}

/**
* Returns all the names of the variables for this scope (no parent scopes).
*/
public getVariableNamesLocal(): Set<string> {
return this.execution.getVariableNamesLocal();
const variableNamesLocal = new Set<string>();
for (const next of this.execution.getVariableNamesLocal().toArray()) {
variableNamesLocal.add(next);
}
return variableNamesLocal;
}

/**
Expand All @@ -369,15 +383,15 @@ class ExecutionContext {
* the value of the variable to be set
*/
public setVariable(variableName: string, value: any): void {
this.execution.setVariable(variableName, value);
this.execution.setVariable(variableName, this.stringifyValue(value));
}

/**
* Similar to {@link #setVariable(String, Object)}, but the variable is set to this scope specifically. Variable name
is handled as a variable name string without resolving an expression.
*/
public setVariableLocal(variableName: string, value: any): any {
return this.execution.setVariableLocal(variableName, value);
return this.execution.setVariableLocal(variableName, this.stringifyValue(value));
}

/**
Expand All @@ -390,13 +404,19 @@ class ExecutionContext {
* a map of keys and values for the variables to be set
*/
public setVariables(variables: Map<string, any>): void {
for (const [key, value] of variables) {
variables.set(key, this.stringifyValue(value));
}
this.execution.setVariables(variables);
}

/**
* Similar to {@link #setVariables(Map)}, but the variable are set on this scope specifically.
*/
public setVariablesLocal(variables: Map<string, any>): void {
for (const [key, value] of variables) {
variables.set(key, this.stringifyValue(value));
}
this.execution.setVariablesLocal(variables);
}

Expand Down Expand Up @@ -463,20 +483,23 @@ class ExecutionContext {
* where 'abc' is both persistent and transient, the transient value is returned.
*/
public setTransientVariable(variableName: string, variableValue: any): void {
this.execution.setTransientVariable(variableName, variableValue);
this.execution.setTransientVariable(variableName, this.stringifyValue(variableValue));
}

/**
* Similar to {@link #setVariableLocal(String, Object)}, but for a transient variable. See {@link #setTransientVariable(String, Object)} for the rules on 'transient' variables.
*/
public setTransientVariableLocal(variableName: string, variableValue: any): void {
this.execution.setTransientVariableLocal(variableName, variableValue);
this.execution.setTransientVariableLocal(variableName, this.stringifyValue(variableValue));
}

/**
* Similar to {@link #setVariables(Map)}, but for transient variables. See {@link #setTransientVariable(String, Object)} for the rules on 'transient' variables.
*/
public setTransientVariables(transientVariables: Map<string, any>): void {
for (const [key, value] of transientVariables) {
transientVariables.set(key, this.stringifyValue(value));
}
this.execution.setTransientVariables(transientVariables);
}

Expand All @@ -485,35 +508,46 @@ class ExecutionContext {
* 'transient' variables.
*/
public getTransientVariable(variableName: string): any {
return this.execution.getTransientVariable(variableName);
return this.parseValue(this.execution.getTransientVariable(variableName));
}

/**
* Similar to {@link #getVariables()}, but for transient variables only. See {@link #setTransientVariable(String, Object)} for the rules on 'transient' variables.
*/
public getTransientVariables(): Map<string, any> {
return this.execution.getTransientVariables();
const transientVariables = this.execution.getTransientVariables();
for (const [key, value] of transientVariables) {
transientVariables.set(key, this.parseValue(value));
}
return transientVariables;
}

/**
* Similar to {@link #setVariablesLocal(Map)}, but for transient variables. See {@link #setTransientVariable(String, Object)} for the rules on 'transient' variables.
*/
public setTransientVariablesLocal(transientVariables: Map<string, any>): void {
for (const [key, value] of transientVariables) {
transientVariables.set(key, this.stringifyValue(value));
}
this.execution.setTransientVariablesLocal(transientVariables);
}

/**
* Similar to {@link #getVariableLocal(String)}, but for a transient variable. See {@link #setTransientVariable(String, Object)} for the rules on 'transient' variables.
*/
public getTransientVariableLocal(variableName: string): any {
return this.execution.getTransientVariableLocal(variableName);
return this.parseValue(this.execution.getTransientVariableLocal(variableName));
}

/**
* Similar to {@link #getVariableLocal(String)}, but for transient variables only. See {@link #setTransientVariable(String, Object)} for the rules on 'transient' variables.
*/
public getTransientVariablesLocal(): Map<string, any> {
return this.execution.getTransientVariablesLocal();
const transientVariablesLocal = this.execution.getTransientVariablesLocal();
for (const [key, value] of transientVariablesLocal) {
transientVariablesLocal.set(key, this.parseValue(value));
}
return transientVariablesLocal;
}

/**
Expand Down Expand Up @@ -544,6 +578,24 @@ class ExecutionContext {
this.execution.removeTransientVariablesLocal();
}

private parseValue(value: any) {
try {
return JSON.parse(value);
} catch (e) {
// Do nothing
}
return value;
}

private stringifyValue(value: any): any {
if (Array.isArray(value)) {
// @ts-ignore
return java.util.Arrays.asList(value.map(e => JSON.stringify(e)));
} else if (typeof value === 'object') {
return JSON.stringify(value);
}
return value;
}
}

// @ts-ignore
Expand Down

0 comments on commit 0ee6f88

Please sign in to comment.