#395 - Add support for suspend repository query methods returning List<T>.
This commit is contained in:
@@ -116,8 +116,8 @@ public class R2dbcQueryMethod extends QueryMethod {
|
||||
this.query = Optional.ofNullable(
|
||||
AnnotatedElementUtils.findMergedAnnotation(method, Query.class));
|
||||
this.modifying = AnnotatedElementUtils.hasAnnotation(method, Modifying.class);
|
||||
this.isCollectionQuery = Lazy.of(() -> !(isPageQuery() || isSliceQuery())
|
||||
&& ReactiveWrappers.isMultiValueType(metadata.getReturnType(method).getType()));
|
||||
this.isCollectionQuery = Lazy.of(() -> (!(isPageQuery() || isSliceQuery())
|
||||
&& ReactiveWrappers.isMultiValueType(metadata.getReturnType(method).getType())) || super.isCollectionQuery());
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
|
||||
@@ -15,8 +15,12 @@
|
||||
*/
|
||||
package org.springframework.data.r2dbc.repository
|
||||
|
||||
import io.r2dbc.spi.test.MockColumnMetadata
|
||||
import io.r2dbc.spi.test.MockResult
|
||||
import io.r2dbc.spi.test.MockRow
|
||||
import io.r2dbc.spi.test.MockRowMetadata
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import org.assertj.core.api.Assertions.assertThat
|
||||
import org.junit.jupiter.api.BeforeEach
|
||||
import org.junit.jupiter.api.Test
|
||||
import org.springframework.data.annotation.Id
|
||||
@@ -62,13 +66,31 @@ class CoroutineRepositoryUnitTests {
|
||||
}
|
||||
}
|
||||
|
||||
@Test // gh-395
|
||||
fun shouldIssueSelectQuery() {
|
||||
|
||||
val rowMetadata = MockRowMetadata.builder().columnMetadata(MockColumnMetadata.builder().name("id").build()).columnMetadata(MockColumnMetadata.builder().name("name").build()).build()
|
||||
val row1 = MockRow.builder().identified("id", Object::class.java, 1L).identified("name", Object::class.java, "Walter").build()
|
||||
val row2 = MockRow.builder().identified("id", Object::class.java, 2L).identified("name", Object::class.java, "White").build()
|
||||
|
||||
val result = MockResult.builder().rowMetadata(rowMetadata).row(row1).row(row2).build()
|
||||
recorder.addStubbing({ s: String -> s.startsWith("SELECT") }, result)
|
||||
|
||||
val repository = repositoryFactory.getRepository(PersonRepository::class.java)
|
||||
|
||||
runBlocking {
|
||||
assertThat(repository.findAllByName("Walt")).hasSize(2)
|
||||
}
|
||||
}
|
||||
|
||||
interface PersonRepository : CoroutineCrudRepository<Person, Long> {
|
||||
|
||||
@Modifying
|
||||
@Query("DELETE FROM person WHERE id = :id ")
|
||||
suspend fun deleteUserAssociation(userId: Int)
|
||||
}
|
||||
|
||||
suspend fun findAllByName(name: String): List<Person>
|
||||
}
|
||||
|
||||
data class Person(@Id var id: Long, var name: String)
|
||||
}
|
||||
|
||||
@@ -40,6 +40,8 @@ class ReactiveR2dbcQueryMethodCoroutineUnitTests {
|
||||
suspend fun findSuspendAllById(): Flow<Person>
|
||||
|
||||
fun findAllById(): Flow<Person>
|
||||
|
||||
suspend fun findSuspendedAllById(): List<Person>
|
||||
}
|
||||
|
||||
@Test // gh-384
|
||||
@@ -48,7 +50,7 @@ class ReactiveR2dbcQueryMethodCoroutineUnitTests {
|
||||
val method = PersonRepository::class.java.getMethod("findAllById")
|
||||
val queryMethod = R2dbcQueryMethod(method, DefaultRepositoryMetadata(PersonRepository::class.java), projectionFactory, R2dbcMappingContext())
|
||||
|
||||
assertThat(queryMethod.isCollectionQuery).isTrue()
|
||||
assertThat(queryMethod.isCollectionQuery).isTrue
|
||||
}
|
||||
|
||||
@Test // gh-384
|
||||
@@ -57,6 +59,15 @@ class ReactiveR2dbcQueryMethodCoroutineUnitTests {
|
||||
val method = PersonRepository::class.java.getMethod("findSuspendAllById", Continuation::class.java)
|
||||
val queryMethod = R2dbcQueryMethod(method, DefaultRepositoryMetadata(PersonRepository::class.java), projectionFactory, R2dbcMappingContext())
|
||||
|
||||
assertThat(queryMethod.isCollectionQuery).isTrue()
|
||||
assertThat(queryMethod.isCollectionQuery).isTrue
|
||||
}
|
||||
|
||||
@Test // gh-395
|
||||
internal fun `should consider suspended methods returning List as collection queries`() {
|
||||
|
||||
val method = PersonRepository::class.java.getMethod("findSuspendedAllById", Continuation::class.java)
|
||||
val queryMethod = R2dbcQueryMethod(method, DefaultRepositoryMetadata(PersonRepository::class.java), projectionFactory, R2dbcMappingContext())
|
||||
|
||||
assertThat(queryMethod.isCollectionQuery).isTrue
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user