This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/main by this push:
new 8348e4022f88 CAMEL-24101: camel-bean - Fix OGNL list index off-by-one
at boundary (#24762)
8348e4022f88 is described below
commit 8348e4022f88093310cd61708b15e910795a06f0
Author: Claus Ibsen <[email protected]>
AuthorDate: Thu Jul 16 15:47:55 2026 +0200
CAMEL-24101: camel-bean - Fix OGNL list index off-by-one at boundary
(#24762)
Co-Authored-By: Claude Opus 4.6 <[email protected]>
---
.../apache/camel/language/bean/BeanExpression.java | 2 +-
.../apache/camel/language/simple/SimpleTest.java | 39 ++++++++++++++++++----
2 files changed, 34 insertions(+), 7 deletions(-)
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..2bb10c6cc3ac 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
@@ -71,8 +71,6 @@ import static org.junit.jupiter.api.Assertions.fail;
public class SimpleTest extends LanguageTestSupport {
- private static final String INDEX_OUT_OF_BOUNDS_ERROR_MSG = "Index 2 out
of bounds for length 2";
-
private static final String BOOKS
= """
{
@@ -443,7 +441,7 @@ public class SimpleTest extends LanguageTestSupport {
"Should have thrown an exception");
IndexOutOfBoundsException cause =
assertIsInstanceOf(IndexOutOfBoundsException.class, e.getCause());
- assertEquals(INDEX_OUT_OF_BOUNDS_ERROR_MSG, cause.getMessage());
+ assertTrue(cause.getMessage().startsWith("Index: 2, Size: 2 out of
bounds with List from bean"));
assertExpression("${exchangeProperty.unknown[cool]}", null);
}
@@ -463,7 +461,7 @@ public class SimpleTest extends LanguageTestSupport {
"Should have thrown an exception");
IndexOutOfBoundsException cause =
assertIsInstanceOf(IndexOutOfBoundsException.class, e.getCause());
- assertEquals(INDEX_OUT_OF_BOUNDS_ERROR_MSG, cause.getMessage());
+ assertTrue(cause.getMessage().startsWith("Index: 2, Size: 2 out of
bounds with List from bean"));
assertExpression("${exchangeProperty.unknown[cool]}", null);
}
@@ -961,7 +959,7 @@ public class SimpleTest extends LanguageTestSupport {
"Should have thrown an exception");
IndexOutOfBoundsException cause =
assertIsInstanceOf(IndexOutOfBoundsException.class, e.getCause());
- assertEquals(INDEX_OUT_OF_BOUNDS_ERROR_MSG, cause.getMessage());
+ assertTrue(cause.getMessage().startsWith("Index: 2, Size: 2 out of
bounds with List from bean"));
assertExpression("${header.unknown[cool]}", null);
}
@@ -980,7 +978,7 @@ public class SimpleTest extends LanguageTestSupport {
"Should have thrown an exception");
IndexOutOfBoundsException cause =
assertIsInstanceOf(IndexOutOfBoundsException.class, e.getCause());
- assertEquals(INDEX_OUT_OF_BOUNDS_ERROR_MSG, cause.getMessage());
+ assertTrue(cause.getMessage().startsWith("Index: 2, Size: 2 out of
bounds with List from bean"));
assertExpression("${header.unknown[cool]}", null);
}
@@ -1371,6 +1369,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<>();