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 86f5a83187b0 CAMEL-24489: camel-api: fix TypeConversionException
printing 'from type: null' for anonymous/local classes
86f5a83187b0 is described below
commit 86f5a83187b0e18c057783c246fe83b7c075d16d
Author: mayurbm <[email protected]>
AuthorDate: Wed Aug 26 14:46:13 2026 +0530
CAMEL-24489: camel-api: fix TypeConversionException printing 'from type:
null' for anonymous/local classes
TypeConversionException.createMessage() used Class.getCanonicalName() to
include the source/target type in the error message. For anonymous and
local classes (e.g. lambda expressions, anonymous inner classes such as
ChannelSftp$2), getCanonicalName() returns null, so the message printed
"from type: null" even though the actual value was not null, making
diagnosis harder.
Adds a private typeName(Class<?>) helper that falls back to getName()
when getCanonicalName() is null, applied to both the source and target
type in the message.
Closes #25738
Co-authored-by: mayurmohan <[email protected]>
Co-authored-by: Claude <[email protected]>
---
.../org/apache/camel/TypeConversionException.java | 16 ++++-
.../camel/TypeConversionExceptionMessageTest.java | 69 ++++++++++++++++++++++
2 files changed, 83 insertions(+), 2 deletions(-)
diff --git
a/core/camel-api/src/main/java/org/apache/camel/TypeConversionException.java
b/core/camel-api/src/main/java/org/apache/camel/TypeConversionException.java
index dffc82d2cb9a..1ada45cecf43 100644
--- a/core/camel-api/src/main/java/org/apache/camel/TypeConversionException.java
+++ b/core/camel-api/src/main/java/org/apache/camel/TypeConversionException.java
@@ -75,9 +75,21 @@ public class TypeConversionException extends
RuntimeCamelException {
public static String createMessage(@Nullable Object value, Class<?> type,
Throwable cause) {
Objects.requireNonNull(type, "type");
Objects.requireNonNull(cause, "cause");
- return "Error during type conversion from type: " + (value != null ?
value.getClass().getCanonicalName() : null)
- + " to the required type: " + type.getCanonicalName() + " with
value " + value + " due to "
+ return "Error during type conversion from type: " + typeName(value !=
null ? value.getClass() : null)
+ + " to the required type: " + typeName(type) + " with value " +
value + " due to "
+ cause.getClass().getName() + ": " + cause.getMessage();
}
+ /**
+ * Returns the class name, preferring canonical name but falling back to
{@link Class#getName()} for anonymous or
+ * local classes whose canonical name is {@code null}.
+ */
+ private static String typeName(Class<?> clazz) {
+ if (clazz == null) {
+ return "null";
+ }
+ String name = clazz.getCanonicalName();
+ return name != null ? name : clazz.getName();
+ }
+
}
diff --git
a/core/camel-core/src/test/java/org/apache/camel/TypeConversionExceptionMessageTest.java
b/core/camel-core/src/test/java/org/apache/camel/TypeConversionExceptionMessageTest.java
new file mode 100644
index 000000000000..13d70ecf3f43
--- /dev/null
+++
b/core/camel-core/src/test/java/org/apache/camel/TypeConversionExceptionMessageTest.java
@@ -0,0 +1,69 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel;
+
+import org.junit.jupiter.api.Test;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+/**
+ * Verifies {@link TypeConversionException#createMessage} handles anonymous
and local classes whose
+ * {@link Class#getCanonicalName()} returns {@code null}.
+ *
+ * <p>
+ * Real-world trigger: SFTP stream body is an anonymous inner class ({@code
ChannelSftp$2}); its canonical name is
+ * {@code null}, causing the error message to print {@code "from type: null"}
instead of the actual class name.
+ */
+class TypeConversionExceptionMessageTest {
+
+ @Test
+ void createMessage_anonymousClassUsesGetName() {
+ // anonymous Runnable — getCanonicalName() returns null
+ Object anon = new Runnable() {
+ @Override
+ public void run() {
+ }
+ };
+ assertThat(anon.getClass().getCanonicalName()).isNull();
+
+ String msg = TypeConversionException.createMessage(anon, String.class,
new RuntimeException("boom"));
+
+ assertThat(msg)
+ .doesNotContain("from type: null")
+ .contains("from type: ")
+ .contains("$"); // binary name contains $ for anonymous classes
+ }
+
+ @Test
+ void createMessage_namedClassUsesCanonicalName() {
+ String msg = TypeConversionException.createMessage("hello",
Integer.class, new RuntimeException("oops"));
+
+ assertThat(msg)
+ .contains("from type: java.lang.String")
+ .contains("to the required type: java.lang.Integer")
+ .contains("due to java.lang.RuntimeException: oops");
+ }
+
+ @Test
+ void createMessage_nullValueShowsNull() {
+ String msg = TypeConversionException.createMessage(null, String.class,
new RuntimeException("npe"));
+
+ assertThat(msg)
+ .contains("from type: null")
+ .contains("to the required type: java.lang.String");
+ }
+}