Workaround for multiple CustomConverters with the same (could be anonymous) classes.
sparecycles opened this issue · 0 comments
The fix is to flush the converter into the lookup cache immediately after registration... as I found with the following code:
void registerConverter(MapperFactory factory, Type<A> aType, Type<B> bType, ...) {
CustomConverter converter = new CustomConverter() { ... };
ConverterFactory converterFactory = factory.getConverterFactory();
converterFactory.registerConverter(converter);
// flush converter into cache
converterFactory.getConverter(aType, bType);
converterFactory.getConverter(bType, aType); // only because my converter is bidirectional
}
Note: it will be necessary to do the cache fill externally to registerConverter(...)
since the pairs of types that getConverter()
resolves to the new converter are dependent on the result of the canConvert()
method.
Looking into this more, it seems it's a workaround for the following equals and hashCode implementation in CustomConverter which is based on the class. (remove this code please?)
orika/core/src/main/java/ma/glasnost/orika/CustomConverter.java
Lines 108 to 114 in 9f46000
...the issue is that anonymous classes all have the same "class" object but different bound variables, so the processing of the converters in the DefaultConverterFactory
through a LinkedHashSet
will end up overwriting all but the last one registered!
It looks like this was first reported in 2017. Let's get this fixed?