Use ConversionService
to convert POJO to array for SpEL varargs invocations
#34371
Labels
in: core
Issues in core modules (aop, beans, core, context, expression)
type: enhancement
A general enhancement
Milestone
We have a scenario where we have a POJO that is "like a list/array", but actually isn't. Which we registered to the
ConversionService
as being convertible toCollection<Object>
andObject[]
.However, even though we've registered this POJO as being convertible to
Collection
andObject[]
, function references aren't able to take advantage of that, and instead attempts to convert the POJO to the element type instead of the varargs container type.Reproduction
Spring Framework:
6.2.2
Here's a simple test class to reproduce the issue:
Now running the
testVarArgsFunction
test will throw the following exception:Potential Fix
Looking at ReflectionHelper, it seems that the detection for whether the last argument should be converted to the
varargsArrayType
orvarargsComponentType
depends on if the argument itself is a literalList
or an array implementation.Looking at the comments above and below this else-if block, maybe the fix for this is to change:
to something like:
That way you'd still preserve the use case where you put priority on converting to the
varargsComponentType
over thevarargsArrayType
, which (I think) would preserve avoiding edge cases like accidentally convertingString
toString[]
viaStringToArrayConverter
.The text was updated successfully, but these errors were encountered: