Skip to content

Commit ea00027

Browse files
committed
Fix empty collection conversion.
We now consider the target type for empty collections. Closes #796
1 parent fbe3bfb commit ea00027

File tree

3 files changed

+47
-5
lines changed

3 files changed

+47
-5
lines changed

src/main/java/org/springframework/data/r2dbc/convert/MappingR2dbcConverter.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,6 @@ private Object readFrom(Row row, @Nullable RowMetadata metadata, RelationalPersi
190190
}
191191
}
192192

193-
194193
public Object readValue(@Nullable Object value, TypeInformation<?> type) {
195194

196195
if (null == value) {
@@ -553,6 +552,8 @@ public Object getArrayValue(ArrayColumns arrayColumns, RelationalPersistentPrope
553552
actualType = property.getActualType();
554553
}
555554

555+
actualType = getTargetType(actualType);
556+
556557
Class<?> targetType = arrayColumns.getArrayType(actualType);
557558

558559
if (!property.isArray() || !targetType.isAssignableFrom(value.getClass())) {

src/main/java/org/springframework/data/r2dbc/core/DefaultReactiveDataAccessStrategy.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,6 @@ private Parameter getArrayValue(Parameter value, RelationalPersistentProperty pr
240240
ArrayColumns arrayColumns = this.dialect.getArraySupport();
241241

242242
if (!arrayColumns.isSupported()) {
243-
244243
throw new InvalidDataAccessResourceUsageException(
245244
"Dialect " + this.dialect.getClass().getName() + " does not support array columns");
246245
}
@@ -266,8 +265,7 @@ private Parameter getArrayValue(Parameter value, RelationalPersistentProperty pr
266265
return Parameter.empty(targetArrayType);
267266
}
268267

269-
return Parameter.fromOrEmpty(this.converter.getArrayValue(arrayColumns, property, value.getValue()),
270-
actualType);
268+
return Parameter.fromOrEmpty(this.converter.getArrayValue(arrayColumns, property, value.getValue()), actualType);
271269
}
272270

273271
/*

src/test/java/org/springframework/data/r2dbc/core/PostgresReactiveDataAccessStrategyTests.java

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,11 @@
1717

1818
import static org.springframework.data.r2dbc.testing.Assertions.*;
1919

20+
import io.r2dbc.postgresql.codec.Interval;
2021
import lombok.RequiredArgsConstructor;
2122

23+
import java.time.Duration;
24+
import java.util.ArrayList;
2225
import java.util.Arrays;
2326
import java.util.Collections;
2427
import java.util.EnumSet;
@@ -28,6 +31,7 @@
2831
import org.junit.jupiter.api.Test;
2932

3033
import org.springframework.core.convert.converter.Converter;
34+
import org.springframework.data.convert.ReadingConverter;
3135
import org.springframework.data.convert.WritingConverter;
3236
import org.springframework.data.r2dbc.convert.EnumWriteSupport;
3337
import org.springframework.data.r2dbc.dialect.PostgresDialect;
@@ -41,7 +45,8 @@
4145
*/
4246
public class PostgresReactiveDataAccessStrategyTests extends ReactiveDataAccessStrategyTestSupport {
4347

44-
private final ReactiveDataAccessStrategy strategy = new DefaultReactiveDataAccessStrategy(PostgresDialect.INSTANCE);
48+
private final ReactiveDataAccessStrategy strategy = new DefaultReactiveDataAccessStrategy(PostgresDialect.INSTANCE,
49+
Arrays.asList(DurationToIntervalConverter.INSTANCE, IntervalToDurationConverter.INSTANCE));
4550

4651
@Override
4752
protected ReactiveDataAccessStrategy getStrategy() {
@@ -116,6 +121,17 @@ void shouldApplyCustomConversionForNull() {
116121
assertThat(outboundRow).containsColumn("my_objects").withColumn("my_objects").isEmpty().hasType(String.class);
117122
}
118123

124+
@Test // gh-796
125+
void shouldApplyCustomConversionForEmptyList() {
126+
127+
WithDuration withDuration = new WithDuration();
128+
withDuration.durations = new ArrayList<>();
129+
130+
OutboundRow outboundRow = strategy.getOutboundRow(withDuration);
131+
132+
assertThat(outboundRow).containsColumn("durations").withColumn("durations").hasType(Interval[].class);
133+
}
134+
119135
@Test // gh-252, gh-593
120136
void shouldConvertCollectionOfEnumToString() {
121137

@@ -202,6 +218,11 @@ static class WithArray {
202218
List<String> stringList;
203219
}
204220

221+
static class WithDuration {
222+
223+
List<Duration> durations;
224+
}
225+
205226
static class WithEnumCollections {
206227

207228
MyEnum[] enumArray;
@@ -242,5 +263,27 @@ public String convert(List<MyObject> myObjects) {
242263
}
243264
}
244265

266+
@WritingConverter
267+
enum DurationToIntervalConverter implements Converter<Duration, Interval> {
268+
269+
INSTANCE;
270+
271+
@Override
272+
public Interval convert(Duration duration) {
273+
return Interval.of(duration);
274+
}
275+
}
276+
277+
@ReadingConverter
278+
enum IntervalToDurationConverter implements Converter<Interval, Duration> {
279+
280+
INSTANCE;
281+
282+
@Override
283+
public Duration convert(Interval interval) {
284+
return interval.getDuration();
285+
}
286+
}
287+
245288
private static class MyEnumSupport extends EnumWriteSupport<MyEnum> {}
246289
}

0 commit comments

Comments
 (0)