Browse Source

Correct conversion from Resource[] with length 1 to Collection<Resource>

Fix GH-31693
pull/31736/head
Yanming Zhou 2 years ago committed by Juergen Hoeller
parent
commit
feef98b73c
  1. 11
      spring-beans/src/main/java/org/springframework/beans/TypeConverterDelegate.java
  2. 5
      spring-context/src/test/java/org/springframework/context/support/ClassPathXmlApplicationContextTests.java
  3. 5
      spring-context/src/test/resources/org/springframework/context/support/test/contextA.xml

11
spring-beans/src/main/java/org/springframework/beans/TypeConverterDelegate.java

@ -49,6 +49,7 @@ import org.springframework.util.StringUtils; @@ -49,6 +49,7 @@ import org.springframework.util.StringUtils;
* @author Juergen Hoeller
* @author Rob Harrop
* @author Dave Syer
* @author Yanming Zhou
* @since 2.0
* @see BeanWrapperImpl
* @see SimpleTypeConverter
@ -178,15 +179,15 @@ class TypeConverterDelegate { @@ -178,15 +179,15 @@ class TypeConverterDelegate {
return (T) convertToTypedArray(convertedValue, propertyName, requiredType.componentType());
}
else if (convertedValue.getClass().isArray()) {
if (Array.getLength(convertedValue) == 1) {
convertedValue = Array.get(convertedValue, 0);
standardConversion = true;
}
else if (Collection.class.isAssignableFrom(requiredType)) {
if (Collection.class.isAssignableFrom(requiredType)) {
convertedValue = convertToTypedCollection(CollectionUtils.arrayToList(convertedValue),
propertyName, requiredType, typeDescriptor);
standardConversion = true;
}
else if (Array.getLength(convertedValue) == 1) {
convertedValue = Array.get(convertedValue, 0);
standardConversion = true;
}
}
else if (convertedValue instanceof Collection<?> coll) {
// Convert elements to target type, if determined.

5
spring-context/src/test/java/org/springframework/context/support/ClassPathXmlApplicationContextTests.java

@ -48,6 +48,7 @@ import static org.assertj.core.api.Assertions.assertThatExceptionOfType; @@ -48,6 +48,7 @@ import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
* @author Juergen Hoeller
* @author Chris Beams
* @author Sam Brannen
* @author Yanming Zhou
*/
public class ClassPathXmlApplicationContextTests {
@ -223,6 +224,10 @@ public class ClassPathXmlApplicationContextTests { @@ -223,6 +224,10 @@ public class ClassPathXmlApplicationContextTests {
Service service = ctx.getBean("service", Service.class);
assertThat(service.getResources()).containsExactlyInAnyOrder(contextA, contextB, contextC);
assertThat(service.getResourceSet()).containsExactlyInAnyOrder(contextA, contextB, contextC);
Service service3 = ctx.getBean("service3", Service.class);
assertThat(service3.getResources()).containsOnly(new ClassPathResource(FQ_CONTEXT_A));
assertThat(service3.getResourceSet()).containsOnly(new ClassPathResource(FQ_CONTEXT_A));
ctx.close();
}

5
spring-context/src/test/resources/org/springframework/context/support/test/contextA.xml

@ -24,6 +24,11 @@ @@ -24,6 +24,11 @@
<property name="resources" value="/org/springframework/context/support/test/context*.xml"/>
</bean>
<bean name="service3" class="org.springframework.context.support.Service">
<property name="resources" value="/org/springframework/context/support/test/contextA.xml"/>
<property name="resourceSet" value="/org/springframework/context/support/test/contextA.xml"/>
</bean>
<bean name="autowiredService" class="org.springframework.context.support.AutowiredService" autowire="byName"/>
<bean name="autowiredService2" class="org.springframework.context.support.AutowiredService" autowire="byType"/>

Loading…
Cancel
Save