This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch fix/CAMEL-24102 in repository https://gitbox.apache.org/repos/asf/camel.git
commit 24a8ad50a4204b9011705ec00f9a85ffe22f759e Author: Claus Ibsen <[email protected]> AuthorDate: Thu Jul 16 09:46:10 2026 +0200 CAMEL-24102: camel-bean - AmbiguousMethodCallException lists actual candidates Co-Authored-By: Claude Opus 4.6 <[email protected]> Signed-off-by: Claus Ibsen <[email protected]> --- .../org/apache/camel/component/bean/BeanInfo.java | 8 +-- .../bean/BeanAmbiguousBodyConversionTest.java | 70 ++++++++++++++++++++++ 2 files changed, 74 insertions(+), 4 deletions(-) diff --git a/components/camel-bean/src/main/java/org/apache/camel/component/bean/BeanInfo.java b/components/camel-bean/src/main/java/org/apache/camel/component/bean/BeanInfo.java index 904e8cfdfdbf..9619d58b9ba1 100644 --- a/components/camel-bean/src/main/java/org/apache/camel/component/bean/BeanInfo.java +++ b/components/camel-bean/src/main/java/org/apache/camel/component/bean/BeanInfo.java @@ -885,7 +885,7 @@ public class BeanInfo { // let's try converting Object newBody = null; MethodInfo matched = null; - int matchCounter = 0; + List<MethodInfo> candidates = new ArrayList<>(); for (MethodInfo methodInfo : operationList) { if (methodInfo.getBodyParameterType() != null) { if (methodInfo.getBodyParameterType().isInstance(body)) { @@ -900,14 +900,14 @@ public class BeanInfo { LOG.trace("Converted body from: {} to: {}", body.getClass().getCanonicalName(), methodInfo.getBodyParameterType().getCanonicalName()); } - matchCounter++; + candidates.add(methodInfo); newBody = value; matched = methodInfo; } } } - if (matchCounter > 1) { - throw new AmbiguousMethodCallException(exchange, Arrays.asList(matched, matched)); + if (candidates.size() > 1) { + throw new AmbiguousMethodCallException(exchange, candidates); } if (matched != null) { LOG.trace("Setting converted body: {}", body); diff --git a/core/camel-core/src/test/java/org/apache/camel/component/bean/BeanAmbiguousBodyConversionTest.java b/core/camel-core/src/test/java/org/apache/camel/component/bean/BeanAmbiguousBodyConversionTest.java new file mode 100644 index 000000000000..019938bf3bc0 --- /dev/null +++ b/core/camel-core/src/test/java/org/apache/camel/component/bean/BeanAmbiguousBodyConversionTest.java @@ -0,0 +1,70 @@ +/* + * 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.component.bean; + +import java.util.ArrayList; +import java.util.Collection; + +import org.apache.camel.CamelExecutionException; +import org.apache.camel.ContextTestSupport; +import org.apache.camel.builder.RouteBuilder; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertInstanceOf; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class BeanAmbiguousBodyConversionTest extends ContextTestSupport { + + @Test + public void testAmbiguousExceptionListsDistinctCandidates() { + CamelExecutionException e = assertThrows(CamelExecutionException.class, + () -> template.sendBody("direct:start", "123")); + + AmbiguousMethodCallException cause = assertInstanceOf(AmbiguousMethodCallException.class, e.getCause()); + Collection<MethodInfo> methods = cause.getMethods(); + + assertEquals(2, methods.size(), "Should list exactly two distinct candidates"); + ArrayList<MethodInfo> list = new ArrayList<>(methods); + assertTrue(list.get(0) != list.get(1), + "The two candidates should be different method instances"); + } + + @Override + protected RouteBuilder createRouteBuilder() { + return new RouteBuilder() { + @Override + public void configure() { + from("direct:start") + .bean(MyAmbiguousBean.class) + .to("mock:result"); + } + }; + } + + @SuppressWarnings("unused") + public static class MyAmbiguousBean { + public String foo(Integer num) { + return "foo:" + num; + } + + public String bar(Double num) { + return "bar:" + num; + } + } +}
