Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
* @author Juergen Hoeller
* @author Rob Harrop
* @author Dave Syer
* @author Yanming Zhou
* @since 2.0
* @see BeanWrapperImpl
* @see SimpleTypeConverter
Expand Down Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
* @author Juergen Hoeller
* @author Chris Beams
* @author Sam Brannen
* @author Yanming Zhou
*/
public class ClassPathXmlApplicationContextTests {

Expand Down Expand Up @@ -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();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"/>
Expand Down