From acecf3753ff9f0b8856c56b4605ddc4d1a975177 Mon Sep 17 00:00:00 2001 From: Yanming Zhou Date: Mon, 27 Nov 2023 17:03:00 +0800 Subject: [PATCH] Correct conversion from Resource[] with length 1 to Collection Fix GH-31693 --- .../springframework/beans/TypeConverterDelegate.java | 11 ++++++----- .../support/ClassPathXmlApplicationContextTests.java | 5 +++++ .../springframework/context/support/test/contextA.xml | 5 +++++ 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/spring-beans/src/main/java/org/springframework/beans/TypeConverterDelegate.java b/spring-beans/src/main/java/org/springframework/beans/TypeConverterDelegate.java index a3bfd17bc169..2fc9486c50f6 100644 --- a/spring-beans/src/main/java/org/springframework/beans/TypeConverterDelegate.java +++ b/spring-beans/src/main/java/org/springframework/beans/TypeConverterDelegate.java @@ -49,6 +49,7 @@ * @author Juergen Hoeller * @author Rob Harrop * @author Dave Syer + * @author Yanming Zhou * @since 2.0 * @see BeanWrapperImpl * @see SimpleTypeConverter @@ -178,15 +179,15 @@ else if (requiredType.isArray()) { 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. diff --git a/spring-context/src/test/java/org/springframework/context/support/ClassPathXmlApplicationContextTests.java b/spring-context/src/test/java/org/springframework/context/support/ClassPathXmlApplicationContextTests.java index 170b1b656134..449518aeab33 100644 --- a/spring-context/src/test/java/org/springframework/context/support/ClassPathXmlApplicationContextTests.java +++ b/spring-context/src/test/java/org/springframework/context/support/ClassPathXmlApplicationContextTests.java @@ -48,6 +48,7 @@ * @author Juergen Hoeller * @author Chris Beams * @author Sam Brannen + * @author Yanming Zhou */ public class ClassPathXmlApplicationContextTests { @@ -223,6 +224,10 @@ void resourceArrayPropertyEditor() throws IOException { 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(); } diff --git a/spring-context/src/test/resources/org/springframework/context/support/test/contextA.xml b/spring-context/src/test/resources/org/springframework/context/support/test/contextA.xml index d58a2e82587a..7a140cfdaad3 100644 --- a/spring-context/src/test/resources/org/springframework/context/support/test/contextA.xml +++ b/spring-context/src/test/resources/org/springframework/context/support/test/contextA.xml @@ -24,6 +24,11 @@ + + + + +