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

Reactive Mongo with Kotlin suspend functions #3886

Open
BarracudaX opened this issue Nov 16, 2021 · 2 comments
Open

Reactive Mongo with Kotlin suspend functions #3886

BarracudaX opened this issue Nov 16, 2021 · 2 comments
Labels
type: bug A general bug

Comments

@BarracudaX
Copy link

When writing ReactiveMongoRepository/ReactiveCrudRepository with spring-boot-starter-data-mongodb-reactive and declaring suspend find function with non-nullable kotlin return types, the repository returns null when it can't find an entity instead of EmptyResultDataAccessException.

Here is the fun declaration:
suspend fun findSomeEntitiesById(id: Long): SomeEntity

And this test prints null when called with id that does not exists:

@Test internal fun returnsNllReference() { runBlocking { println(repositoryMy.findSomeEntitiesById(123)) } }

If I replace spring-boot-starter-data-mongodb-reactive with spring-boot-starter-data-r2dbc, it throws

org.springframework.dao.EmptyResultDataAccessException: Result must not be null!

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Nov 16, 2021
@mp911de mp911de added type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged labels Nov 17, 2021
@mp911de mp911de self-assigned this Nov 17, 2021
@mp911de
Copy link
Member

mp911de commented Jan 25, 2022

The MethodInvocationValidator introspects the return value from coroutine invocations. In that case we see CoroutineSingletons.COROUTINE_SUSPENDED being returned and we cannot perform any further validations. I'm not sure how to proceed here. Maybe @sdeleuze can provide additional guidance on how to either intercept the continuation or how we can post-process the emitted/not-emitted value from the continuation.

@mp911de mp911de removed their assignment Jan 25, 2022
@sdeleuze
Copy link
Contributor

This kind of use case may require using Kotlin reflection (via kotlin-reflect) to get the null-safety information, that what we do on Framework side, see https://github.com/spring-projects/spring-framework/blob/main/spring-core/src/main/java/org/springframework/core/MethodParameter.java#L872.

See also related spring-projects/spring-framework#21546 (comment) issue/comment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug A general bug
Projects
None yet
Development

No branches or pull requests

4 participants