-
Notifications
You must be signed in to change notification settings - Fork 17
Description
I've noticed strange behavior evaluating two instances of com.dashjoin.jsonata.Jsonata expressions in the same Thread - if the first expression has some bindings these bindings become part of second one. From the code snippet below, both result1 and result2 are equal to value but for result2 empty string is expected.
import static com.dashjoin.jsonata.Jsonata.jsonata;
public class Demo {
public static void main(String[] args) {
var expression = "$test";
var jsonata1 = jsonata(expression);
var environment = jsonata1.createFrame();
environment.bind("test", "value");
var jsonata2 = jsonata(expression);
var result1 = jsonata1.evaluate("", environment);
System.out.println(result1);
var result2 = jsonata2.evaluate("");
System.out.println(result2);
}
}Issue comes from com.dashjoin.jsonata.Jsonata#getPerThreadInstance method when it calls by com.dashjoin.jsonata.Jsonata#evaluate(com.dashjoin.jsonata.Parser.Symbol, java.lang.Object, com.dashjoin.jsonata.Jsonata.Frame)
Since ThreadLocal<Jsonata> current updates every time when you call jsonata(expression) for jsonata1.evaluate("", environment) it holds instance of jsonata2 and overrides its environment (line #140)
It's better to remove thread local variable from here, mark this class as not thread safe and let developers create new instances of Jsonata for different threads.