Repository navigation
Conversation
… null - JexlOptions.setSafeUndefined(boolean), JexlBuilder.safeUndefined(boolean), true by default; usable as a flag name and through the jexl.options pragma; - when false, safe navigation only applies to null; an undefined variable, property, method or function is an error, with or without ?., ?[ or the safe option; - when true, the safe access operators treat properties, methods and array accesses alike: x.missing?.y and x?['missing'] evaluate as null as x.missing()?.y and x?.missing do; - ??, ?: and the ternary condition tolerate a missing method or function; - a safe access no longer hides an exception thrown by a property getter. Co-Authored-By: Claude <noreply@anthropic.com>
There was a problem hiding this comment.
Warning
Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.
Copilot review overview
4 open issues
This introduces an unconditional cast ofmethodNodetoASTIdentifierAccess. In this codebase,… · New This wording is misleading in light of the documented compatibility exceptions (notably: with… · NewindexOf(indexed, node)can return-1if the passednodeis not a direct child ofindexed,… · New The test usesexpected == errwhereerris aStringsentinel. This is brittle because it… · New
What changed in this PR
Adds a new safeUndefined option (default true) to control whether safe navigation (?., ?[, safe) also short-circuits undefined references (missing variables/properties/methods/functions), enabling a “null-only” safe-navigation model when disabled.
Changes:
- Introduces
safeUndefinedflag inJexlOptionsand exposes it viaJexlBuilder. - Updates interpreter safe-navigation behavior for undefined references and ternary/elvis/null-coalescing protection.
- Adds regression tests for the four combinations of
(safeUndefined, safe)and updates syntax docs/changelog.
| File | Description |
|---|---|
| src/test/java/org/apache/commons/jexl3/Issues400Test.java | Adds test476 matrix covering safeUndefined/safe combinations and pragma behavior. |
| src/site/xdoc/reference/syntax.xml | Documents how safeUndefined affects safe-access operators. |
| src/main/java/org/apache/commons/jexl3/parser/JexlNode.java | Adjusts safe-LHS detection to consider safe array access children. |
| src/main/java/org/apache/commons/jexl3/internal/InterpreterBase.java | Adds safeUndefined helpers and refines undefined-property/variable/method handling. |
| src/main/java/org/apache/commons/jexl3/internal/Interpreter.java | Updates call/reference evaluation to honor safeUndefined and track undefined roots. |
| src/main/java/org/apache/commons/jexl3/JexlOptions.java | Adds safeUndefined flag, defaulting on, with public getter/setter and docs. |
| src/main/java/org/apache/commons/jexl3/JexlBuilder.java | Exposes safeUndefined() and safeUndefined(boolean) in the builder API. |
| src/changes/changes.xml | Adds release-note entry for JEXL-476. |
Review effort: Lite
| if (object == null) { | ||
| // no object, we fail | ||
| return node.isSafeLhs(isSafe()) | ||
| return isSafe() || ((ASTIdentifierAccess) methodNode).isSafe() |
There was a problem hiding this 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.
| * <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> |
There was a problem hiding this comment.
Fixed in 816b020: 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.
| final int next = indexOf(indexed, node) + 1; | ||
| if (indexed.isSafeChild(next - 1)) { | ||
| return true; | ||
| } |
There was a problem hiding this 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.
| if (expected == err) { | ||
| assertThrows(JexlException.class, () -> script.execute(ctxt), msg); | ||
| } else { | ||
| assertEquals(expected, script.execute(ctxt), msg); |
There was a problem hiding this comment.
Fixed in 816b020: the marker is now a dedicated Object.
…ker in test476 Address review comments: the safe option is equivalent to a safe access for null only, what is undefined being ruled by safeUndefined. Co-Authored-By: Claude <noreply@anthropic.com>
There was a problem hiding this comment.
Warning
Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.
Copilot review overview
7 open issues
This unconditionally castsmethodNodetoASTIdentifierAccess. If the invoked member is not an… · New This introduces an unconditional cast ofmethodNodetoASTIdentifierAccess. In this codebase,… The new behavior and the PR description/test matrix indicatesafeUndefinedalso impacts undefined… · New The test usesexpected == errwhereerris aStringsentinel. This is brittle because it…indexOf(indexed, node)can return-1if the passednodeis not a direct child ofindexed,… This wording is misleading in light of the documented compatibility exceptions (notably: with… The local nameundefis easy to misread as 'isUndefined' rather than 'safeUndefined enabled'.… · New
Review effort: Lite
| return isSafe() || ((ASTIdentifierAccess) methodNode).isSafe() | ||
| ? null | ||
| : unsolvableMethod(methodNode, "<null>.<?>(...)"); |
There was a problem hiding this comment.
The cast could not throw, it is inside the if (methodNode instanceof ASTIdentifierAccess) branch. Replaced by isSafeAccess(methodNode) in 4e2f8b3 anyway, which is equivalent there and reads better.
There was a problem hiding this comment.
Warning
Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.
Copilot review overview
10 open issues
This introduces a hard cast to ASTIdentifierAccess. IfmethodNodecan ever be anASTArrayAccess… · New This unconditionally castsmethodNodetoASTIdentifierAccess. If the invoked member is not an… This introduces an unconditional cast ofmethodNodetoASTIdentifierAccess. In this codebase,… This changed the error node passed intounsolvableMethod(...)frommethodNodetonode. That… · New ThesafeUndefinedoption also applies to undefined functions per the PR description/API docs. The… · New The new behavior and the PR description/test matrix indicatesafeUndefinedalso impacts undefined… The test usesexpected == errwhereerris aStringsentinel. This is brittle because it…indexOf(indexed, node)can return-1if the passednodeis not a direct child ofindexed,… This wording is misleading in light of the documented compatibility exceptions (notably: with… The local nameundefis easy to misread as 'isUndefined' rather than 'safeUndefined enabled'.…
Review effort: Lite
| object = data; | ||
| if (object == null) { | ||
| // no object, we fail | ||
| return node.isSafeLhs(isSafe()) | ||
| return isSafe() || ((ASTIdentifierAccess) methodNode).isSafe() | ||
| ? null | ||
| : unsolvableMethod(methodNode, "<null>.<?>(...)"); |
There was a problem hiding this comment.
The cast could not throw, it is inside the if (methodNode instanceof ASTIdentifierAccess) branch. Replaced by isSafeAccess(methodNode) in 4e2f8b3 anyway, which is equivalent there and reads better.
| return isSafeUndefined() && node.isSafeLhs(isSafe()) | ||
| ? null | ||
| : unsolvableMethod(methodNode, "<?>.<null>(...)"); | ||
| : unsolvableMethod(node, "<?>.<null>(...)"); |
There was a problem hiding this comment.
Intended: the ternary, ?: and ?? protection walks up from the node the error is raised on, and stops at the method node when started from its first child. Raised from methodNode, a call on a null result such as map.get('z')(1) ?? 1 is an error on a strict engine; raised from node, it evaluates as 1, as the other method errors do. 4e2f8b3 adds that expression to test476.
| <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, | ||
| true by default; when false, only null is shortcut and a non-existent reference is an error. |
There was a problem hiding this comment.
Fixed in 4e2f8b3: the paragraph now reads "an undefined variable, property, method or function".
…w cleanups Address review comments: use isSafeAccess instead of a cast when calling a method without an object, name the safeUndefined flag in test476 and check a call on a null result is tolerated by the null-coalescing operator. Co-Authored-By: Claude <noreply@anthropic.com>



JEXL-476, on top of #424 (JEXL-474).
Safe navigation (
?.,?[, thesafeoption) excuses both null (the object an access applies to has no value)and undefined (a variable, property, method or function does not exist), and does so differently for properties,
methods and array accesses. A new option,
safeUndefined, true by default, makes the null-only model available:JexlOptions.setSafeUndefined(boolean)/isSafeUndefined(),JexlBuilder.safeUndefined(boolean); also a flagname (
"-safeUndefined") forJexlOptions.setFlagsand thejexl.optionspragmawith or without
?.,?[or thesafeoptionx.missing?.yandx?['missing']evaluate as null asx.missing()?.yandx?.missingalready do??,?:and the ternary condition tolerate a missing method or function, as they do anundefined variable or a missing property
bean?.boom)Two released behaviors are kept with the default and the
safeoption, since changing them would affect defaultengines:
x.missingis an error wherex.missing()is null, andvar x = uwithuundefined is null (JEXL-275).Neither exists when
safeUndefinedis false.Tests:
Issues400Test.test476(expressions x the foursafeUndefined/safecombinations); no existing test changed.mvndefault goal passes🤖 Generated with Claude Code