Copilot commented on code in PR #425:
URL: https://github.com/apache/commons-jexl/pull/425#discussion_r4549004464
##########
src/main/java/org/apache/commons/jexl3/internal/Interpreter.java:
##########
@@ -1721,7 +1721,7 @@ private Object visit(final ASTMethodNode node, final
Object antish, final Object
object = data;
if (object == null) {
// no object, we fail
- return node.isSafeLhs(isSafe())
+ return isSafe() || ((ASTIdentifierAccess)
methodNode).isSafe()
? null
: unsolvableMethod(methodNode, "<null>.<?>(...)");
Review Comment:
This unconditionally casts `methodNode` to `ASTIdentifierAccess`. If the
invoked member is not an identifier access (e.g., an array-based access like
`x?['m']()` or any other non-`ASTIdentifierAccess` form), this will throw
`ClassCastException` during evaluation. Use a safe-access predicate that
supports both identifier and array access (e.g., `isSafeAccess(...)`), or guard
the cast with `instanceof` and fall back appropriately.
##########
src/site/xdoc/reference/syntax.xml:
##########
@@ -291,6 +291,10 @@
In the previous expression, if 'foo' is null, the
whole expression will evaluate as null.
Note that this can also be used in a chain as in
<code>x?[y]?[z]</code>.
</p>
+ <p>Whether safe-access operators shortcut non-existent
references - an undefined variable,
+ property or method - as they do null is determined by
the <code>safeUndefined</code> option,
Review Comment:
The new behavior and the PR description/test matrix indicate `safeUndefined`
also impacts undefined functions (and not just variables/properties/methods).
This doc block should be updated to mention functions as well so users
understand what is covered by `safeUndefined`.
##########
src/test/java/org/apache/commons/jexl3/Issues400Test.java:
##########
@@ -1393,5 +1393,76 @@ void test475() {
assertNull(jexl.createScript("#pragma jexl.options
'-strict'\nfoo").execute(null));
assertThrows(JexlException.Variable.class, () ->
jexl.createScript("foo").execute(null));
}
-}
+ @Test
+ void test476() {
+ final Object err = new Object();
+ // an expression and its result when (safeUndefined, safe) is (true,
false), (true, true), (false, false), (false, true)
+ final Object[][] cases = {
+ // an undefined variable
+ {"u", err, err, err, err},
+ {"u.y", err, null, err, err},
+ {"u?.y", null, null, err, err},
+ {"u[0]", err, null, err, err},
+ {"u?[0]", null, null, err, err},
+ {"u.m()", err, null, err, err},
+ {"u?.m()", null, null, err, err},
+ {"u == null", err, err, err, err},
+ {"u ?? 1", 1, 1, 1, 1},
+ // an undefined property
+ {"'abc'.nothing", err, err, err, err},
+ {"'abc'?.nothing", null, null, err, err},
+ {"'abc'['nothing']", err, err, err, err},
+ {"'abc'?['nothing']", null, null, err, err},
+ {"'abc'.nothing.y", err, err, err, err},
+ {"'abc'.nothing?.y", null, null, err, err},
+ {"'abc'.nothing ?? 1", 1, 1, 1, 1},
+ // an undefined method
+ {"'abc'.nothing()", err, null, err, err},
+ {"'abc'?.nothing()", null, null, err, err},
+ {"'abc'.nothing().y", err, null, err, err},
+ {"'abc'.nothing()?.y", null, null, err, err},
+ {"'abc'.nothing() ?? 1", 1, 1, 1, 1},
+ // an undefined function
+ {"nothing(1)", err, err, err, err},
+ {"nothing(1).y", err, null, err, err},
+ {"nothing(1)?.y", null, null, err, err},
+ {"nothing(1) ?? 1", 1, 1, 1, 1},
+ // null is not undefined
+ {"nul", null, null, null, null},
+ {"nul.y", err, null, err, null},
+ {"nul?.y", null, null, null, null},
+ {"nul?.y.z", null, null, null, null},
+ {"nul[0]", err, null, err, null},
+ {"nul?[0]", null, null, null, null},
+ {"nul.m()", err, null, err, null},
+ {"nul?.m()", null, null, null, null},
+ {"nul(1)", err, err, err, err},
+ {"nul + 1", err, err, err, err},
+ {"nul.y ?? 1", 1, 1, 1, 1},
+ };
+ final JexlContext ctxt = new MapContext();
+ ctxt.set("nul", null);
+ for (int m = 0; m < 4; ++m) {
+ final boolean undef = (m & 2) == 0;
+ final boolean safe = (m & 1) != 0;
+ final JexlEngine jexl = new
JexlBuilder().strict(true).safe(safe).safeUndefined(undef).create();
+ for (final Object[] c : cases) {
+ final String src = (String) c[0];
+ final Object expected = c[m + 1];
+ final String msg = src + ", safeUndefined: " + undef + ",
safe: " + safe;
Review Comment:
The local name `undef` is easy to misread as 'isUndefined' rather than
'safeUndefined enabled'. Renaming it to `safeUndefined` (or similar) would make
the test matrix easier to understand and reduce cognitive load when reading
assertions and failure messages.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]