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

davsclaus pushed a commit to branch fix/CAMEL-24101
in repository https://gitbox.apache.org/repos/asf/camel.git

commit f6664a83dab3c947514ee40f4645c1f3e748d9c4
Author: Claus Ibsen <[email protected]>
AuthorDate: Thu Jul 16 09:42:11 2026 +0200

    CAMEL-24101: camel-bean - Fix OGNL list index off-by-one at boundary
    
    Co-Authored-By: Claude Opus 4.6 <[email protected]>
    Signed-off-by: Claus Ibsen <[email protected]>
---
 .../apache/camel/language/bean/BeanExpression.java |  2 +-
 .../apache/camel/language/simple/SimpleTest.java   | 29 ++++++++++++++++++++++
 2 files changed, 30 insertions(+), 1 deletion(-)

diff --git 
a/components/camel-bean/src/main/java/org/apache/camel/language/bean/BeanExpression.java
 
b/components/camel-bean/src/main/java/org/apache/camel/language/bean/BeanExpression.java
index d1227c971332..c30cb51cc795 100644
--- 
a/components/camel-bean/src/main/java/org/apache/camel/language/bean/BeanExpression.java
+++ 
b/components/camel-bean/src/main/java/org/apache/camel/language/bean/BeanExpression.java
@@ -552,7 +552,7 @@ public class BeanExpression implements Expression, 
Predicate {
                         }
                     }
                 }
-                if (num != null && num >= 0 && !list.isEmpty() && list.size() 
> num - 1) {
+                if (num != null && num >= 0 && !list.isEmpty() && list.size() 
> num) {
                     return list.get(num);
                 }
                 if (!nullSafe) {
diff --git 
a/core/camel-core/src/test/java/org/apache/camel/language/simple/SimpleTest.java
 
b/core/camel-core/src/test/java/org/apache/camel/language/simple/SimpleTest.java
index 980f182d453d..0b4956108f7f 100644
--- 
a/core/camel-core/src/test/java/org/apache/camel/language/simple/SimpleTest.java
+++ 
b/core/camel-core/src/test/java/org/apache/camel/language/simple/SimpleTest.java
@@ -1371,6 +1371,35 @@ public class SimpleTest extends LanguageTestSupport {
         assertExpression("${in.body?.lines[3].id}", null);
     }
 
+    @Test
+    public void testBodyOGNLOrderListOutOfBoundsAtBoundary() {
+        List<OrderLine> lines = new ArrayList<>();
+        lines.add(new OrderLine(123, "Camel in Action"));
+        lines.add(new OrderLine(456, "ActiveMQ in Action"));
+        Order order = new Order(lines);
+
+        exchange.getIn().setBody(order);
+
+        RuntimeBeanExpressionException e = 
assertThrows(RuntimeBeanExpressionException.class,
+                () -> assertExpression("${in.body.getLines[2].getId}", 123),
+                "Should have thrown an exception");
+
+        IndexOutOfBoundsException cause = 
assertIsInstanceOf(IndexOutOfBoundsException.class, e.getCause());
+        assertTrue(cause.getMessage().startsWith("Index: 2, Size: 2 out of 
bounds with List from bean"));
+    }
+
+    @Test
+    public void testBodyOGNLOrderListOutOfBoundsAtBoundaryWithNullSafe() {
+        List<OrderLine> lines = new ArrayList<>();
+        lines.add(new OrderLine(123, "Camel in Action"));
+        lines.add(new OrderLine(456, "ActiveMQ in Action"));
+        Order order = new Order(lines);
+
+        exchange.getIn().setBody(order);
+
+        assertExpression("${in.body?.getLines[2].getId}", null);
+    }
+
     @Test
     public void testBodyOGNLOrderListNoMethodNameWithNullSafe() {
         List<OrderLine> lines = new ArrayList<>();

Reply via email to