henrib commented on code in PR #425:
URL: https://github.com/apache/commons-jexl/pull/425#discussion_r4547770834
##########
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()
Review Comment:
Not an issue: the cast is inside the `if (methodNode instanceof
ASTIdentifierAccess)` branch that opens a few lines above, so it cannot throw.
When the method member is any other node type, the `else` branch evaluates it
instead.
##########
src/main/java/org/apache/commons/jexl3/JexlOptions.java:
##########
@@ -535,6 +551,8 @@ public void setNamespaces(final Map<String, Object> ns) {
* <p>If safe, encountering null during a navigation expression -
dereferencing a method or a field through a null
* object or property - will <em>not</em> be considered an error but
evaluated as <em>null</em>. It is recommended
* to use <em>setSafe(false)</em> as an explicit default.</p>
+ * <p>If safe, every access behaves as a safe access, {@code x.y} as
{@code x?.y}; whether this also
+ * applies to what is undefined is determined by {@link
#setSafeUndefined(boolean)}.</p>
Review Comment:
Fixed in 816b020b: the paragraph now states that the equivalence between
`x.y` and `x?.y` holds when `x` is null, and that what is undefined is
determined by `setSafeUndefined`.
##########
src/main/java/org/apache/commons/jexl3/internal/InterpreterBase.java:
##########
@@ -770,6 +771,82 @@ protected boolean isSilent() {
return options.isSilent();
}
+ /**
+ * Whether safe navigation applies to what is undefined in this
interpreter.
+ *
+ * @return true if safe navigation applies to undefined and null, false if
it only applies to null
+ */
+ protected boolean isSafeUndefined() {
+ return options.isSafeUndefined();
+ }
+
+ /**
+ * Whether safe navigation excuses an undefined property.
+ * <p>The access to the property is explicitly safe, as in x?.y or x?[y],
or the access that follows it is,
+ * as in x.y?.z; the safe option does not excuse an undefined property.</p>
+ *
+ * @param node the node accessing the property
+ * @return true if the property evaluates as null, false if it is an error
+ */
+ protected boolean isSafeUndefined(final JexlNode node) {
+ if (!isSafeUndefined()) {
+ return false;
+ }
+ JexlNode access = node;
+ JexlNode parent = node.jjtGetParent();
+ if (parent instanceof ASTArrayAccess) {
+ // an index, as in x?[y] or x[y]?[z]
+ final ASTArrayAccess indexed = (ASTArrayAccess) parent;
+ final int next = indexOf(indexed, node) + 1;
+ if (indexed.isSafeChild(next - 1)) {
+ return true;
+ }
Review Comment:
Not an issue: `indexed` is `node.jjtGetParent()`, so `node` is always one of
its children and `indexOf` cannot return -1 here. `indexOf` is a private
helper, and `isSafeChild` only tests a bit, so it would not fail either.
##########
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 String err = "error";
+ // 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;
+ final JexlScript script = jexl.createScript(src);
+ if (expected == err) {
+ assertThrows(JexlException.class, () ->
script.execute(ctxt), msg);
+ } else {
+ assertEquals(expected, script.execute(ctxt), msg);
Review Comment:
Fixed in 816b020b: the marker is now a dedicated `Object`.
--
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]