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 7cba02fe656c CAMEL-27423: camel-support - the body origin of a split
item names the split, not the consumer (#30643)
7cba02fe656c is described below
commit 7cba02fe656c8a3ce951f7101746130efebecd2a
Author: Claus Ibsen <[email protected]>
AuthorDate: Mon Nov 23 09:30:56 2026 +0100
CAMEL-27423: camel-support - the body origin of a split item names the
split, not the consumer (#30643)
BodyFacts said "unchanged since from: <consumer>" for the body of a split
item. The Splitter gives each item a message history of its own, which
starts inside the split, so no step in it saw another body type. When the
first step of the history is inside a split, the split is now named as the
step that set the body: "set by split at orders.yaml:9".
Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
.../org/apache/camel/support/BodyFactsTest.java | 31 ++++++++++++++++++++++
.../java/org/apache/camel/support/BodyFacts.java | 22 +++++++++++++--
2 files changed, 51 insertions(+), 2 deletions(-)
diff --git
a/core/camel-core/src/test/java/org/apache/camel/support/BodyFactsTest.java
b/core/camel-core/src/test/java/org/apache/camel/support/BodyFactsTest.java
index 426d5260c8ac..3478d60f285d 100644
--- a/core/camel-core/src/test/java/org/apache/camel/support/BodyFactsTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/support/BodyFactsTest.java
@@ -72,6 +72,10 @@ public class BodyFactsTest extends ContextTestSupport {
.process(e -> seen.add(BodyFacts.hint(e, Need.DATA)));
from("direct:marshalled").marshal(new TextDataFormat())
.process(e -> seen.add(BodyFacts.describe(e) + ": " +
BodyFacts.hint(e, Need.DATA)));
+ from("direct:split").split(body()).process(e ->
seen.add(BodyFacts.describe(e)));
+ from("direct:splitToItem").split(body()).to("direct:item");
+ from("direct:item").process(e ->
seen.add(BodyFacts.describe(e)));
+
from("direct:afterSplit").split(body()).log("item").end().process(e ->
seen.add(BodyFacts.describe(e)));
}
};
}
@@ -99,6 +103,33 @@ public class BodyFactsTest extends ContextTestSupport {
assertTrue(seen.get(0).startsWith("the body is a Map (LinkedHashMap),
set by setBody"), seen.get(0));
}
+ @Test
+ public void testTheSplitSetTheBodyOfAnItem() {
+ // r36 (sftp s2): the rows of a CSV file were split, and the store of
a row in onException said the Map was
+ // unchanged since from: sftp, as the split gives each item a message
history of its own
+ template.sendBody("direct:split", List.of(new
LinkedHashMap<>(Map.of("sku", "A1")), new LinkedHashMap<>()));
+ assertEquals(2, seen.size());
+ for (String s : seen) {
+ assertTrue(s.startsWith("the body is a Map (LinkedHashMap), set by
split"), s);
+ }
+ }
+
+ @Test
+ public void testTheSplitSetTheBodyOfAnItemSentToAnotherRoute() {
+ template.sendBody("direct:splitToItem", List.of("A1", "B2"));
+ assertEquals(2, seen.size());
+ for (String s : seen) {
+ assertTrue(s.startsWith("the body is text (a String), set by
split"), s);
+ }
+ }
+
+ @Test
+ public void testTheConsumerGaveTheBodyThatWasSplit() {
+ // after the split the message goes on with its own body, which the
split did not set
+ template.sendBody("direct:afterSplit", new ArrayList<>(List.of("A1",
"B2")));
+ assertEquals(List.of("the body is a List (ArrayList), unchanged since
from: direct://afterSplit"), seen);
+ }
+
@Test
public void testAFieldReadAfterASetBodyThatEmptiedIt() {
// r23 (http-client s1): the route set the body to ${null} and then
read ${body[sku]}
diff --git
a/core/camel-support/src/main/java/org/apache/camel/support/BodyFacts.java
b/core/camel-support/src/main/java/org/apache/camel/support/BodyFacts.java
index bac9bd7536c8..e94d647d6c9c 100644
--- a/core/camel-support/src/main/java/org/apache/camel/support/BodyFacts.java
+++ b/core/camel-support/src/main/java/org/apache/camel/support/BodyFacts.java
@@ -416,8 +416,9 @@ public final class BodyFacts {
/**
* The step that set the body, from the message history: the last node
reached with a body of another type, which
- * ran on it and left this one. When every node was reached with this
type, the consumer gave it. Empty when the
- * message history is off.
+ * ran on it and left this one. When every node was reached with this
type, the consumer gave it; or, for an item of
+ * a split, the split, as the split gives each item a message history of
its own, which starts inside the split.
+ * Empty when the message history is off.
*/
private static String setBy(Exchange exchange, Object body) {
List<MessageHistory> history =
exchange.getProperty(ExchangePropertyKey.MESSAGE_HISTORY, List.class);
@@ -439,9 +440,26 @@ public final class BodyFacts {
if (!recorded || exchange.getFromEndpoint() == null) {
return "";
}
+ NamedNode split = splitAround(history.get(0).getNode());
+ if (split != null) {
+ return ", set by " + step(split);
+ }
return ", unchanged since from: " +
URISupport.sanitizeUri(exchange.getFromEndpoint().getEndpointUri());
}
+ /**
+ * The nearest split among the parents of a node, or null. The node itself
does not count: a message that reached a
+ * split had its body before the split ran on it.
+ */
+ private static NamedNode splitAround(NamedNode node) {
+ for (NamedNode parent = node != null ? node.getParent() : null; parent
!= null; parent = parent.getParent()) {
+ if ("split".equals(parent.getShortName())) {
+ return parent;
+ }
+ }
+ return null;
+ }
+
/**
* When other messages have reached the failing step with a body of
another kind, as the {@link BodyTypeRegistry}
* knows while the message history is on: the body depends on the path a
message takes, such as a branch that does