Skip to content

Commit

Permalink
Use ReflectionUtils.isVoid(…) to whether Kotlin coroutines should ret…
Browse files Browse the repository at this point in the history
…urn 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
  • Loading branch information
mp911de committed Aug 28, 2024
1 parent 612a15e commit 3ec515a
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
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;
Expand Down Expand Up @@ -532,7 +533,7 @@ private boolean isNumericOrVoidReturnValue() {
}

boolean isUpdateCountReturnType = ClassUtils.isAssignable(Number.class, resultType);
boolean isVoidReturnType = ClassUtils.isAssignable(Void.class, resultType);
boolean isVoidReturnType = ReflectionUtils.isVoid(resultType);

return isUpdateCountReturnType || isVoidReturnType;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class KotlinRepositoryUnitTests {
}

@Test // DATAMONGO-2601
fun should() {
fun shouldSupportDeleteMethods() {

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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() }
}
}

0 comments on commit 3ec515a

Please sign in to comment.