Skip to content

Commit 7a094b6

Browse files
committed
#395 - Add support for suspend repository query methods returning List<T>.
1 parent d382394 commit 7a094b6

File tree

3 files changed

+38
-5
lines changed

3 files changed

+38
-5
lines changed

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,8 @@ public R2dbcQueryMethod(Method method, RepositoryMetadata metadata, ProjectionFa
116116
this.query = Optional.ofNullable(
117117
AnnotatedElementUtils.findMergedAnnotation(method, Query.class));
118118
this.modifying = AnnotatedElementUtils.hasAnnotation(method, Modifying.class);
119-
this.isCollectionQuery = Lazy.of(() -> !(isPageQuery() || isSliceQuery())
120-
&& ReactiveWrappers.isMultiValueType(metadata.getReturnType(method).getType()));
119+
this.isCollectionQuery = Lazy.of(() -> (!(isPageQuery() || isSliceQuery())
120+
&& ReactiveWrappers.isMultiValueType(metadata.getReturnType(method).getType())) || super.isCollectionQuery());
121121
}
122122

123123
/* (non-Javadoc)

src/test/kotlin/org/springframework/data/r2dbc/repository/CoroutineRepositoryUnitTests.kt

+23-1
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,12 @@
1515
*/
1616
package org.springframework.data.r2dbc.repository
1717

18+
import io.r2dbc.spi.test.MockColumnMetadata
1819
import io.r2dbc.spi.test.MockResult
20+
import io.r2dbc.spi.test.MockRow
21+
import io.r2dbc.spi.test.MockRowMetadata
1922
import kotlinx.coroutines.runBlocking
23+
import org.assertj.core.api.Assertions.assertThat
2024
import org.junit.jupiter.api.BeforeEach
2125
import org.junit.jupiter.api.Test
2226
import org.springframework.data.annotation.Id
@@ -62,13 +66,31 @@ class CoroutineRepositoryUnitTests {
6266
}
6367
}
6468

69+
@Test // gh-395
70+
fun shouldIssueSelectQuery() {
71+
72+
val rowMetadata = MockRowMetadata.builder().columnMetadata(MockColumnMetadata.builder().name("id").build()).columnMetadata(MockColumnMetadata.builder().name("name").build()).build()
73+
val row1 = MockRow.builder().identified("id", Object::class.java, 1L).identified("name", Object::class.java, "Walter").build()
74+
val row2 = MockRow.builder().identified("id", Object::class.java, 2L).identified("name", Object::class.java, "White").build()
75+
76+
val result = MockResult.builder().rowMetadata(rowMetadata).row(row1).row(row2).build()
77+
recorder.addStubbing({ s: String -> s.startsWith("SELECT") }, result)
78+
79+
val repository = repositoryFactory.getRepository(PersonRepository::class.java)
80+
81+
runBlocking {
82+
assertThat(repository.findAllByName("Walt")).hasSize(2)
83+
}
84+
}
85+
6586
interface PersonRepository : CoroutineCrudRepository<Person, Long> {
6687

6788
@Modifying
6889
@Query("DELETE FROM person WHERE id = :id ")
6990
suspend fun deleteUserAssociation(userId: Int)
70-
}
7191

92+
suspend fun findAllByName(name: String): List<Person>
93+
}
7294

7395
data class Person(@Id var id: Long, var name: String)
7496
}

src/test/kotlin/org/springframework/data/r2dbc/repository/query/ReactiveR2dbcQueryMethodCoroutineUnitTests.kt

+13-2
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ class ReactiveR2dbcQueryMethodCoroutineUnitTests {
4040
suspend fun findSuspendAllById(): Flow<Person>
4141

4242
fun findAllById(): Flow<Person>
43+
44+
suspend fun findSuspendedAllById(): List<Person>
4345
}
4446

4547
@Test // gh-384
@@ -48,7 +50,7 @@ class ReactiveR2dbcQueryMethodCoroutineUnitTests {
4850
val method = PersonRepository::class.java.getMethod("findAllById")
4951
val queryMethod = R2dbcQueryMethod(method, DefaultRepositoryMetadata(PersonRepository::class.java), projectionFactory, R2dbcMappingContext())
5052

51-
assertThat(queryMethod.isCollectionQuery).isTrue()
53+
assertThat(queryMethod.isCollectionQuery).isTrue
5254
}
5355

5456
@Test // gh-384
@@ -57,6 +59,15 @@ class ReactiveR2dbcQueryMethodCoroutineUnitTests {
5759
val method = PersonRepository::class.java.getMethod("findSuspendAllById", Continuation::class.java)
5860
val queryMethod = R2dbcQueryMethod(method, DefaultRepositoryMetadata(PersonRepository::class.java), projectionFactory, R2dbcMappingContext())
5961

60-
assertThat(queryMethod.isCollectionQuery).isTrue()
62+
assertThat(queryMethod.isCollectionQuery).isTrue
63+
}
64+
65+
@Test // gh-395
66+
internal fun `should consider suspended methods returning List as collection queries`() {
67+
68+
val method = PersonRepository::class.java.getMethod("findSuspendedAllById", Continuation::class.java)
69+
val queryMethod = R2dbcQueryMethod(method, DefaultRepositoryMetadata(PersonRepository::class.java), projectionFactory, R2dbcMappingContext())
70+
71+
assertThat(queryMethod.isCollectionQuery).isTrue
6172
}
6273
}

0 commit comments

Comments
 (0)