@Value Optional<...> field injection fails in case of registered ConversionService [SPR-17607] #22139
Labels
in: core
Issues in core modules (aop, beans, core, context, expression)
status: backported
An issue that has been backported to maintenance branches
type: regression
A bug that is also a regression
Milestone
ssundell opened SPR-17607 and commented
We've used Optional fields in our project to handle configurable features where configuration properties can be defined or left out. However, after upgrading from Springboot 1.5.3 to Springboot 2.1 and Spring Framework 5.x value injection fails.
For example, we've used annotations such as this:
This initializes fine, but when you actually try to use the Optional, it fails with an exception:
In other words: the type of the field is evaluated twice, and as a result, the post processor injects the intializing bean with
Optional<Optional<String>>
instead ofOptional<String>
.The flow seems to be like this:
DefaultListableBeanFactory.resolveDependency
recognized Optional and callsDefaultListableBeanFactory.createOptionalDependency
which then resolves the dependency and, through a couple of methods, ends up callingTypeConverterDelegate.convertIfNecessary
with bothrequiredType
== String and field'sTypeDescriptor
, which then identifies the type as Optional<String>. This ignores the requiredType enough so that it ends up callingObjectToOptionalConverter
, which, naturally, converts the provided property String into Optional<String>.DefaultListableBeanFactory.createOptionalDependency
, which now has Optional<String> created in step 4 in its hands.... and it wraps it inside another Optional by callingOptional.ofNullable
.Affects: 5.1.3
Attachments:
Referenced from: commits e714fc5, 183f367, 9cb5369
Backported to: 5.0.12, 4.3.22
The text was updated successfully, but these errors were encountered: