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
This commit is contained in:
parent
3b2a818f1e
commit
cb6a5baac5
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2022 the original author or authors.
|
* Copyright 2002-2024 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|
@ -34,6 +34,7 @@ import org.springframework.util.ObjectUtils;
|
||||||
*
|
*
|
||||||
* @author Keith Donald
|
* @author Keith Donald
|
||||||
* @author Phillip Webb
|
* @author Phillip Webb
|
||||||
|
* @author Sam Brannen
|
||||||
* @since 3.0
|
* @since 3.0
|
||||||
*/
|
*/
|
||||||
final class ArrayToArrayConverter implements ConditionalGenericConverter {
|
final class ArrayToArrayConverter implements ConditionalGenericConverter {
|
||||||
|
|
@ -64,8 +65,8 @@ final class ArrayToArrayConverter implements ConditionalGenericConverter {
|
||||||
public Object convert(@Nullable Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
|
public Object convert(@Nullable Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
|
||||||
if (this.conversionService instanceof GenericConversionService genericConversionService) {
|
if (this.conversionService instanceof GenericConversionService genericConversionService) {
|
||||||
TypeDescriptor targetElement = targetType.getElementTypeDescriptor();
|
TypeDescriptor targetElement = targetType.getElementTypeDescriptor();
|
||||||
if (targetElement != null && genericConversionService.canBypassConvert(
|
if (targetElement != null && targetType.getType().isInstance(source) &&
|
||||||
sourceType.getElementTypeDescriptor(), targetElement)) {
|
genericConversionService.canBypassConvert(sourceType.getElementTypeDescriptor(), targetElement)) {
|
||||||
return source;
|
return source;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -43,7 +43,6 @@ import java.util.UUID;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
import org.junit.jupiter.api.Disabled;
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
import org.springframework.core.MethodParameter;
|
import org.springframework.core.MethodParameter;
|
||||||
|
|
@ -640,8 +639,7 @@ class DefaultConversionServiceTests {
|
||||||
assertThat(result).containsExactly(1, 2, 3);
|
assertThat(result).containsExactly(1, 2, 3);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Disabled("Primitive array to Object[] conversion is not currently supported")
|
@Test // gh-33212
|
||||||
@Test
|
|
||||||
void convertIntArrayToObjectArray() {
|
void convertIntArrayToObjectArray() {
|
||||||
Object[] result = conversionService.convert(new int[] {1, 2}, Object[].class);
|
Object[] result = conversionService.convert(new int[] {1, 2}, Object[].class);
|
||||||
assertThat(result).containsExactly(1, 2);
|
assertThat(result).containsExactly(1, 2);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue