Skip to content

Commit 3daa844

Browse files
committed
Fix writing collections where first element is null
Closes #2215
1 parent 458050d commit 3daa844

2 files changed

Lines changed: 15 additions & 10 deletions

File tree

spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/JdbcRepositoryCustomConversionIntegrationTests.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import java.math.BigDecimal;
2323
import java.sql.JDBCType;
24+
import java.util.ArrayList;
2425
import java.util.Date;
2526
import java.util.List;
2627
import java.util.Set;
@@ -182,6 +183,19 @@ void saveAndLoadListOfDirectionsAsArray() {
182183
assertThat(reloaded).isEqualTo(saved);
183184
}
184185

186+
@Test
187+
@EnabledOnFeature(TestDatabaseFeatures.Feature.SUPPORTS_ARRAYS)
188+
void saveAndLoadListOfNullAsArray() {
189+
var list = new ArrayList<Direction>();
190+
list.add(null);
191+
192+
EntityWithDirections saved = repositoryWithDirections.save(new EntityWithDirections(null, list));
193+
194+
EntityWithDirections reloaded = repositoryWithDirections.findById(saved.id).orElseThrow();
195+
196+
assertThat(reloaded).isEqualTo(saved);
197+
}
198+
185199
interface EntityWithStringyBigDecimalRepository extends CrudRepository<EntityWithStringyBigDecimal, CustomId> {
186200

187201
@Query("SELECT * FROM ENTITY_WITH_STRINGY_BIG_DECIMAL WHERE DIRECTION IN (:types)")

spring-data-relational/src/main/java/org/springframework/data/relational/core/conversion/MappingRelationalConverter.java

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -809,16 +809,7 @@ private Object writeCollection(Iterable<?> value, TypeInformation<?> type) {
809809
return mapped;
810810
}
811811

812-
// if we succeeded converting the members of the collection, we actually ignore the fallback targetType since that
813-
// was derived without considering custom conversions.
814-
Class<?> targetType = type.getType();
815-
if (!mapped.isEmpty()) {
816-
817-
Class<?> targetComponentType = mapped.get(0).getClass();
818-
targetType = Array.newInstance(targetComponentType, 0).getClass();
819-
}
820-
821-
Object converted = getConversionService().convert(mapped, targetType);
812+
Object converted = getConversionService().convert(mapped, type.getType());
822813

823814
Assert.state(converted != null, "Converted must not be null");
824815

0 commit comments

Comments
 (0)