Skip to content

Commit 1847d2f

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) (cherry picked from commit 3e73724)
1 parent 351a17a commit 1847d2f

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

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

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 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,7 +65,7 @@ public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
6465
public Object convert(@Nullable Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
6566
if (this.conversionService instanceof GenericConversionService) {
6667
TypeDescriptor targetElement = targetType.getElementTypeDescriptor();
67-
if (targetElement != null &&
68+
if (targetElement != null && targetType.getType().isInstance(source) &&
6869
((GenericConversionService) this.conversionService).canBypassConvert(
6970
sourceType.getElementTypeDescriptor(), targetElement)) {
7071
return source;

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

+21-3
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.
@@ -626,9 +626,27 @@ void convertObjectArrayToIntArray() {
626626
assertThat(result[2]).isEqualTo(3);
627627
}
628628

629+
@Test // gh-33212
630+
void convertIntArrayToObjectArray() {
631+
Object[] result = conversionService.convert(new int[] {1, 2}, Object[].class);
632+
assertThat(result).containsExactly(1, 2);
633+
}
634+
629635
@Test
630-
void convertByteArrayToWrapperArray() {
631-
byte[] byteArray = new byte[] {1, 2, 3};
636+
void convertIntArrayToFloatArray() {
637+
Float[] result = conversionService.convert(new int[] {1, 2}, Float[].class);
638+
assertThat(result).containsExactly(1.0F, 2.0F);
639+
}
640+
641+
@Test
642+
void convertIntArrayToPrimitiveFloatArray() {
643+
float[] result = conversionService.convert(new int[] {1, 2}, float[].class);
644+
assertThat(result).containsExactly(1.0F, 2.0F);
645+
}
646+
647+
@Test
648+
void convertPrimitiveByteArrayToByteWrapperArray() {
649+
byte[] byteArray = {1, 2, 3};
632650
Byte[] converted = conversionService.convert(byteArray, Byte[].class);
633651
assertThat(converted).isEqualTo(new Byte[]{1, 2, 3});
634652
}

0 commit comments

Comments
 (0)