Skip to content

Commit fe25694

Browse files
committed
Use ReflectionUtils.isVoid(…) to whether Kotlin coroutines should return a value.
We now use a different utility method that is aware of whether a return type maps to Kotlin's Unit to indicate a void return type. Previously, we only checked for Java's void types. Closes #4772
1 parent 9786098 commit fe25694

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/query/MongoQueryMethod.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import org.springframework.data.repository.query.QueryMethod;
4747
import org.springframework.data.util.Lazy;
4848
import org.springframework.data.util.ReactiveWrappers;
49+
import org.springframework.data.util.ReflectionUtils;
4950
import org.springframework.data.util.TypeInformation;
5051
import org.springframework.lang.Nullable;
5152
import org.springframework.util.Assert;
@@ -533,7 +534,7 @@ private boolean isNumericOrVoidReturnValue() {
533534
}
534535

535536
boolean isUpdateCountReturnType = ClassUtils.isAssignable(Number.class, resultType);
536-
boolean isVoidReturnType = ClassUtils.isAssignable(Void.class, resultType);
537+
boolean isVoidReturnType = ReflectionUtils.isVoid(resultType);
537538

538539
return isUpdateCountReturnType || isVoidReturnType;
539540
}

spring-data-mongodb/src/test/kotlin/org/springframework/data/mongodb/repository/KotlinRepositoryUnitTests.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class KotlinRepositoryUnitTests {
4545
}
4646

4747
@Test // DATAMONGO-2601
48-
fun should() {
48+
fun shouldSupportDeleteMethods() {
4949

5050
val repository = repositoryFactory.getRepository(PersonRepository::class.java)
5151

spring-data-mongodb/src/test/kotlin/org/springframework/data/mongodb/repository/query/ReactiveMongoQueryMethodCoroutineUnitTests.kt

+14
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@ package org.springframework.data.mongodb.repository.query
1717

1818
import kotlinx.coroutines.flow.Flow
1919
import org.assertj.core.api.Assertions.assertThat
20+
import org.assertj.core.api.Assertions.assertThatNoException
2021
import org.junit.jupiter.api.Test
2122
import org.springframework.data.mongodb.core.mapping.MongoMappingContext
2223
import org.springframework.data.mongodb.repository.Person
24+
import org.springframework.data.mongodb.repository.Update
2325
import org.springframework.data.projection.SpelAwareProxyProjectionFactory
2426
import org.springframework.data.repository.core.support.DefaultRepositoryMetadata
2527
import org.springframework.data.repository.kotlin.CoroutineCrudRepository
@@ -41,6 +43,9 @@ class ReactiveMongoQueryMethodCoroutineUnitTests {
4143
fun findAllByName(): Flow<Person>
4244

4345
suspend fun findSuspendByName(): List<Person>
46+
47+
@Update("{ \$inc: { age: 1 } }")
48+
suspend fun findAndIncrementAgeByName(name: String)
4449
}
4550

4651
@Test // DATAMONGO-2562
@@ -69,4 +74,13 @@ class ReactiveMongoQueryMethodCoroutineUnitTests {
6974

7075
assertThat(queryMethod.isCollectionQuery).isTrue()
7176
}
77+
78+
@Test // GH-4772
79+
internal fun `should consider suspended update queries`() {
80+
81+
val method = PersonRepository::class.java.getMethod("findAndIncrementAgeByName", String::class.java, Continuation::class.java)
82+
val queryMethod = ReactiveMongoQueryMethod(method, DefaultRepositoryMetadata(PersonRepository::class.java), projectionFactory, MongoMappingContext())
83+
84+
assertThatNoException().isThrownBy { queryMethod.verify() }
85+
}
7286
}

0 commit comments

Comments
 (0)