You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Custom Converter annotated with @ConfigurationPropertiesBinding does not get selected if targetType has a static factory method different return type
#28592
Closed
DieBauer opened this issue
Nov 10, 2021
· 2 comments
I'm upgrading my application from 2.4.10 to 2.5.6 and one of my custom Converters does not get picked up anymore since a specific static factory method (from) is present on the targetType.
After debugging this issue, it looks like in 2.5, the ObjectToObjectConverter wins, and gets called before my custom Converter is picked up.
Even though the return type of the factory method does not fit the targetType!
In my case the factory method returns an Optional.
The converter gets selected by the Binder, and my property is bound to the properties class.
In spring boot 2.5, the exact same code base fails with ConverterNotFoundException
The resulting error looks like this:
Failed to bind properties under 'my-data' to app.Data:
Property: my-data
Value: hello
Origin: class path resource [application.properties] - 1:8
Reason: org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [java.util.Optional<?>] to type [app.Data]
In my opinion this is a bug in the priority of the generic ObjectToObjectConverter (not taking into account the targetType) and user-defined custom converters!
Thanks for the sample. I've reproduced the behaviour that you've described. It appears to be a regression introduced in 2.5.0-RC1 as your sample works fine with 2.5.0-M3. I've yet to verify this, but I suspect that 51c3e18 may be the cause.
philwebb
changed the title
Custom Converter annotated with @ConfigurationPropertiesBinding does not get selected if targetType has a static factory method from sourceType
Custom Converter annotated with @ConfigurationPropertiesBinding does not get selected if targetType has a static factory method different return type
Jun 10, 2022
I'm upgrading my application from 2.4.10 to 2.5.6 and one of my custom Converters does not get picked up anymore since a specific static factory method (
from
) is present on the targetType.After debugging this issue, it looks like in 2.5, the ObjectToObjectConverter wins, and gets called before my custom Converter is picked up.
Even though the return type of the factory method does not fit the targetType!
In my case the factory method returns an Optional.
while my Properties expects a
Data
.In spring-boot 2.4.x this works for me.
The converter gets selected by the Binder, and my property is bound to the properties class.
In spring boot 2.5, the exact same code base fails with
ConverterNotFoundException
The resulting error looks like this:
In my opinion this is a bug in the priority of the generic ObjectToObjectConverter (not taking into account the targetType) and user-defined custom converters!
Reproduce
A small reproducer is here: https://github.com/DieBauer/spring-boot-propertiesbinding
I have a properties class that gets instantiated with a custom data type like so:
and the corresponding properties class.
To make this work, I have a custom converter
Which is annotated like this in my Configuration class.
Potential workarounds
Register the converter manually
I can register my Converter directly in the conversionService bean, following #26294 and the linked SO question
like this:
rendering the @ConfigurationPropertiesBinding useless.
Rename my static factory method
When the static factory method does not match any of the three terms in the
ObjectToObjectConverter
(valueOf
,of
,from
) my converter gets picked up.Add a converter as per suggestion of the error message
Adding a converter of
Optional<?>
(or whatever the return type of the static factory method is) to your custom type might work.The text was updated successfully, but these errors were encountered: