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

jamesnetherton 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 b410a1228c96 CAMEL-27027: camel-core - message history fails when a 
body's class has no canonical name
b410a1228c96 is described below

commit b410a1228c966d1d73aea2a7e51407f52dfa8b4a
Author: James Netherton <[email protected]>
AuthorDate: Mon Nov 16 14:46:16 2026 +0000

    CAMEL-27027: camel-core - message history fails when a body's class has no 
canonical name
    
    * CAMEL-27027: camel-core - message history fails when a body's class has 
no canonical name
    
    An anonymous, local or hidden class has no canonical name, so the body
    type registry stored null for it and the next exchange at that step
    failed with a NullPointerException. Fall back to the class name.
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
    
    * CAMEL-27027: rename the helper so it does not read as BodyFacts.typeName
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
    
    * CAMEL-27027: the body facts and the message history dump name the body as 
the history does
    
    BodyFacts compared the history's class name with the canonical name, which
    is null for an anonymous class, and said a step set a body the consumer 
gave.
    The registry also ignores a null type from another MessageHistory.
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
    
    ---------
    
    Co-authored-by: Claude Opus 5.5 <[email protected]>
---
 .../main/java/org/apache/camel/MessageHistory.java |  5 +++--
 .../camel/impl/engine/DefaultBodyTypeRegistry.java |  3 +++
 .../camel/processor/BodyTypeRegistryTest.java      | 24 ++++++++++++++++++++++
 .../org/apache/camel/support/BodyFactsTest.java    | 10 +++++++++
 .../java/org/apache/camel/support/BodyFacts.java   | 20 ++++++++++++++----
 .../camel/support/DefaultMessageHistory.java       |  3 +--
 .../org/apache/camel/support/MessageHelper.java    |  2 +-
 7 files changed, 58 insertions(+), 9 deletions(-)

diff --git a/core/camel-api/src/main/java/org/apache/camel/MessageHistory.java 
b/core/camel-api/src/main/java/org/apache/camel/MessageHistory.java
index 8cfea6b46267..d002c232affb 100644
--- a/core/camel-api/src/main/java/org/apache/camel/MessageHistory.java
+++ b/core/camel-api/src/main/java/org/apache/camel/MessageHistory.java
@@ -79,8 +79,9 @@ public interface MessageHistory {
 
     /**
      * The class of the message body when this node was reached (the type the 
body arrived with, before the node
-     * processed it): the canonical class name, the string {@code "null"} when 
the body was null, or null when not
-     * captured. Cheap to capture: the class, no conversion, no copy.
+     * processed it): the canonical class name, or the class name when the 
class has none (an anonymous, local or hidden
+     * class), the string {@code "null"} when the body was null, or null when 
not captured. Cheap to capture: the class,
+     * no conversion, no copy.
      *
      * @since 4.23
      */
diff --git 
a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultBodyTypeRegistry.java
 
b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultBodyTypeRegistry.java
index b72af0d3287d..25f26840c822 100644
--- 
a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultBodyTypeRegistry.java
+++ 
b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultBodyTypeRegistry.java
@@ -42,6 +42,9 @@ public class DefaultBodyTypeRegistry implements 
BodyTypeRegistry {
 
     @Override
     public void addBodyType(String routeId, String nodeId, String bodyType, 
Kind kind) {
+        if (bodyType == null) {
+            return;
+        }
         // called for every node an exchange reaches while the message history 
is on: plain lookups first, and no
         // allocation when the node has seen the type before
         Map<String, NodeTypes> nodes = routes.get(routeId);
diff --git 
a/core/camel-core/src/test/java/org/apache/camel/processor/BodyTypeRegistryTest.java
 
b/core/camel-core/src/test/java/org/apache/camel/processor/BodyTypeRegistryTest.java
index 659f1034fbbc..22c9e8f0f16a 100644
--- 
a/core/camel-core/src/test/java/org/apache/camel/processor/BodyTypeRegistryTest.java
+++ 
b/core/camel-core/src/test/java/org/apache/camel/processor/BodyTypeRegistryTest.java
@@ -79,6 +79,30 @@ public class BodyTypeRegistryTest extends ContextTestSupport 
{
         assertEquals(List.of(Kind.NULL), registry.getBodyKinds("orders", 
"maybeMap"));
     }
 
+    @Test
+    public void testClassWithoutCanonicalName() throws Exception {
+        getMockEndpoint("mock:result").expectedMessageCount(2);
+
+        Object anonymous = new Object() {
+        };
+        template.sendBody("direct:start", anonymous);
+        template.sendBody("direct:start", "hello");
+        assertMockEndpointsSatisfied();
+
+        // an anonymous class has no canonical name, so its class name is kept
+        assertEquals(List.of(anonymous.getClass().getName(), 
"java.lang.String"),
+                registry.getBodyTypes("orders", "maybeMap"));
+    }
+
+    @Test
+    public void testANullTypeIsIgnored() {
+        // a MessageHistory that did not capture the type
+        registry.addBodyType("custom", "step", null, null);
+        registry.addBodyType("custom", "step", "java.lang.String", 
Kind.PAYLOAD);
+
+        assertEquals(List.of("java.lang.String"), 
registry.getBodyTypes("custom", "step"));
+    }
+
     @Test
     public void testNotReachedIsEmpty() {
         assertTrue(registry.getBodyTypes("orders", "maybeMap").isEmpty());
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 bd84e41c66d3..1709410e1ee5 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
@@ -82,6 +82,16 @@ public class BodyFactsTest extends ContextTestSupport {
         assertEquals(List.of("the body is text (a String), unchanged since 
from: direct://unchanged"), seen);
     }
 
+    @Test
+    public void testTheConsumerGaveABodyWithoutCanonicalName() {
+        Object body = new Object() {
+        };
+        template.sendBody("direct:unchanged", body);
+        assertEquals(List.of("the body is an object of class " + 
body.getClass().getName()
+                             + ", unchanged since from: direct://unchanged"),
+                seen);
+    }
+
     @Test
     public void testTheStepThatSetTheBody() {
         template.sendBody("direct:setBy", "hello");
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 d919d4283295..c978930ca3e6 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
@@ -41,7 +41,6 @@ import org.apache.camel.StreamCache;
 import org.apache.camel.WrappedFile;
 import org.apache.camel.spi.BodyTypeRegistry;
 import org.apache.camel.util.MimeTypeHelper;
-import org.apache.camel.util.ObjectHelper;
 import org.apache.camel.util.StringHelper;
 import org.apache.camel.util.URISupport;
 
@@ -296,7 +295,7 @@ public final class BodyFacts {
         if (history == null || history.isEmpty()) {
             return null;
         }
-        String now = ObjectHelper.classCanonicalName(body);
+        String now = className(body);
         for (int i = history.size() - 1; i >= 0; i--) {
             MessageHistory step = history.get(i);
             String type = step.getBodyType();
@@ -336,7 +335,7 @@ public final class BodyFacts {
         if (history == null || history.isEmpty()) {
             return null;
         }
-        String now = body != null ? ObjectHelper.classCanonicalName(body) : 
"null";
+        String now = body != null ? className(body) : "null";
         for (int i = history.size() - 1; i >= 0; i--) {
             String type = history.get(i).getBodyType();
             if (type == null || type.equals(now)) {
@@ -400,7 +399,7 @@ public final class BodyFacts {
         if (history == null || history.isEmpty()) {
             return "";
         }
-        String now = body != null ? ObjectHelper.classCanonicalName(body) : 
"null";
+        String now = body != null ? className(body) : "null";
         boolean recorded = false;
         for (int i = history.size() - 1; i >= 0; i--) {
             String type = history.get(i).getBodyType();
@@ -539,6 +538,19 @@ public final class BodyFacts {
         return body == null || body instanceof String s && s.isBlank() || body 
instanceof byte[] b && b.length == 0;
     }
 
+    /**
+     * The class name of the body as the message history keeps it: the 
canonical name, or the class name when the class
+     * has none (an anonymous, local or hidden class, such as a lambda). Null 
for a null body.
+     */
+    static String className(Object body) {
+        if (body == null) {
+            return null;
+        }
+        Class<?> type = body.getClass();
+        String name = type.getCanonicalName();
+        return name != null ? name : type.getName();
+    }
+
     /**
      * The class of a body for a message: its simple name, or for a class the 
JDK keeps to itself (the Map of Map.of,
      * the List of Arrays.asList) the interface it implements, as its own name 
says nothing to the reader.
diff --git 
a/core/camel-support/src/main/java/org/apache/camel/support/DefaultMessageHistory.java
 
b/core/camel-support/src/main/java/org/apache/camel/support/DefaultMessageHistory.java
index f92a2a727f01..e8161eebdd8e 100644
--- 
a/core/camel-support/src/main/java/org/apache/camel/support/DefaultMessageHistory.java
+++ 
b/core/camel-support/src/main/java/org/apache/camel/support/DefaultMessageHistory.java
@@ -26,7 +26,6 @@ import org.apache.camel.MessageHistory;
 import org.apache.camel.NamedNode;
 import org.apache.camel.spi.BodyTypeRegistry;
 import org.apache.camel.spi.MessageSizeStrategy;
-import org.apache.camel.util.ObjectHelper;
 
 /**
  * Default {@link org.apache.camel.MessageHistory}.
@@ -113,7 +112,7 @@ public class DefaultMessageHistory implements 
MessageHistory {
     public void captureBody(Exchange exchange, BodyTypeRegistry registry) {
         Message current = exchange.getMessage();
         Object body = current.getBody();
-        bodyType = body != null ? ObjectHelper.classCanonicalName(body) : 
"null";
+        bodyType = body != null ? BodyFacts.className(body) : "null";
         bodyDataType = body instanceof Map<?, ?> || body instanceof 
Collection<?> ? BodyFacts.typeName(body) : null;
         if (registry != null && routeId != null && nodeId != null) {
             registry.addBodyType(routeId, nodeId, bodyType, 
BodyFacts.kindOf(body));
diff --git 
a/core/camel-support/src/main/java/org/apache/camel/support/MessageHelper.java 
b/core/camel-support/src/main/java/org/apache/camel/support/MessageHelper.java
index 121fdb74191f..8a5780592f8c 100644
--- 
a/core/camel-support/src/main/java/org/apache/camel/support/MessageHelper.java
+++ 
b/core/camel-support/src/main/java/org/apache/camel/support/MessageHelper.java
@@ -777,7 +777,7 @@ public final class MessageHelper {
 
         // the body as it is now, for the route's own row (the history rows 
carry the body as each node was reached)
         Object body = exchange.getMessage().getBody();
-        String bodyType = body != null ? ObjectHelper.classCanonicalName(body) 
: "null";
+        String bodyType = body != null ? BodyFacts.className(body) : "null";
         String bodySize = "";
         if (body != null && exchange.getContext().getMessageSizeStrategy() != 
null
                 && exchange.getContext().getMessageSizeStrategy().isEnabled()) 
{

Reply via email to