You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Henri Biestro (Jira)" <ji...@apache.org> on 2020/02/11 16:40:00 UTC

[jira] [Comment Edited] (JEXL-302) JexlScript.getVariables returns strange values for array access

    [ https://issues.apache.org/jira/browse/JEXL-302?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17034586#comment-17034586 ] 

Henri Biestro edited comment on JEXL-302 at 2/11/20 4:39 PM:
-------------------------------------------------------------

That subject is moot if you use a JexBuilder(). collectAll(false) as in:
{code:java}
        JexlEngine jexld = new JexlBuilder().collectAll(false).create();
        e = jexld.createScript("x.y[{'z': 't'}]");
        vars = e.getVariables();
        Assert.assertEquals(1, vars.size());
        Assert.assertTrue(eq(mkref(new String[][]{{"x", "y"}}), vars));}
 {code}
In the collectAll(true) case, the logic is in the actual intent of getVariables ; statically capture the elements that will potentially influence the script execution flow, ie all variables. Since one can configure JEXL so that a.b is equivalent to a['b'], it stands to reason that a a[['b', 'c’]] can be interpreted as [a.b, a.c]. , thus the a, ['b', 'c'] resulting of getVariables. The same logic can be applied to constant sets and maps.

For instance:
{code:java}
        script = JEXL.createScript("moon.landing[['yyyy', 'MM', 'dd']]");
        result = (List<String>) script.execute(ctxt);
        Assert.assertEquals(Arrays.asList("1969", "7", "20"), result);
{code}
In this example, the script
{code:java}
 moon.landing[['yyyy', 'MM', 'dd']]{code}
 is equivalent to 
{code:java}
 [ moon.landing.yyyy, moon.landing.MM, moon.landing.dd ]
{code}
With a bit of added parsing, it becomes possible to determine those from getVariables.
 I'm adding a test case to illustrate, [JEXL-302: added a test for JexBuilder.captureAll() and JexlScript.get…|https://github.com/apache/commons-jexl/commit/09db242b8cffbf63ad4a59d4e62f3ac358d4be24]


was (Author: henrib):
That subject is moot if you use a JexBuilder().captureAll(false) as in:
{code:java}
        JexlEngine jexld = new JexlBuilder().collectAll(false).create();
        e = jexld.createScript("x.y[{'z': 't'}]");
        vars = e.getVariables();
        Assert.assertEquals(1, vars.size());
        Assert.assertTrue(eq(mkref(new String[][]{{"x", "y"}}), vars));}
 {code}
In the captureAll(true) case, the logic is in the actual intent of getVariables ; statically capture the elements that will potentially influence the script execution flow, ie all variables. Since one can configure JEXL so that a.b is equivalent to a['b'], it stands to reason that a a[['b', 'c’]] can be interpreted as [a.b, a.c]. , thus the a, ['b', 'c'] resulting of getVariables. The same logic can be applied to constant sets and maps.

For instance:
{code:java}
        script = JEXL.createScript("moon.landing[['yyyy', 'MM', 'dd']]");
        result = (List<String>) script.execute(ctxt);
        Assert.assertEquals(Arrays.asList("1969", "7", "20"), result);
{code}
In this example, the script
{code:java}
 moon.landing[['yyyy', 'MM', 'dd']]{code}
 is equivalent to 
{code:java}
 [ moon.landing.yyyy, moon.landing.MM, moon.landing.dd ]
{code}
With a bit of added parsing, it becomes possible to determine those from getVariables.
 I'm adding a test case to illustrate, [JEXL-302: added a test for JexBuilder.captureAll() and JexlScript.get…|https://github.com/apache/commons-jexl/commit/09db242b8cffbf63ad4a59d4e62f3ac358d4be24]

> JexlScript.getVariables returns strange values for array access
> ---------------------------------------------------------------
>
>                 Key: JEXL-302
>                 URL: https://issues.apache.org/jira/browse/JEXL-302
>             Project: Commons JEXL
>          Issue Type: Bug
>    Affects Versions: 3.1
>            Reporter: Dmitri Blinov
>            Assignee: Henri Biestro
>            Priority: Minor
>             Fix For: 3.2
>
>
> I can not understand the logic behind the current implementation of {{JexlScript.getVariables()}} method. From the documentation we know that the result should be the set of script variables. For the code
> {code:java}
> a[b][c]{code}
> it gives three variables {{a}}, {{b}}, {{c}}. So far so good. But for the code
> {code:java}
> a[b]['c']{code}
> it returns {{a}} and {{b c}}, where second variable has two fragments {{b}} and {{c}}. The documentation states that variables with multiple fragments are ant-ish variables, but I don't have any of ant-ish variables in the example, and {{'c'}} is not a variable, but a constant. I expect to get {{a}} and {{b}} as a result.



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