Browse Source

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
4.3.x
Mark Paluch 1 year ago
parent
commit
fe256944a1
No known key found for this signature in database
GPG Key ID: 55BC6374BAA9D973
  1. 3
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/query/MongoQueryMethod.java
  2. 2
      spring-data-mongodb/src/test/kotlin/org/springframework/data/mongodb/repository/KotlinRepositoryUnitTests.kt
  3. 14
      spring-data-mongodb/src/test/kotlin/org/springframework/data/mongodb/repository/query/ReactiveMongoQueryMethodCoroutineUnitTests.kt

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

@ -46,6 +46,7 @@ import org.springframework.data.repository.query.ParametersSource; @@ -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 { @@ -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;
}

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

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

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

@ -17,9 +17,11 @@ package org.springframework.data.mongodb.repository.query @@ -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 { @@ -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 { @@ -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() }
}
}

Loading…
Cancel
Save