Skip to content

Evaluation of different Jsonata instances in the same thread causes issue #93

@kancko

Description

@kancko

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)

Image

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)

Image

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions