Skip to content

Commit cb6a5ba

Browse files
committed
Support conversion from primitive array to Object[] in ConversionService
Prior to this commit, the ConversionService failed to convert a primitive array (such as int[]) to an Object[] due to an error in the logic in ArrayToArrayConverter. This commit addresses this by augmenting the "can bypass conversion" check in ArrayToArrayConverter to ensure that the supplied source object is an instance of the target type (i.e., that the source array can be cast to the target type array without conversion). Closes gh-33212
1 parent 3b2a818 commit cb6a5ba

File tree

2 files changed

+5
-6
lines changed

2 files changed

+5
-6
lines changed

spring-core/src/main/java/org/springframework/core/convert/support/ArrayToArrayConverter.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -34,6 +34,7 @@
3434
*
3535
* @author Keith Donald
3636
* @author Phillip Webb
37+
* @author Sam Brannen
3738
* @since 3.0
3839
*/
3940
final class ArrayToArrayConverter implements ConditionalGenericConverter {
@@ -64,8 +65,8 @@ public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
6465
public Object convert(@Nullable Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
6566
if (this.conversionService instanceof GenericConversionService genericConversionService) {
6667
TypeDescriptor targetElement = targetType.getElementTypeDescriptor();
67-
if (targetElement != null && genericConversionService.canBypassConvert(
68-
sourceType.getElementTypeDescriptor(), targetElement)) {
68+
if (targetElement != null && targetType.getType().isInstance(source) &&
69+
genericConversionService.canBypassConvert(sourceType.getElementTypeDescriptor(), targetElement)) {
6970
return source;
7071
}
7172
}

spring-core/src/test/java/org/springframework/core/convert/converter/DefaultConversionServiceTests.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343
import java.util.regex.Pattern;
4444
import java.util.stream.Stream;
4545

46-
import org.junit.jupiter.api.Disabled;
4746
import org.junit.jupiter.api.Test;
4847

4948
import org.springframework.core.MethodParameter;
@@ -640,8 +639,7 @@ void convertObjectArrayToIntArray() {
640639
assertThat(result).containsExactly(1, 2, 3);
641640
}
642641

643-
@Disabled("Primitive array to Object[] conversion is not currently supported")
644-
@Test
642+
@Test // gh-33212
645643
void convertIntArrayToObjectArray() {
646644
Object[] result = conversionService.convert(new int[] {1, 2}, Object[].class);
647645
assertThat(result).containsExactly(1, 2);

0 commit comments

Comments
 (0)