Skip to content

Commit a25cbfe

Browse files
mcvaycmcvayc
mcvayc
authored andcommitted
Support primitive array to Object[] conversion.
Convert if source and target types are not same primitive or non-primitive. Closes spring-projects#33212.
1 parent 31a3119 commit a25cbfe

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

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

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,27 @@ public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
6464
public Object convert(@Nullable Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
6565
if (this.conversionService instanceof GenericConversionService genericConversionService) {
6666
TypeDescriptor targetElement = targetType.getElementTypeDescriptor();
67-
if (targetElement != null && genericConversionService.canBypassConvert(
68-
sourceType.getElementTypeDescriptor(), targetElement)) {
69-
return source;
67+
if (targetElement != null) {
68+
TypeDescriptor sourceElement = sourceType.getElementTypeDescriptor();
69+
if (genericConversionService.canBypassConvert(sourceElement, targetElement) && canBypassArrayConvert(sourceElement, targetElement)) {
70+
return source;
71+
}
7072
}
7173
}
7274
List<Object> sourceList = Arrays.asList(ObjectUtils.toObjectArray(source));
7375
return this.helperConverter.convert(sourceList, sourceType, targetType);
7476
}
7577

78+
private static boolean canBypassArrayConvert(@Nullable TypeDescriptor sourceElement, TypeDescriptor targetElement) {
79+
if (sourceElement == null) {
80+
return true;
81+
}
82+
83+
if(sourceElement.isPrimitive() && targetElement.isPrimitive()) {
84+
return sourceElement.getObjectType() == targetElement.getObjectType();
85+
}
86+
87+
return !sourceElement.isPrimitive() && !targetElement.isPrimitive();
88+
}
89+
7690
}

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

Lines changed: 6 additions & 2 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;
@@ -634,13 +633,18 @@ void convertObjectArrayToIntegerArray() {
634633
assertThat(result).containsExactly(1, 2, 3);
635634
}
636635

636+
@Test
637+
void convertIntegerArrayToObjectArray() {
638+
Object[] result = conversionService.convert(new Integer[] {1, 2, 3}, Object[].class);
639+
assertThat(result).containsExactly(1, 2, 3);
640+
}
641+
637642
@Test
638643
void convertObjectArrayToIntArray() {
639644
int[] result = conversionService.convert(new Object[] {1, 2, 3}, int[].class);
640645
assertThat(result).containsExactly(1, 2, 3);
641646
}
642647

643-
@Disabled("Primitive array to Object[] conversion is not currently supported")
644648
@Test
645649
void convertIntArrayToObjectArray() {
646650
Object[] result = conversionService.convert(new int[] {1, 2}, Object[].class);

0 commit comments

Comments
 (0)