Skip to content

Commit 3e73724

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 (cherry picked from commit cb6a5ba)
1 parent e11a419 commit 3e73724

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

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

+4-3
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
}

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

+20-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 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.
@@ -607,8 +607,26 @@ void convertObjectArrayToIntArray() {
607607
assertThat(result).containsExactly(1, 2, 3);
608608
}
609609

610+
@Test // gh-33212
611+
void convertIntArrayToObjectArray() {
612+
Object[] result = conversionService.convert(new int[] {1, 2}, Object[].class);
613+
assertThat(result).containsExactly(1, 2);
614+
}
615+
616+
@Test
617+
void convertIntArrayToFloatArray() {
618+
Float[] result = conversionService.convert(new int[] {1, 2}, Float[].class);
619+
assertThat(result).containsExactly(1.0F, 2.0F);
620+
}
621+
622+
@Test
623+
void convertIntArrayToPrimitiveFloatArray() {
624+
float[] result = conversionService.convert(new int[] {1, 2}, float[].class);
625+
assertThat(result).containsExactly(1.0F, 2.0F);
626+
}
627+
610628
@Test
611-
void convertByteArrayToWrapperArray() {
629+
void convertPrimitiveByteArrayToByteWrapperArray() {
612630
byte[] byteArray = {1, 2, 3};
613631
Byte[] converted = conversionService.convert(byteArray, Byte[].class);
614632
assertThat(converted).isEqualTo(new Byte[]{1, 2, 3});

0 commit comments

Comments
 (0)