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
This commit is contained in:
Mark Paluch
2024-08-28 13:35:34 +02:00
parent 18af599b32
commit 28ec15b4e7
3 changed files with 17 additions and 2 deletions

View File

@@ -46,6 +46,7 @@ import org.springframework.data.repository.query.ParametersSource;
import org.springframework.data.repository.query.QueryMethod;
import org.springframework.data.util.Lazy;
import org.springframework.data.util.ReactiveWrappers;
import org.springframework.data.util.ReflectionUtils;
import org.springframework.data.util.TypeInformation;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
@@ -533,7 +534,7 @@ public class MongoQueryMethod extends QueryMethod {
}
boolean isUpdateCountReturnType = ClassUtils.isAssignable(Number.class, resultType);
boolean isVoidReturnType = ClassUtils.isAssignable(Void.class, resultType);
boolean isVoidReturnType = ReflectionUtils.isVoid(resultType);
return isUpdateCountReturnType || isVoidReturnType;
}

View File

@@ -45,7 +45,7 @@ class KotlinRepositoryUnitTests {
}
@Test // DATAMONGO-2601
fun should() {
fun shouldSupportDeleteMethods() {
val repository = repositoryFactory.getRepository(PersonRepository::class.java)

View File

@@ -17,9 +17,11 @@ package org.springframework.data.mongodb.repository.query
import kotlinx.coroutines.flow.Flow
import org.assertj.core.api.Assertions.assertThat
import org.assertj.core.api.Assertions.assertThatNoException
import org.junit.jupiter.api.Test
import org.springframework.data.mongodb.core.mapping.MongoMappingContext
import org.springframework.data.mongodb.repository.Person
import org.springframework.data.mongodb.repository.Update
import org.springframework.data.projection.SpelAwareProxyProjectionFactory
import org.springframework.data.repository.core.support.DefaultRepositoryMetadata
import org.springframework.data.repository.kotlin.CoroutineCrudRepository
@@ -41,6 +43,9 @@ class ReactiveMongoQueryMethodCoroutineUnitTests {
fun findAllByName(): Flow<Person>
suspend fun findSuspendByName(): List<Person>
@Update("{ \$inc: { age: 1 } }")
suspend fun findAndIncrementAgeByName(name: String)
}
@Test // DATAMONGO-2562
@@ -69,4 +74,13 @@ class ReactiveMongoQueryMethodCoroutineUnitTests {
assertThat(queryMethod.isCollectionQuery).isTrue()
}
@Test // GH-4772
internal fun `should consider suspended update queries`() {
val method = PersonRepository::class.java.getMethod("findAndIncrementAgeByName", String::class.java, Continuation::class.java)
val queryMethod = ReactiveMongoQueryMethod(method, DefaultRepositoryMetadata(PersonRepository::class.java), projectionFactory, MongoMappingContext())
assertThatNoException().isThrownBy { queryMethod.verify() }
}
}