Skip to content

Commit 4f04c87

Browse files
committed
#421 - Suppress results for suspended query methods returning kotlin.Unit.
We now discard results for suspended query methods if the return type is kotlin.Unit. Related ticket: DATACMNS-1779 Original pull request: #422.
1 parent 0257136 commit 4f04c87

File tree

3 files changed

+79
-3
lines changed

3 files changed

+79
-3
lines changed

src/main/java/org/springframework/data/r2dbc/repository/query/AbstractR2dbcQuery.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import org.springframework.data.repository.query.RepositoryQuery;
3535
import org.springframework.data.repository.query.ResultProcessor;
3636
import org.springframework.data.repository.query.ReturnedType;
37+
import org.springframework.data.util.ReflectionUtils;
3738
import org.springframework.util.Assert;
3839

3940
/**
@@ -136,7 +137,7 @@ private R2dbcQueryExecution getExecutionToWrap(ReturnedType returnedType) {
136137
});
137138
}
138139

139-
if (Void.class.isAssignableFrom(returnedType.getReturnedType())) {
140+
if (ReflectionUtils.isVoid(returnedType.getReturnedType())) {
140141
return (q, t, c) -> q.rowsUpdated().then();
141142
}
142143

@@ -152,7 +153,7 @@ private R2dbcQueryExecution getExecutionToWrap(ReturnedType returnedType) {
152153

153154
/**
154155
* Returns whether this query is a modifying one.
155-
*
156+
*
156157
* @return
157158
* @since 1.1
158159
*/

src/main/java/org/springframework/data/r2dbc/repository/query/R2dbcQueryExecution.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.springframework.data.relational.repository.query.DtoInstantiatingConverter;
3131
import org.springframework.data.repository.query.ResultProcessor;
3232
import org.springframework.data.repository.query.ReturnedType;
33+
import org.springframework.data.util.ReflectionUtils;
3334
import org.springframework.util.ClassUtils;
3435

3536
/**
@@ -97,7 +98,7 @@ public Object convert(Object source) {
9798
return source;
9899
}
99100

100-
if (Void.class == returnedType.getReturnedType()) {
101+
if (ReflectionUtils.isVoid(returnedType.getReturnedType())) {
101102

102103
if (source instanceof Mono) {
103104
return ((Mono<?>) source).then();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Copyright 2020 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.r2dbc.repository
17+
18+
import io.r2dbc.spi.test.MockResult
19+
import kotlinx.coroutines.runBlocking
20+
import org.junit.jupiter.api.BeforeEach
21+
import org.junit.jupiter.api.Test
22+
import org.springframework.data.annotation.Id
23+
import org.springframework.data.r2dbc.core.DatabaseClient
24+
import org.springframework.data.r2dbc.core.DefaultReactiveDataAccessStrategy
25+
import org.springframework.data.r2dbc.core.R2dbcEntityTemplate
26+
import org.springframework.data.r2dbc.dialect.PostgresDialect
27+
import org.springframework.data.r2dbc.repository.support.R2dbcRepositoryFactory
28+
import org.springframework.data.r2dbc.testing.StatementRecorder
29+
import org.springframework.data.repository.kotlin.CoroutineCrudRepository
30+
31+
/**
32+
* Unit tests for [CoroutineCrudRepository].
33+
*
34+
* @author Mark Paluch
35+
*/
36+
class CoroutineRepositoryUnitTests {
37+
38+
lateinit var client: DatabaseClient
39+
lateinit var entityTemplate: R2dbcEntityTemplate
40+
lateinit var recorder: StatementRecorder
41+
lateinit var repositoryFactory: R2dbcRepositoryFactory
42+
43+
@BeforeEach
44+
fun before() {
45+
recorder = StatementRecorder.newInstance()
46+
client = DatabaseClient.builder().connectionFactory(recorder)
47+
.dataAccessStrategy(DefaultReactiveDataAccessStrategy(PostgresDialect.INSTANCE)).build()
48+
entityTemplate = R2dbcEntityTemplate(client)
49+
repositoryFactory = R2dbcRepositoryFactory(entityTemplate)
50+
}
51+
52+
@Test
53+
fun shouldIssueDeleteQuery() {
54+
55+
val result = MockResult.builder().rowsUpdated(2).build()
56+
recorder.addStubbing({ s: String -> s.startsWith("DELETE") }, result)
57+
58+
val repository = repositoryFactory.getRepository(PersonRepository::class.java)
59+
60+
runBlocking {
61+
repository.deleteUserAssociation(2)
62+
}
63+
}
64+
65+
interface PersonRepository : CoroutineCrudRepository<Person, Long> {
66+
67+
@Modifying
68+
@Query("DELETE FROM person WHERE id = :id ")
69+
suspend fun deleteUserAssociation(userId: Int)
70+
}
71+
72+
73+
data class Person(@Id var id: Long, var name: String)
74+
}

0 commit comments

Comments
 (0)