Henri Biestro created JEXL-476:
----------------------------------
Summary: 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
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 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|
|undefined variable, property, method or function|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.
* {{??}}, {{?:}} 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. {{??}} tests for null or undefined only; {{?:}}
and the ternary coerce to boolean, so {{0 ?: 1}} is 1 where {{0 ?? 1}} is 0.
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.
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)