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

Henri Biestro updated JEXL-476:
-------------------------------
    Description: 
h3. Problem
JEXL does not separate two different questions consistently:
* *null*: the object an access applies to has no value;
* *undefined*: a variable, a property, a method or a function does not exist.

Four mechanisms can excuse them (the safe access operators, the engine {{safe}} 
option, the null-coalescing operator, and {{?:}} / ternary), and each covers a 
different subset. On a strict engine, with a String that has no {{nothing}} 
member:

||Expression||safe(false)||safe(true)||
|{{'abc'?.nothing}}|null|null|
|{{'abc'?.nothing()}}|null|null|
|{{'abc'.nothing?.y}}|error|error|
|{{'abc'.nothing()?.y}}|null|null|
|{{'abc'.nothing}}|error|error|
|{{'abc'.nothing()}}|error|null|
|{{nothing(1)}}|error|error|
|{{nothing(1).y}}|error|null|
|{{'abc'.nothing ?? 1}}|1|1|
|{{'abc'.nothing() ?? 1}}|error|error|

A missing method is excused by a following {{?.}} and by the {{safe}} option 
but not by the null-coalescing operator; a missing property is the exact 
opposite. A property may hold a function, so the two should not differ.

h3. Solution
Making safe navigation null-only outright would break default engines (strict 
and safe) and scripts that rely on JEXL-290, so the null-only model is an 
option: {{safeUndefined}}, true by default.
* {{JexlOptions.setSafeUndefined(boolean)}} / {{isSafeUndefined()}}, 
{{JexlBuilder.safeUndefined(boolean)}};
* as a flag name, {{"-safeUndefined"}}, in {{JexlOptions.setFlags}}, 
{{JexlOptions.setDefaultFlags}} and the {{jexl.options}} pragma.

*safeUndefined false*: safe navigation applies to null only. {{x?.y}}, 
{{x?.y()}}, {{x?\[y\]}}, and any access with the {{safe}} option, evaluate as 
null when {{x}} is null. An undefined variable, a missing property, a missing 
method or an unsolvable function is an error, with or without a safe access 
operator or the {{safe}} option. There is no exception to that rule.

*safeUndefined true* (default): safe navigation applies to undefined as it does 
to null, as it does today, with the safe access operators made consistent:
* {{x?.y}}, {{x?.y()}} and {{x?\[y\]}} evaluate as null when {{x}} is null or 
undefined, or when {{x}} does not have the member {{y}};
* what is undefined evaluates as null when the access that follows is safe, as 
in {{x.y?.z}} or {{x.y()?.z}}, properties and methods alike;
* the {{safe}} option excuses an undefined variable or a missing method for 
every access.

*In both cases*:
* an undefined variable, property, method or function that is not navigated is 
an error on a strict engine;
* the null-coalescing operator, {{?:}} and the condition of a ternary tolerate 
what is undefined in their first operand, now including a missing method or 
function. Null-coalescing tests for null or undefined only; {{?:}} and the 
ternary coerce to boolean, so {{0 ?: 1}} is 1 where {{0 ?? 1}} is 0.

On a strict engine, where {{nul}} is a null variable, {{u}} an undefined 
variable, and a String has no {{nothing}} member:

||Expression||true||true, safe||false||false, safe||
|{{nul.y}}|error|null|error|null|
|{{nul?.y}}|null|null|null|null|
|{{u}}|error|error|error|error|
|{{u.y}}|error|null|error|error|
|{{u?.y}}|null|null|error|error|
|{{'abc'.nothing}}|error|error|error|error|
|{{'abc'?.nothing}}|null|null|error|error|
|{{'abc'?\['nothing'\]}}|null|null|error|error|
|{{'abc'.nothing?.y}}|null|null|error|error|
|{{'abc'.nothing()}}|error|null|error|error|
|{{'abc'?.nothing()}}|null|null|error|error|
|{{'abc'.nothing()?.y}}|null|null|error|error|
|{{nothing(1)}}|error|error|error|error|
|{{nothing(1).y}}|error|null|error|error|
|{{nothing(1)?.y}}|null|null|error|error|
|{{'abc'.nothing ?? 1}}|1|1|1|1|
|{{'abc'.nothing() ?? 1}}|1|1|1|1|

h3. Exceptions kept for compatibility
With safeUndefined true and the {{safe}} option, two released behaviours do not 
follow the model; removing them would change what default engines do:
* a missing property of a non-null object is an error where a missing method is 
null: {{x.missing}} versus {{x.missing()}};
* an undefined variable that is the value of an assignment evaluates as null: 
{{var x = u}} (JEXL-275).

Neither exists with safeUndefined false.

h3. Compatibility
With the default (safeUndefined true), the behaviour changes are:
* a missing property followed by a safe access evaluates as null, as a missing 
method does: {{x.missing?.y}}, {{x.missing?\[0\]}} (were errors);
* the safe array access excuses a missing member as {{?.}} does: 
{{x?\['missing'\]}}, {{nothing(1)?\[0\]}} (were errors);
* the null-coalescing operator, {{?:}} and the ternary tolerate a missing 
method or function (were errors);
* a safe access no longer hides an exception thrown by a property getter: 
{{bean?.boom}} is an error when {{getBoom()}} throws (was null).

No existing test needed to change.

h3. Related
JEXL-474 already applies the null half of the rule: only the access applied to 
null decides, so {{m?.get('z').y}} is an error when {{m.get('z')}} is null, and 
the {{safe}} option covers array accesses.


  was:
  h3. Problem
  JEXL does not separate two different questions consistently:
  * *null*: the object an access applies to has no value;
  * *undefined*: a variable, a property or a method does not exist.

  Four mechanisms can excuse them ({{?.}}, the engine {{safe}} option, {{??}}, 
and {{?:}} / ternary), and each covers a different subset. On a strict engine, 
with a String that has no {{nothing}} member:

  ||Expression||safe(false)||safe(true)||
  |{{'abc'?.nothing}}|null|null|
  |{{'abc'?.nothing()}}|null|null|
  |{{'abc'.nothing?.y}}|error|error|
  |{{'abc'.nothing()?.y}}|null|null|
  |{{'abc'.nothing}}|error|error|
  |{{'abc'.nothing()}}|error|null|
  |{{nothing(1)}}|error|error|
  |{{nothing(1).y}}|error|null|
  |{{'abc'.nothing ?? 1}}|1|1|
  |{{'abc'.nothing() ?? 1}}|error|error|

  A missing method is excused by a following {{?.}} and by the {{safe}} option 
but not by {{??}}; a missing property is the exact opposite. A property may 
hold a function, so the two should not differ.

  h3. Proposal
  Safe navigation is about null only; undefined has its own operators.

  ||Condition||{{?.}} / {{?[}}||{{safe}} option||{{??}}||{{?:}} and 
ternary||strict(false)||
  |null value|null|null|right operand|falsy|null|
  |missing property, method or function of a non-null object|error|error|right 
operand|falsy|null|

  * {{x?.y}}, {{x?.y()}} and {{x?[y]}} evaluate as null when {{x}} is null. 
They no longer excuse a missing property or method on a non-null object, 
whether the safe access is the failing one or the one that
  follows.
  * The {{safe}} option does the same for every access, without the explicit 
operator.
  * The null-coalescing operator, {{?:}} and the ternary tolerate a missing 
method or function in their first operand, as they already do for an undefined 
variable or a missing property. Null-coalescing tests for
  null or undefined only; {{?:}} and the ternary coerce to boolean, so {{0 ?: 
1}} is 1 where {{0 ?? 1}} is 0.

  An undefined variable at the root of a navigation is left out of this ticket: 
{{u?.y}}, and {{u.y}} with the {{safe}} option, keep evaluating as null. JEXL 
can tell an undefined variable from a null one
  ({{JexlContext.has}} for globals, locals are known at parse time), so the 
same rule could apply. It is left out because both mechanisms already agree 
there and because making {{u?.y}} an error is a wider break;


  h3. Compatibility
  This changes released behaviour (JEXL-290):
  * {{x?.missing()}} and {{x?.missing}} on a non-null {{x}} become errors on a 
strict engine;
  * {{x.missing()?.y}} becomes an error;
  * with {{safe(true)}}, a missing method becomes an error.

  Scripts that use {{?.}} for duck typing should use {{x.maybe() ?? fallback}} 
instead, which this ticket makes work for methods.

  h3. Related
  JEXL-474 already applies the null half of the rule: only the access applied 
to null decides, so {{m?.get('z').y}} is an error when {{m.get('z')}} is null, 
and the {{safe}} option covers array accesses.



> Safe navigation should only shortcut null, not undefined properties or methods
> ------------------------------------------------------------------------------
>
>                 Key: JEXL-476
>                 URL: https://issues.apache.org/jira/browse/JEXL-476
>             Project: Commons JEXL
>          Issue Type: Improvement
>    Affects Versions: 3.7.1
>            Reporter: Henri Biestro
>            Assignee: Henri Biestro
>            Priority: Major
>             Fix For: 3.7.2
>
>
> h3. Problem
> JEXL does not separate two different questions consistently:
> * *null*: the object an access applies to has no value;
> * *undefined*: a variable, a property, a method or a function does not exist.
> Four mechanisms can excuse them (the safe access operators, the engine 
> {{safe}} option, the null-coalescing operator, and {{?:}} / ternary), and 
> each covers a different subset. On a strict engine, with a String that has no 
> {{nothing}} member:
> ||Expression||safe(false)||safe(true)||
> |{{'abc'?.nothing}}|null|null|
> |{{'abc'?.nothing()}}|null|null|
> |{{'abc'.nothing?.y}}|error|error|
> |{{'abc'.nothing()?.y}}|null|null|
> |{{'abc'.nothing}}|error|error|
> |{{'abc'.nothing()}}|error|null|
> |{{nothing(1)}}|error|error|
> |{{nothing(1).y}}|error|null|
> |{{'abc'.nothing ?? 1}}|1|1|
> |{{'abc'.nothing() ?? 1}}|error|error|
> A missing method is excused by a following {{?.}} and by the {{safe}} option 
> but not by the null-coalescing operator; a missing property is the exact 
> opposite. A property may hold a function, so the two should not differ.
> h3. Solution
> Making safe navigation null-only outright would break default engines (strict 
> and safe) and scripts that rely on JEXL-290, so the null-only model is an 
> option: {{safeUndefined}}, true by default.
> * {{JexlOptions.setSafeUndefined(boolean)}} / {{isSafeUndefined()}}, 
> {{JexlBuilder.safeUndefined(boolean)}};
> * as a flag name, {{"-safeUndefined"}}, in {{JexlOptions.setFlags}}, 
> {{JexlOptions.setDefaultFlags}} and the {{jexl.options}} pragma.
> *safeUndefined false*: safe navigation applies to null only. {{x?.y}}, 
> {{x?.y()}}, {{x?\[y\]}}, and any access with the {{safe}} option, evaluate as 
> null when {{x}} is null. An undefined variable, a missing property, a missing 
> method or an unsolvable function is an error, with or without a safe access 
> operator or the {{safe}} option. There is no exception to that rule.
> *safeUndefined true* (default): safe navigation applies to undefined as it 
> does to null, as it does today, with the safe access operators made 
> consistent:
> * {{x?.y}}, {{x?.y()}} and {{x?\[y\]}} evaluate as null when {{x}} is null or 
> undefined, or when {{x}} does not have the member {{y}};
> * what is undefined evaluates as null when the access that follows is safe, 
> as in {{x.y?.z}} or {{x.y()?.z}}, properties and methods alike;
> * the {{safe}} option excuses an undefined variable or a missing method for 
> every access.
> *In both cases*:
> * an undefined variable, property, method or function that is not navigated 
> is an error on a strict engine;
> * the null-coalescing operator, {{?:}} and the condition of a ternary 
> tolerate what is undefined in their first operand, now including a missing 
> method or function. Null-coalescing tests for null or undefined only; {{?:}} 
> and the ternary coerce to boolean, so {{0 ?: 1}} is 1 where {{0 ?? 1}} is 0.
> On a strict engine, where {{nul}} is a null variable, {{u}} an undefined 
> variable, and a String has no {{nothing}} member:
> ||Expression||true||true, safe||false||false, safe||
> |{{nul.y}}|error|null|error|null|
> |{{nul?.y}}|null|null|null|null|
> |{{u}}|error|error|error|error|
> |{{u.y}}|error|null|error|error|
> |{{u?.y}}|null|null|error|error|
> |{{'abc'.nothing}}|error|error|error|error|
> |{{'abc'?.nothing}}|null|null|error|error|
> |{{'abc'?\['nothing'\]}}|null|null|error|error|
> |{{'abc'.nothing?.y}}|null|null|error|error|
> |{{'abc'.nothing()}}|error|null|error|error|
> |{{'abc'?.nothing()}}|null|null|error|error|
> |{{'abc'.nothing()?.y}}|null|null|error|error|
> |{{nothing(1)}}|error|error|error|error|
> |{{nothing(1).y}}|error|null|error|error|
> |{{nothing(1)?.y}}|null|null|error|error|
> |{{'abc'.nothing ?? 1}}|1|1|1|1|
> |{{'abc'.nothing() ?? 1}}|1|1|1|1|
> h3. Exceptions kept for compatibility
> With safeUndefined true and the {{safe}} option, two released behaviours do 
> not follow the model; removing them would change what default engines do:
> * a missing property of a non-null object is an error where a missing method 
> is null: {{x.missing}} versus {{x.missing()}};
> * an undefined variable that is the value of an assignment evaluates as null: 
> {{var x = u}} (JEXL-275).
> Neither exists with safeUndefined false.
> h3. Compatibility
> With the default (safeUndefined true), the behaviour changes are:
> * a missing property followed by a safe access evaluates as null, as a 
> missing method does: {{x.missing?.y}}, {{x.missing?\[0\]}} (were errors);
> * the safe array access excuses a missing member as {{?.}} does: 
> {{x?\['missing'\]}}, {{nothing(1)?\[0\]}} (were errors);
> * the null-coalescing operator, {{?:}} and the ternary tolerate a missing 
> method or function (were errors);
> * a safe access no longer hides an exception thrown by a property getter: 
> {{bean?.boom}} is an error when {{getBoom()}} throws (was null).
> No existing test needed to change.
> h3. Related
> JEXL-474 already applies the null half of the rule: only the access applied 
> to null decides, so {{m?.get('z').y}} is an error when {{m.get('z')}} is 
> null, and the {{safe}} option covers array accesses.



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

Reply via email to