From 1847d2f686bb70f1e614a37dab91439a00778db8 Mon Sep 17 00:00:00 2001 From: Sam Brannen <104798+sbrannen@users.noreply.github.com> Date: Sun, 4 Aug 2024 17:13:56 +0300 Subject: [PATCH] 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 cb6a5baac508921486fd13a7a91cab9d7625868b) (cherry picked from commit 3e7372491c6a152c473c746dd75e029e0a6ac8b8) --- .../support/ArrayToArrayConverter.java | 5 ++-- .../DefaultConversionServiceTests.java | 24 ++++++++++++++++--- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/spring-core/src/main/java/org/springframework/core/convert/support/ArrayToArrayConverter.java b/spring-core/src/main/java/org/springframework/core/convert/support/ArrayToArrayConverter.java index a51144741c..5af4bd785c 100644 --- a/spring-core/src/main/java/org/springframework/core/convert/support/ArrayToArrayConverter.java +++ b/spring-core/src/main/java/org/springframework/core/convert/support/ArrayToArrayConverter.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2024 the original author or authors. * * Licensed under the Apache License, Version 2.0 (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 Phillip Webb + * @author Sam Brannen * @since 3.0 */ final class ArrayToArrayConverter implements ConditionalGenericConverter { @@ -64,7 +65,7 @@ final class ArrayToArrayConverter implements ConditionalGenericConverter { public Object convert(@Nullable Object source, TypeDescriptor sourceType, TypeDescriptor targetType) { if (this.conversionService instanceof GenericConversionService) { TypeDescriptor targetElement = targetType.getElementTypeDescriptor(); - if (targetElement != null && + if (targetElement != null && targetType.getType().isInstance(source) && ((GenericConversionService) this.conversionService).canBypassConvert( sourceType.getElementTypeDescriptor(), targetElement)) { return source; diff --git a/spring-core/src/test/java/org/springframework/core/convert/converter/DefaultConversionServiceTests.java b/spring-core/src/test/java/org/springframework/core/convert/converter/DefaultConversionServiceTests.java index 09be4d15fb..db3de65357 100644 --- a/spring-core/src/test/java/org/springframework/core/convert/converter/DefaultConversionServiceTests.java +++ b/spring-core/src/test/java/org/springframework/core/convert/converter/DefaultConversionServiceTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2023 the original author or authors. + * Copyright 2002-2024 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -626,9 +626,27 @@ class DefaultConversionServiceTests { assertThat(result[2]).isEqualTo(3); } + @Test // gh-33212 + void convertIntArrayToObjectArray() { + Object[] result = conversionService.convert(new int[] {1, 2}, Object[].class); + assertThat(result).containsExactly(1, 2); + } + @Test - void convertByteArrayToWrapperArray() { - byte[] byteArray = new byte[] {1, 2, 3}; + void convertIntArrayToFloatArray() { + Float[] result = conversionService.convert(new int[] {1, 2}, Float[].class); + assertThat(result).containsExactly(1.0F, 2.0F); + } + + @Test + void convertIntArrayToPrimitiveFloatArray() { + float[] result = conversionService.convert(new int[] {1, 2}, float[].class); + assertThat(result).containsExactly(1.0F, 2.0F); + } + + @Test + void convertPrimitiveByteArrayToByteWrapperArray() { + byte[] byteArray = {1, 2, 3}; Byte[] converted = conversionService.convert(byteArray, Byte[].class); assertThat(converted).isEqualTo(new Byte[]{1, 2, 3}); }