#161 - Polishing.

Properly convert null arrays to the corresponding, driver-supported array type.
This commit is contained in:
Mark Paluch
2019-09-05 15:12:28 +02:00
parent 611b73149b
commit 084273f026
3 changed files with 26 additions and 5 deletions

View File

@@ -37,9 +37,9 @@ import org.springframework.data.r2dbc.mapping.OutboundRow;
import org.springframework.data.r2dbc.mapping.R2dbcMappingContext;
import org.springframework.data.r2dbc.mapping.SettableValue;
import org.springframework.data.r2dbc.query.UpdateMapper;
import org.springframework.data.r2dbc.support.ArrayUtils;
import org.springframework.data.relational.core.dialect.ArrayColumns;
import org.springframework.data.relational.core.dialect.RenderContextFactory;
import org.springframework.data.relational.core.mapping.RelationalMappingContext;
import org.springframework.data.relational.core.mapping.RelationalPersistentEntity;
import org.springframework.data.relational.core.mapping.RelationalPersistentProperty;
import org.springframework.lang.Nullable;
@@ -213,7 +213,7 @@ public class DefaultReactiveDataAccessStrategy implements ReactiveDataAccessStra
}
private boolean shouldConvertArrayValue(RelationalPersistentProperty property, SettableValue value) {
return value != null && value.hasValue() && property.isCollectionLike();
return property.isCollectionLike();
}
private SettableValue getArrayValue(SettableValue value, RelationalPersistentProperty property) {
@@ -225,9 +225,18 @@ public class DefaultReactiveDataAccessStrategy implements ReactiveDataAccessStra
throw new InvalidDataAccessResourceUsageException(
"Dialect " + this.dialect.getClass().getName() + " does not support array columns");
}
Class<?> actualType = property.getActualType();
if (value.isEmpty()) {
Class<?> targetType = arrayColumns.getArrayType(actualType);
int depth = actualType.isArray() ? ArrayUtils.getDimensionDepth(actualType) : 1;
Class<?> targetArrayType = ArrayUtils.getArrayClass(targetType, depth);
return SettableValue.empty(targetArrayType);
}
return SettableValue.fromOrEmpty(this.converter.getArrayValue(arrayColumns, property, value.getValue()),
property.getActualType());
actualType);
}
/*

View File

@@ -115,6 +115,9 @@ public class PostgresIntegrationTests extends R2dbcIntegrationTestSupport {
assertThat(actual.multidimensionalArray[0]).containsExactly(1, 2, 3);
assertThat(actual.multidimensionalArray[1]).containsExactly(4, 5, 6);
});
client.update().table(EntityWithArrays.class).using(withArrays).then() //
.as(StepVerifier::create).verifyComplete();
}
private void insert(EntityWithArrays object) {

View File

@@ -41,7 +41,7 @@ public class PostgresReactiveDataAccessStrategyTests extends ReactiveDataAccessS
return strategy;
}
@Test
@Test // gh-161
public void shouldConvertPrimitiveMultidimensionArrayToWrapper() {
OutboundRow row = strategy.getOutboundRow(new WithMultidimensionalArray(new int[][] { { 1, 2, 3 }, { 4, 5 } }));
@@ -50,7 +50,16 @@ public class PostgresReactiveDataAccessStrategyTests extends ReactiveDataAccessS
assertThat(row.get("myarray").getValue()).isInstanceOf(Integer[][].class);
}
@Test
@Test // gh-161
public void shouldConvertNullArrayToDriverArrayType() {
OutboundRow row = strategy.getOutboundRow(new WithMultidimensionalArray(null));
assertThat(row.get("myarray").hasValue()).isFalse();
assertThat(row.get("myarray").getType()).isEqualTo(Integer[].class);
}
@Test // gh-161
public void shouldConvertCollectionToArray() {
OutboundRow row = strategy.getOutboundRow(new WithIntegerCollection(Arrays.asList(1, 2, 3)));