David Costanzo created JEXL-327:
-----------------------------------

             Summary: map[null] does not work in assignment context
                 Key: JEXL-327
                 URL: https://issues.apache.org/jira/browse/JEXL-327
             Project: Commons JEXL
          Issue Type: Bug
    Affects Versions: 3.1
            Reporter: David Costanzo


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 
{code:java}
map[null] = 'bar'{code}
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:
{code:java}
{
   null : "Unknown",
   1    : "MALE",
   2    : "FEMALE"
}[DMSEX]{code}
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:*
{code:java}
@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);
}{code}
*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".

 



--
This message was sent by Atlassian Jira
(v8.3.4#803005)

Reply via email to