Details
-
Bug
-
Status: Closed
-
Trivial
-
Resolution: Fixed
-
3.1
-
None
Description
In JEXL, you can create a map with a null key by using a map literal and you can get the value associated with a null key. However, you can't assign a value to a null key, as in
map[null] = 'bar'
This asymmetry is weird and might be an oversight.
Impact:
This has not blocked us from doing anything, so the impact nearly zero. It's something I found while writing unit tests for my application, but it does not impact my users. I'm mostly reporting it as a "language weirdness", in case the JEXL developers care.
That said, we do use null keys in maps. In my domain (working with data from clinical trials), a null numeric value means "missing", which is a valid value. Our JEXL programmers implement "switch" statements using a map, so you might see an expression that translates a coded variable named "DMSEX" into an English description like:
{ null : "Unknown", 1 : "MALE", 2 : "FEMALE" }[DMSEX]
Fortunately, this works as expected. This bug only prevents us from building the lookup map programmatically, but we prefer using a literal, anyway.
Steps to Reproduce:
@Test public void testSetNullKey() throws IOException { JexlEngine jexl = new JexlBuilder().create(); JexlContext jc = new MapContext(); JexlExpression expression1 = jexl.createExpression( "(function () {\n" + " var map = {null : 'foo'};\n" + " map[null] = 'bar';\n" + " return map[null];\n" + "})()"); // JEXL 3.1, throws JexlException$Property "unsolvable property '<?>.<null>'" // JEXL 3.2, throws JexlException$Property "undefined property '<?>.<null>'" Object o1 = expression1.evaluate(jc); Assert.assertEquals("bar", o1); }
What Happens:
JEXL throws a JexlException.Property exception when it tries to evaluate "map[null] = 'bar'".
Expected Result:
"map[null] = 'bar'" changes the null key's value from "foo" to "bar" and the function returns "bar".