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

Jdbc CoroutineCrudRepository does not handle nullable return types #1297

Closed
marcmarcet opened this issue Jan 25, 2022 · 1 comment · Fixed by #1302
Closed

Jdbc CoroutineCrudRepository does not handle nullable return types #1297

marcmarcet opened this issue Jan 25, 2022 · 1 comment · Fixed by #1302
Labels
type: bug Something isn't working
Milestone

Comments

@marcmarcet
Copy link

marcmarcet commented Jan 25, 2022

Expected Behavior

Given the following repository, I'd expect that if no record is found, it returns null

@JdbcRepository(dialect = Dialect.MYSQL)
interface UserRepository : CoroutineCrudRepository<User, Long>, {
    suspend fun findByEmail(email: String): User?
}

Actual Behaviour

What I actually get is a EmptyResultException

io.micronaut.data.exceptions.EmptyResultException: Query produced no result
	at io.micronaut.data.runtime.operations.ExecutorAsyncOperations.lambda$findOne$2(ExecutorAsyncOperations.java:89)
	at java.base/java.util.concurrent.CompletableFuture$AsyncSupply.run$$$capture(CompletableFuture.java:1768)
	at java.base/java.util.concurrent.CompletableFuture$AsyncSupply.run(CompletableFuture.java)
	at io.micronaut.scheduling.instrument.InvocationInstrumenterWrappedRunnable.run(InvocationInstrumenterWrappedRunnable.java:47)
	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136)
	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
	at java.base/java.lang.Thread.run(Thread.java:833)

Steps To Reproduce

Any similar CoroutineCrudRepository should be able to reproduce the issue

Environment Information

macOS Monterrey
Kotlin 1.6.10
JDK 17

Example Application

No response

Version

3.2.7

@marcmarcet marcmarcet changed the title CoroutineCrudRepository does not handle nullable return types Jdbc CoroutineCrudRepository does not handle nullable return types Jan 25, 2022
@marcmarcet
Copy link
Author

I'm currently using the following work around, which seems to be good enough as a temporary solution.

@JdbcRepository(dialect = Dialect.MYSQL)
interface UserRepository : CoroutineCrudRepository<User, Long> {
    suspend fun findByEmail(email: String): User
}

suspend fun UserRepository.findByEmailAsNullable(email: String): User? {
    return try {
        this.findByEmail(email)
    } catch (_: EmptyResultException) {
        null
    }
}

@dstepanov dstepanov added the type: bug Something isn't working label Mar 21, 2022
@dstepanov dstepanov added this to the 3.3.0 milestone Mar 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants