#354 - Allow saving an entity with a read-only collection-like property.

Previously a NullPointerException would be thrown.

Original pull request: #355.
This commit is contained in:
Louis Morgan
2020-04-22 11:19:41 +01:00
committed by Mark Paluch
parent 8c0d01ae8c
commit bf19cb36a4
2 changed files with 21 additions and 1 deletions

View File

@@ -206,7 +206,7 @@ public class DefaultReactiveDataAccessStrategy implements ReactiveDataAccessStra
for (RelationalPersistentProperty property : entity) {
SettableValue value = row.get(property.getColumnName());
if (shouldConvertArrayValue(property, value)) {
if (value != null && shouldConvertArrayValue(property, value)) {
SettableValue writeValue = getArrayValue(value, property);
row.put(property.getColumnName(), writeValue);

View File

@@ -36,6 +36,7 @@ import java.util.function.Function;
import org.junit.Test;
import org.springframework.data.annotation.ReadOnlyProperty;
import org.springframework.data.r2dbc.dialect.R2dbcDialect;
import org.springframework.data.r2dbc.mapping.SettableValue;
import org.springframework.data.relational.core.sql.SqlIdentifier;
@@ -177,6 +178,16 @@ public abstract class ReactiveDataAccessStrategyTestSupport {
testType(PrimitiveTypes::setBinary, PrimitiveTypes::getBinary, "hello".getBytes(), "binary");
}
@Test // gh-354
public void shouldNotWriteReadOnlyFields() {
TypeWithReadOnlyFields toSave = new TypeWithReadOnlyFields();
toSave.setWritableField("writable");
toSave.setReadOnlyField("readonly");
toSave.setReadOnlyArrayField("readonly_array".getBytes());
assertThat(getStrategy().getOutboundRow(toSave))
.containsOnlyKeys(SqlIdentifier.unquoted("writable_field"));
}
private <T> void testType(BiConsumer<PrimitiveTypes, T> setter, Function<PrimitiveTypes, T> getter, T testValue,
String fieldname) {
@@ -235,4 +246,13 @@ public abstract class ReactiveDataAccessStrategyTestSupport {
UUID uuid;
}
@Data
static class TypeWithReadOnlyFields {
String writableField;
@ReadOnlyProperty
String readOnlyField;
@ReadOnlyProperty
byte[] readOnlyArrayField;
}
}