Skip to content

Commit bf19cb3

Browse files
ljrmorganmp911de
authored andcommitted
#354 - Allow saving an entity with a read-only collection-like property.
Previously a NullPointerException would be thrown. Original pull request: #355.
1 parent 8c0d01a commit bf19cb3

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ public OutboundRow getOutboundRow(Object object) {
206206
for (RelationalPersistentProperty property : entity) {
207207

208208
SettableValue value = row.get(property.getColumnName());
209-
if (shouldConvertArrayValue(property, value)) {
209+
if (value != null && shouldConvertArrayValue(property, value)) {
210210

211211
SettableValue writeValue = getArrayValue(value, property);
212212
row.put(property.getColumnName(), writeValue);

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

+20
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636

3737
import org.junit.Test;
3838

39+
import org.springframework.data.annotation.ReadOnlyProperty;
3940
import org.springframework.data.r2dbc.dialect.R2dbcDialect;
4041
import org.springframework.data.r2dbc.mapping.SettableValue;
4142
import org.springframework.data.relational.core.sql.SqlIdentifier;
@@ -177,6 +178,16 @@ public void shouldReadAndWriteBinary() {
177178
testType(PrimitiveTypes::setBinary, PrimitiveTypes::getBinary, "hello".getBytes(), "binary");
178179
}
179180

181+
@Test // gh-354
182+
public void shouldNotWriteReadOnlyFields() {
183+
TypeWithReadOnlyFields toSave = new TypeWithReadOnlyFields();
184+
toSave.setWritableField("writable");
185+
toSave.setReadOnlyField("readonly");
186+
toSave.setReadOnlyArrayField("readonly_array".getBytes());
187+
assertThat(getStrategy().getOutboundRow(toSave))
188+
.containsOnlyKeys(SqlIdentifier.unquoted("writable_field"));
189+
}
190+
180191
private <T> void testType(BiConsumer<PrimitiveTypes, T> setter, Function<PrimitiveTypes, T> getter, T testValue,
181192
String fieldname) {
182193

@@ -235,4 +246,13 @@ static class PrimitiveTypes {
235246

236247
UUID uuid;
237248
}
249+
250+
@Data
251+
static class TypeWithReadOnlyFields {
252+
String writableField;
253+
@ReadOnlyProperty
254+
String readOnlyField;
255+
@ReadOnlyProperty
256+
byte[] readOnlyArrayField;
257+
}
238258
}

0 commit comments

Comments
 (0)