Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support @Update annotation on unit-returning suspend functions #4772

Closed
breader124 opened this issue Aug 27, 2024 · 1 comment
Closed

Support @Update annotation on unit-returning suspend functions #4772

breader124 opened this issue Aug 27, 2024 · 1 comment
Assignees
Labels
type: enhancement A general enhancement

Comments

@breader124
Copy link

breader124 commented Aug 27, 2024

When I try to annotate the unit-returning suspend function with @Update annotation

@Repository
interface PersonRepository : CoroutineCrudRepository<Person, String> {
    @Update("{ \$inc: { age: 1 } }")
    suspend fun findAndIncrementAgeByName(name: String)
}

I get an exception

Caused by: java.lang.IllegalStateException: Update method may be void or return a numeric value (the number of updated documents). Offending Method: PersonRepository.findAndIncrementAgeByName
	at org.springframework.data.mongodb.repository.query.MongoQueryMethod.verify(MongoQueryMethod.java:497) ~[spring-data-mongodb-4.3.3.jar:4.3.3]
	at org.springframework.data.mongodb.repository.query.ReactiveMongoQueryMethod.verify(ReactiveMongoQueryMethod.java:164) ~[spring-data-mongodb-4.3.3.jar:4.3.3]
	at org.springframework.data.mongodb.repository.support.ReactiveMongoRepositoryFactory$MongoQueryLookupStrategy.resolveQuery(ReactiveMongoRepositoryFactory.java:184) ~[spring-data-mongodb-4.3.3.jar:4.3.3]
	at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.lookupQuery(QueryExecutorMethodInterceptor.java:115) ~[spring-data-commons-3.3.3.jar:3.3.3]
	... 42 common frames omitted

It seems to be related to the behavior of Kotlin's compiler. After the code compilation suspending functions return java.lang.Object and thus the function is not treated as a void-returning one on the Spring Data MongoDB level. When I pause the program execution with the debugger, it shows that the resultType is class kotlin.Unit. When I delete the suspend keyword from the findAndIncrementAgeByName method, it just shows void as a resultType.

I've prepared a minimal example to make it easy for you to reproduce this issue quickly. You can find the project here: https://github.com/breader124/spring-data-mongodb-issues-kotlin/tree/suspend-fun-annotated-with-update

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Aug 27, 2024
@mp911de mp911de added type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged labels Aug 28, 2024
@mp911de mp911de changed the title Support @Update annotation on unit-returning suspend functions Support @Update annotation on unit-returning suspend functions Aug 28, 2024
@mp911de
Copy link
Member

mp911de commented Aug 28, 2024

The validation fails because it uses a method that doesn't consider Unit being a surrogate for Void. Switching to Spring Data's ReflectionUtils.isVoid(…) should fix the issue.

@mp911de mp911de self-assigned this Aug 28, 2024
mp911de added a commit that referenced this issue Aug 28, 2024
…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
mp911de added a commit that referenced this issue Aug 28, 2024
…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
@mp911de mp911de added this to the 4.2.10 (2023.1.10) milestone Aug 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

3 participants