Skip to content

JEXL-476: Add the safeUndefined option to restrict safe navigation to null - #425

Merged
henrib merged 3 commits into
masterfrom
JEXL-476
Nov 19, 2026
Merged

henrib merged 3 commits into
masterfrom
JEXL-476

Conversation

@henrib

@henrib henrib commented Nov 11, 2026

Copy link
Copy Markdown
Contributor

JEXL-476, on top of #424 (JEXL-474).

Safe navigation (?., ?[, the safe option) 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:

JexlEngine jexl = new JexlBuilder().strict(true).safeUndefined(false).create();
  • JexlOptions.setSafeUndefined(boolean) / isSafeUndefined(), JexlBuilder.safeUndefined(boolean); also a flag
    name ("-safeUndefined") for JexlOptions.setFlags and the jexl.options pragma
  • false: safe navigation only applies to null; an undefined variable, property, method or function is an error,
    with or without ?., ?[ or the safe option
  • true (default): behavior as released, with the safe access operators made consistent:
    x.missing?.y and x?['missing'] evaluate as null as x.missing()?.y and x?.missing already do
  • in both cases, ??, ?: and the ternary condition tolerate a missing method or function, as they do an
    undefined variable or a missing property
  • a safe access no longer hides an exception thrown by a property getter (bean?.boom)

Two released behaviors are kept with the default and the safe option, since changing them would affect default
engines: x.missing is an error where x.missing() is null, and var x = u with u undefined is null (JEXL-275).
Neither exists when safeUndefined is false.

Tests: Issues400Test.test476 (expressions x the four safeUndefined/safe combinations); no existing test changed.

  • mvn default goal passes
  • AI disclosure: written with Claude Code (Claude Fable 5), reviewed by the committer.

🤖 Generated with Claude Code

… 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>
Copilot AI balanced review requested due to automatic review settings November 11, 2026 17:54

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
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 safeUndefined flag in JexlOptions and exposes it via JexlBuilder.
  • 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()

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +554 to +555
* <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>

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +800 to +803
final int next = indexOf(indexed, node) + 1;
if (indexed.isSafeChild(next - 1)) {
return true;
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +1455 to +1458
if (expected == err) {
assertThrows(JexlException.class, () -> script.execute(ctxt), msg);
} else {
assertEquals(expected, script.execute(ctxt), msg);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>
@henrib
henrib requested a balanced review from Copilot November 12, 2026 09:51

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment on lines 1724 to 1726
return isSafe() || ((ASTIdentifierAccess) methodNode).isSafe()
? null
: unsolvableMethod(methodNode, "<null>.<?>(...)");

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/site/xdoc/reference/syntax.xml Resolved

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment on lines 1721 to 1726
object = data;
if (object == null) {
// no object, we fail
return node.isSafeLhs(isSafe())
return isSafe() || ((ASTIdentifierAccess) methodNode).isSafe()
? null
: unsolvableMethod(methodNode, "<null>.<?>(...)");

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>(...)");

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +294 to +296
<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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>
@henrib
henrib merged commit 06dde65 into master Nov 19, 2026
16 checks passed
@henrib
henrib deleted the JEXL-476 branch November 19, 2026 07:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants