Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import java.math.BigDecimal;
import java.sql.JDBCType;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;
Expand Down Expand Up @@ -68,6 +69,11 @@ EntityWithDirectionsRepository repositoryWithDirections(JdbcRepositoryFactory fa
return factory.getRepository(EntityWithDirectionsRepository.class);
}

@Bean
EntityWithIdsRepository repositoryWithIds(JdbcRepositoryFactory factory) {
return factory.getRepository(EntityWithIdsRepository.class);
}

@Bean
JdbcCustomConversions jdbcCustomConversions() {
return new JdbcCustomConversions(asList(StringToBigDecimalConverter.INSTANCE, BigDecimalToString.INSTANCE,
Expand All @@ -78,6 +84,7 @@ JdbcCustomConversions jdbcCustomConversions() {

@Autowired EntityWithStringyBigDecimalRepository repository;
@Autowired EntityWithDirectionsRepository repositoryWithDirections;
@Autowired EntityWithIdsRepository repositoryWithIds;

/**
* In PostrgreSQL this fails if a simple converter like the following is used.
Expand Down Expand Up @@ -182,6 +189,19 @@ void saveAndLoadListOfDirectionsAsArray() {
assertThat(reloaded).isEqualTo(saved);
}

@Test // GH-2215
@EnabledOnFeature(TestDatabaseFeatures.Feature.SUPPORTS_ARRAYS)
void saveAndLoadListOfNullAsArray() {
var list = new ArrayList<Integer>();
list.add(null);

EntityWithIds saved = repositoryWithIds.save(new EntityWithIds(null, list));

EntityWithIds reloaded = repositoryWithIds.findById(saved.id).orElseThrow();

assertThat(reloaded).isEqualTo(saved);
}

interface EntityWithStringyBigDecimalRepository extends CrudRepository<EntityWithStringyBigDecimal, CustomId> {

@Query("SELECT * FROM ENTITY_WITH_STRINGY_BIG_DECIMAL WHERE DIRECTION IN (:types)")
Expand All @@ -193,6 +213,8 @@ interface EntityWithStringyBigDecimalRepository extends CrudRepository<EntityWit

interface EntityWithDirectionsRepository extends CrudRepository<EntityWithDirections, Long> {}

interface EntityWithIdsRepository extends CrudRepository<EntityWithIds, Long> {}

private static class EntityWithStringyBigDecimal {

@Id CustomId id;
Expand Down Expand Up @@ -223,6 +245,9 @@ enum Direction {
LEFT, CENTER, RIGHT
}

record EntityWithIds(@Id Long id, List<Integer> ids) {
}

@WritingConverter
enum StringToBigDecimalConverter implements Converter<String, JdbcValue> {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
CREATE TABLE ENTITY_WITH_STRINGY_BIG_DECIMAL ( id IDENTITY PRIMARY KEY, Stringy_number DECIMAL(20,10), DIRECTION INTEGER);
CREATE TABLE OTHER_ENTITY ( ID IDENTITY PRIMARY KEY, CREATED DATE, ENTITY_WITH_STRINGY_BIG_DECIMAL INTEGER);
CREATE TABLE ENTITY_WITH_DIRECTIONS ( ID IDENTITY PRIMARY KEY, DIRECTIONS INTEGER ARRAY);
CREATE TABLE ENTITY_WITH_IDS ( ID IDENTITY PRIMARY KEY, IDS INTEGER ARRAY);
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
CREATE TABLE ENTITY_WITH_STRINGY_BIG_DECIMAL ( id IDENTITY PRIMARY KEY, Stringy_number DECIMAL(20,10), DIRECTION INTEGER);
CREATE TABLE OTHER_ENTITY ( ID IDENTITY PRIMARY KEY, CREATED DATE, ENTITY_WITH_STRINGY_BIG_DECIMAL INTEGER);
CREATE TABLE ENTITY_WITH_DIRECTIONS ( ID IDENTITY PRIMARY KEY, DIRECTIONS INTEGER ARRAY);
CREATE TABLE ENTITY_WITH_IDS ( ID IDENTITY PRIMARY KEY, IDS INTEGER ARRAY);

Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
CREATE TABLE ENTITY_WITH_STRINGY_BIG_DECIMAL ( id SERIAL PRIMARY KEY, Stringy_number DECIMAL(20,10), DIRECTION INTEGER);
CREATE TABLE OTHER_ENTITY ( ID SERIAL PRIMARY KEY, CREATED DATE, ENTITY_WITH_STRINGY_BIG_DECIMAL INTEGER);
CREATE TABLE ENTITY_WITH_DIRECTIONS ( ID SERIAL PRIMARY KEY, DIRECTIONS INTEGER ARRAY);
CREATE TABLE ENTITY_WITH_IDS ( ID SERIAL PRIMARY KEY, IDS INTEGER ARRAY);
Original file line number Diff line number Diff line change
Expand Up @@ -815,12 +815,11 @@ private Object writeCollection(Iterable<?> value, TypeInformation<?> type) {
// if we succeeded converting the members of the collection, we actually ignore the fallback targetType since that
// was derived without considering custom conversions.
Class<?> targetType = type.getType();
if (!mapped.isEmpty()) {
if (!mapped.isEmpty() && mapped.get(0) != null) {

Class<?> targetComponentType = mapped.get(0).getClass();
targetType = Array.newInstance(targetComponentType, 0).getClass();
}

Object converted = getConversionService().convert(mapped, targetType);

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