Skip to content

Commit feef98b

Browse files
quaffjhoeller
authored andcommitted
Correct conversion from Resource[] with length 1 to Collection<Resource>
Fix GH-31693
1 parent 4a6c3e8 commit feef98b

File tree

3 files changed

+16
-5
lines changed

3 files changed

+16
-5
lines changed

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
* @author Juergen Hoeller
5050
* @author Rob Harrop
5151
* @author Dave Syer
52+
* @author Yanming Zhou
5253
* @since 2.0
5354
* @see BeanWrapperImpl
5455
* @see SimpleTypeConverter
@@ -178,15 +179,15 @@ else if (requiredType.isArray()) {
178179
return (T) convertToTypedArray(convertedValue, propertyName, requiredType.componentType());
179180
}
180181
else if (convertedValue.getClass().isArray()) {
181-
if (Array.getLength(convertedValue) == 1) {
182-
convertedValue = Array.get(convertedValue, 0);
183-
standardConversion = true;
184-
}
185-
else if (Collection.class.isAssignableFrom(requiredType)) {
182+
if (Collection.class.isAssignableFrom(requiredType)) {
186183
convertedValue = convertToTypedCollection(CollectionUtils.arrayToList(convertedValue),
187184
propertyName, requiredType, typeDescriptor);
188185
standardConversion = true;
189186
}
187+
else if (Array.getLength(convertedValue) == 1) {
188+
convertedValue = Array.get(convertedValue, 0);
189+
standardConversion = true;
190+
}
190191
}
191192
else if (convertedValue instanceof Collection<?> coll) {
192193
// Convert elements to target type, if determined.

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
* @author Juergen Hoeller
4949
* @author Chris Beams
5050
* @author Sam Brannen
51+
* @author Yanming Zhou
5152
*/
5253
public class ClassPathXmlApplicationContextTests {
5354

@@ -223,6 +224,10 @@ void resourceArrayPropertyEditor() throws IOException {
223224
Service service = ctx.getBean("service", Service.class);
224225
assertThat(service.getResources()).containsExactlyInAnyOrder(contextA, contextB, contextC);
225226
assertThat(service.getResourceSet()).containsExactlyInAnyOrder(contextA, contextB, contextC);
227+
228+
Service service3 = ctx.getBean("service3", Service.class);
229+
assertThat(service3.getResources()).containsOnly(new ClassPathResource(FQ_CONTEXT_A));
230+
assertThat(service3.getResourceSet()).containsOnly(new ClassPathResource(FQ_CONTEXT_A));
226231
ctx.close();
227232
}
228233

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@
2424
<property name="resources" value="/org/springframework/context/support/test/context*.xml"/>
2525
</bean>
2626

27+
<bean name="service3" class="org.springframework.context.support.Service">
28+
<property name="resources" value="/org/springframework/context/support/test/contextA.xml"/>
29+
<property name="resourceSet" value="/org/springframework/context/support/test/contextA.xml"/>
30+
</bean>
31+
2732
<bean name="autowiredService" class="org.springframework.context.support.AutowiredService" autowire="byName"/>
2833

2934
<bean name="autowiredService2" class="org.springframework.context.support.AutowiredService" autowire="byType"/>

0 commit comments

Comments
 (0)