mikebrock / mvel

MVEL (MVFLEX Expression Language)
http://mvel.codehaus.org/
Apache License 2.0
50 stars 14 forks source link

null converts into "null" literal string #23

Open n3integration opened 11 years ago

n3integration commented 11 years ago

Running the following test against MVEL 2.1.3.Final, a null value is being converted into the string "null", which is not the desired behavior. The expectation is that a null value will remain null rather than be converted to the string literal null.

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import org.mvel2.MVEL;
import org.mvel2.ParserContext;

public class MVELTest {

    public static void main(String[] _args) {
        Map<String, Object> params = new HashMap<String, Object>();
        Map<String, Object> values = new HashMap<String, Object>();

        ParserContext ctx = new ParserContext();
        ctx.addImport("str", MVELTest.class);

        Serializable func = MVEL.compileExpression("def upper(s) { str.toUpper(s); }", ctx);
        MVEL.executeExpression(func, params);

        Serializable defn = MVEL.compileExpression("upper(mapped.value)", ctx);

        params.put("mapped", values);

        runThrough(1, null, defn, values, params);
        runThrough(100, "foo", defn, values, params);
        runThrough(1, null, defn, values, params);
    }

    public static void runThrough(int times, final String val, final Serializable defn, final Map<String, Object> values, final Map<String, Object> params) {
        for(int i = 0; i < times; ++i) {
            values.put("value", val);
            System.err.println(MVEL.executeExpression(defn, params));
        }
    }

    public static String toUpper(String val) {
        if(val == null) {
            return null;
        }
        return val.toUpperCase();
    }
}
n3integration commented 11 years ago

Cross-posting an issue from another user logged in the codehaus issue tracker.

Volanx commented 6 years ago

hello, we still encounter this problem, could you please share some suggestion? I tried to visit the link "http://jira.codehaus.org/browse/MVEL-274" in your comment, however it seems expired.