Skip to content

TypeConverterDelegate standard conversion changes single element array to a class instance when trying to convert to Object [SPR-10996] #15624

@spring-projects-issues

Description

@spring-projects-issues

Martin Scheffelaar opened SPR-10996 and commented

TypeConverterDelegate.convertIfNecessary exhibits weird behavior for a Foo[] newValue and requiredType == Object. If newValue.length != 1, the method will return newValue as expected, as Foo[] is an instance of Object. However, if newValue length == 1, the method will return newValue[0], which is an unnecessary conversion.

I ran into this issue when trying to set a bean Object-typed property to a String[] using user input in a webapp; the actual type of the property would be String[] for a {"foo", "bar"...} input and String for a {"foo"} input, which broke some logic down the line.

The culprit appears to be this bit of code; it's only reached if requiredType is not an array, which is fine for any value aside from Object. I'd propose adding a check here to see if convertedValue instanceof requiredType, or explicitly checking if requiredType == Object, or something similar.

if (convertedValue.getClass().isArray() && Array.getLength(convertedValue) == 1) {
    convertedValue = Array.get(convertedValue, 0);
    standardConversion = true;
}

Affects: 4.0 M3

Sub-tasks:

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions