Federico Mariani created CAMEL-26982:
----------------------------------------
Summary: camel-core: addTypeConverters(Object) does not use the
given instance for non-static @Converter methods
Key: CAMEL-26982
URL: https://issues.apache.org/jira/browse/CAMEL-26982
Project: Camel
Issue Type: Bug
Components: camel-core
Affects Versions: 4.18.5, 4.23.0, 4.22.2
Reporter: Federico Mariani
Fix For: 4.24.0
{{TypeConverterRegistry.addTypeConverters(Object)}} keeps only the class of the
given object. A non-static {{@Converter}} method then runs on a new instance
created by the injector, not on the registered object: a class without a no-arg
constructor fails at conversion time with {{NoSuchMethodException}}, and a
configured instance (e.g. a Spring bean with injected state) is silently
replaced by a fresh, unconfigured one.
{code:java}
public class PrefixConverters implements TypeConverters {
private final String prefix;
public PrefixConverters(String prefix) { this.prefix = prefix; }
@Converter
public Bar toBar(Foo foo) { return new Bar(prefix + foo.getValue()); }
}
context.getTypeConverterRegistry().addTypeConverters(new
PrefixConverters("x-"));
context.getTypeConverter().convertTo(Bar.class, new Foo("a")); //
TypeConversionException: NoSuchMethodException PrefixConverters.<init>()
{code}
Expected: the converter methods run on the given instance, as the javadoc says
("Registers all the type converters from the instance").
_Claude Code on behalf of Federico Mariani_
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)