Announcement Announcement Module
Collapse
No announcement yet.
TypeConverterDelegate converts arrays size 1 into a single object Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • TypeConverterDelegate converts arrays size 1 into a single object

    I am migrating a code from Spring version 2.0 to Spring 3.0.5.RELEASE but a piece of code doesn't work in newer version. The scenario is similar to the following:

    Code:
           <bean id="arraySizeOne" class="ArrayFactory" factory-method="getStaticArraySizeOne"/>
    
    	<bean id="arraySizeTwo" class="ArrayFactory" factory-method="getStaticArraySizeTwo"/>
    
    	<bean id="fromPropertySizeOne" class="ObjectHolder">
    		<property name="object">
    			<ref bean="arraySizeOne"/>
    		</property>
    	</bean>
    	
    	<bean id="fromPropertySizeTwo" class="ObjectHolder">
    		<property name="object">
    			<ref bean="arraySizeTwo"/>
    		</property>
    	</bean>
    
    	<bean id="fromConstructorSizeOne" class="ObjectHolder">
    		<constructor-arg index="0" type="java.lang.Object">
    			<ref bean="arraySizeOne"/>
    		</constructor-arg>
    	</bean>
    	
    	<bean id="fromConstructorSizeTwo" class="ObjectHolder">
    		<constructor-arg index="0" type="java.lang.Object">
    			<ref bean="arraySizeTwo"/>
    		</constructor-arg>
    	</bean>

    In both versions, using property and constructor argument, the array of size one is converted to a single String, so the old code -- that used to work in Spring 2 -- throws a java.lang.ClassCastException when tries to cast to java.lang.String[].

    Of course, there is a workaround for this, that consist in using a strong String[] type instead of Object. But I was wondering if this is a bug in the spring-beans code. The affected line is
    Code:
    org.springframework.beans.TypeConverterDelegate.convertIfNecessary(String, Object, Object, Class<T>, TypeDescriptor):181
    
    if (convertedValue.getClass().isArray() && Array.getLength(convertedValue) == 1) {
         convertedValue = Array.get(convertedValue, 0);
    }

    Does somebody know why that type of conversion is performed or is it a defect of the code?
Working...
X