You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by he...@apache.org on 2022/07/23 15:15:48 UTC

[commons-jexl] branch master updated: JEXL-374: better control over property existence;

This is an automated email from the ASF dual-hosted git repository.

henrib pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-jexl.git


The following commit(s) were added to refs/heads/master by this push:
     new 4d9a57cd JEXL-374: better control over property existence;
     new 22bd084d Merge remote-tracking branch 'origin/master'
4d9a57cd is described below

commit 4d9a57cdad3fb5f179328a898735cdd1a47091d6
Author: henrib <he...@apache.org>
AuthorDate: Sat Jul 23 17:15:37 2022 +0200

    JEXL-374: better control over property existence;
---
 .../apache/commons/jexl3/internal/Interpreter.java |  9 +----
 .../org/apache/commons/jexl3/Issues300Test.java    | 46 ++++++++++++++++++++--
 .../org/apache/commons/jexl3/JexlTestCase.java     |  4 +-
 3 files changed, 46 insertions(+), 13 deletions(-)

diff --git a/src/main/java/org/apache/commons/jexl3/internal/Interpreter.java b/src/main/java/org/apache/commons/jexl3/internal/Interpreter.java
index 2ce7923f..4ba5113b 100644
--- a/src/main/java/org/apache/commons/jexl3/internal/Interpreter.java
+++ b/src/main/java/org/apache/commons/jexl3/internal/Interpreter.java
@@ -1177,7 +1177,7 @@ public class Interpreter extends InterpreterBase {
         JexlNode objectNode = null;
         JexlNode ptyNode = null;
         StringBuilder ant = null;
-        boolean antish = !(parent instanceof ASTReference);
+        boolean antish = !(parent instanceof ASTReference) && options.isAntish();
         int v = 1;
         main:
         for (int c = 0; c < numChildren; c++) {
@@ -1229,11 +1229,6 @@ public class Interpreter extends InterpreterBase {
                     }
                     final ASTIdentifier afirst = (ASTIdentifier) first;
                     ant = new StringBuilder(afirst.getName());
-                    // skip the else...*
-                    // *... and continue
-                    if (!options.isAntish()) {
-                        antish = false;
-                    }
                     continue;
                     // skip the first node case since it was trialed in jjtAccept above and returned null
                 }
@@ -1256,7 +1251,7 @@ public class Interpreter extends InterpreterBase {
                 object = context.get(ant.toString());
             } else if (c != numChildren - 1) {
                 // only the last one may be null
-                ptyNode = objectNode;
+                ptyNode = c == 0 && numChildren > 1 ? node.jjtGetChild(1) : objectNode;
                 break; //
             }
         }
diff --git a/src/test/java/org/apache/commons/jexl3/Issues300Test.java b/src/test/java/org/apache/commons/jexl3/Issues300Test.java
index 4c1eb00f..f3d2d1ce 100644
--- a/src/test/java/org/apache/commons/jexl3/Issues300Test.java
+++ b/src/test/java/org/apache/commons/jexl3/Issues300Test.java
@@ -16,6 +16,11 @@
  */
 package org.apache.commons.jexl3;
 
+import org.apache.commons.jexl3.internal.Engine32;
+import org.apache.commons.jexl3.internal.OptionsContext;
+import org.junit.Assert;
+import org.junit.Test;
+
 import java.io.StringReader;
 import java.io.StringWriter;
 import java.util.ArrayList;
@@ -26,11 +31,7 @@ import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
 
-import org.apache.commons.jexl3.internal.Engine32;
-import org.apache.commons.jexl3.internal.OptionsContext;
-import org.junit.Assert;
 import static org.junit.Assert.assertEquals;
-import org.junit.Test;
 
 /**
  * Test cases for reported issue between JEXL-300 and JEXL-399.
@@ -814,4 +815,41 @@ public class Issues300Test {
             Assert.assertEquals("phone", xvar.getVariable());
         }
     }
+
+    public static class TestObject374 {
+        private String name;
+        private TestObject374 nested = null;
+        public String getName() {
+            return name;
+        }
+        public void setName(String pName) {
+            this.name = pName;
+        }
+        public TestObject374 getNested() {
+            return nested;
+        }
+        public void setNested(TestObject374 pNested) {
+            nested = pNested;
+        }
+    }
+
+    @Test
+    public void test374() {
+        JexlEngine engine = new JexlBuilder().cache(512).strict(true).silent(false).antish(false).safe(false).create();
+        // Create expression to evaluate 'name'
+        JexlExpression expr = engine.createExpression("nested.name");
+        // Create an object with getter for name
+        TestObject374 myObject = new TestObject374();
+        myObject.setName("John");
+        JexlContext context = new ObjectContext<TestObject374>(engine, myObject);
+        // Expect an exception because nested is null, so we are doing null.name
+        try {
+            Object result = expr.evaluate(context);
+            Assert.fail("An exception expected, but got: " + result);
+        } catch (JexlException ex) {
+            // Expected
+            //ex.printStackTrace();
+        }
+    }
+
 }
diff --git a/src/test/java/org/apache/commons/jexl3/JexlTestCase.java b/src/test/java/org/apache/commons/jexl3/JexlTestCase.java
index 68dafbfc..efa05735 100644
--- a/src/test/java/org/apache/commons/jexl3/JexlTestCase.java
+++ b/src/test/java/org/apache/commons/jexl3/JexlTestCase.java
@@ -34,7 +34,7 @@ import org.junit.Assert;
  * Eases the implementation of main methods to debug.
  */
 public class JexlTestCase {
-    // The default options: all tests where engine must lexicality is
+    // The default options: all tests where engine lexicality is
     // important can be identified by the builder  calling lexical(...).
     static {
         JexlOptions.setDefaultFlags("-safe", "+lexical");
@@ -48,7 +48,7 @@ public class JexlTestCase {
     protected final JexlEngine JEXL;
 
     public JexlTestCase(final String name) {
-        this(name, new JexlBuilder().cache(128).create());
+        this(name, new JexlBuilder().permissions(null).cache(128).create());
     }
 
     protected JexlTestCase(final String name, final JexlEngine jexl) {