[jira] Resolved: (JEXL-26) ASTArrayAccess messes up on fallback to JexlContext

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

[jira] Resolved: (JEXL-26) ASTArrayAccess messes up on fallback to JexlContext

JIRA jira@apache.org

     [ https://issues.apache.org/jira/browse/JEXL-26?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

dion gillard resolved JEXL-26.
------------------------------

    Resolution: Fixed

This is fixed in the 2.0 branch.
There is a test in ArrayAccessTest.testArrayAndDottedConflict() which makes sure it works correctly.

> ASTArrayAccess messes up on fallback to JexlContext
> ---------------------------------------------------
>
>                 Key: JEXL-26
>                 URL: https://issues.apache.org/jira/browse/JEXL-26
>             Project: Commons JEXL
>          Issue Type: Bug
>    Affects Versions: 1.1
>         Environment: N/A
>            Reporter: Colin Lear
>             Fix For: 2.0
>
>
> When executing an expression if a null comes up (unresolved object) it attempts to build an identifier to query the JexlContext directly. However it only works on sequential ASTIdentifier nodes. If there is another sort of node then it ends up searching the JexlContext for a mangled or malformed variable.
> I think this is a bug because subobjects with properties or methods that are null can "inherit" the value of objects or properties in the base JexlContext scope. It's too unpredictable to be a legitimate feature.
> for example in the expression: objects[23].status
> if (objects[23] != null && objects[23].status == null) then it will search for the bare expression "status" in the JexlContext. Ideally it would query the JexlContext map for the full expression, however the details of that are difficult to implement and can produce odd results.
> Even worse with an expression like: base.objects[23].status.
> if (base != null && base.objects[23] != null && base.objects[23].status == null) then this expression will evaluate look for "base.status" in the JexlContext map.
> It currently works as expected for expressions like base.object.status by searching for "base.object.status" in the JexlContext if status is null. Although I'm not entirely convinced that it is a good idea to do this.
> Simple Fix:
> in ASTReference.java change the function getIdentifierToDepth() to return an empty string if there are non ASTIdentifier Nodes in the expression (in my case it's ASTArrayAccess nodes). This is the simplest solution and stops subobjects effectively assuming values of objects in the base JexlContext while still allowing you to bind objects into the JexlContext with identifiers with "." in the name.
> So you could add "object.tracking.status" directly into the JexlContext and it would work. However you couldn't add object.tracking[23].status because one of the Nodes is an ArrayAccess node and it can't be translated back into a string by the current implementation.
> /* patched function */
>     private String getIdentifierToDepth(int i) {
>         StringBuffer varName = new StringBuffer();
>         for (int j = 0; j <= i; j++) {
>             SimpleNode node = (SimpleNode) jjtGetChild(j);
>             if (node instanceof ASTIdentifier) {
>                 varName.append(((ASTIdentifier) node).getIdentifierString());
>                 if (j != i) {
>                     varName.append('.');
>                 }
>             } else {
>                 /* expression is unresolvable and variable name is invalid */
>                 return "";
>             }
>         }
>         return varName.toString();
>     }
> You could always add some code to support ASTArrayAccess nodes. However you'd also have to support either evaluating, or reducing to a string, the index expression. Which is messy.

--
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.