Skip to content

Commit 507340e

Browse files
committed
#250 - Throw IllegalArgumentException in StatementMapper if UPDATE contains no assignments.
We now properly throw an IllegalArgumentException. Previously, the update object was attempted to being mapped and this failed as the update object was null.
1 parent 85acdbb commit 507340e

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -192,15 +192,15 @@ private PreparedOperation<Update> getMappedObject(UpdateSpec updateSpec,
192192
BindMarkers bindMarkers = this.dialect.getBindMarkersFactory().create();
193193
Table table = Table.create(updateSpec.getTable());
194194

195+
if (updateSpec.getUpdate() == null || updateSpec.getUpdate().getAssignments().isEmpty()) {
196+
throw new IllegalArgumentException("UPDATE contains no assignments");
197+
}
198+
195199
BoundAssignments boundAssignments = this.updateMapper.getMappedObject(bindMarkers,
196200
updateSpec.getUpdate().getAssignments(), table, entity);
197201

198202
Bindings bindings;
199203

200-
if (boundAssignments.getAssignments().isEmpty()) {
201-
throw new IllegalStateException("UPDATE contains no assignments");
202-
}
203-
204204
bindings = boundAssignments.getBindings();
205205

206206
UpdateBuilder.UpdateWhere updateBuilder = StatementBuilder.update(table).set(boundAssignments.getAssignments());

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package org.springframework.data.r2dbc.core;
1717

18+
import static org.assertj.core.api.Assertions.*;
1819
import static org.mockito.Mockito.*;
1920
import static org.springframework.data.r2dbc.query.Criteria.*;
2021

@@ -37,6 +38,7 @@
3738
import org.reactivestreams.Publisher;
3839
import org.reactivestreams.Subscription;
3940

41+
import org.springframework.data.annotation.Id;
4042
import org.springframework.data.r2dbc.dialect.PostgresDialect;
4143
import org.springframework.data.r2dbc.mapping.SettableValue;
4244
import org.springframework.data.r2dbc.support.R2dbcExceptionTranslator;
@@ -352,4 +354,23 @@ public void rowsUpdatedShouldEmitSingleValue() {
352354
.expectNextCount(1) //
353355
.verifyComplete();
354356
}
357+
358+
@Test // gh-250
359+
public void shouldThrowExceptionForSingleColumnObjectUpdate() {
360+
361+
DefaultDatabaseClient databaseClient = (DefaultDatabaseClient) DatabaseClient.builder()
362+
.connectionFactory(connectionFactory) //
363+
.dataAccessStrategy(new DefaultReactiveDataAccessStrategy(PostgresDialect.INSTANCE)) //
364+
.build();
365+
366+
assertThatIllegalArgumentException().isThrownBy(() -> databaseClient.update() //
367+
.table(IdOnly.class) //
368+
.using(new IdOnly()) //
369+
.then()).withMessageContaining("UPDATE contains no assignments");
370+
}
371+
372+
static class IdOnly {
373+
374+
@Id String id;
375+
}
355376
}

0 commit comments

Comments
 (0)