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

Allow null for query parameters in Kotlin panache #45609

Merged
merged 1 commit into from
Jan 15, 2025

Conversation

LarsSven
Copy link
Contributor

@LarsSven LarsSven commented Jan 15, 2025

Quarkus Panache has Kotlin specific extensions that should be used for Kotlin projects when working with Hibernate and Panache. We recently wanted to migrate our Kotlin project from using the Java panache extensions to the Kotlin panache extensions, however it turned out that they behaved differently.

The big difference is that since Kotlin is non-nullable by default, function signatures such as vararg params: Any in Kotlin are different from A java version like vararg params: Object, since an Object is nullable but Any is not. This is a problem, because the query can absolutely take nullable parameters.

For example, the following code would work with the Java version, but not the Kotlin version, since all 3 parameters are possibly null within this context:

find("""
    (cast(?1 AS uuid) IS null OR assignment.courseInstanceId = ?1) AND
    (cast(?2 AS uuid) IS null OR groupId                     = ?2) AND 
    (cast(?3 AS uuid) IS null OR assignment.id               = ?3)
""", courseInstanceId, groupId, assignmentId)

It is also unreasonable to expect the user to deal with nullability outside of the query, as checking for nullability outside of this query makes the query extremely ugly as you'd need to dynamically construct the query based on whether a parameter is null or not.

This PR converts the Any parameter type to Any?, to allow for nullable parameters when using the Kotlin extensions (which is absolutely valid for queries), and to bring the Kotlin version in line with the Java version. These changes are made for both the Kotlin Panache for Hibernate ORM extension as well as the Kotlin Panache for Hibernate Reactive extension.

Copy link

quarkus-bot bot commented Jan 15, 2025

Status for workflow Quarkus CI

This is the status report for running Quarkus CI on commit dbdc588.

✅ The latest workflow run for the pull request has completed successfully.

It should be safe to merge provided you have a look at the other checks in the summary.

You can consult the Develocity build scans.

@geoand
Copy link
Contributor

geoand commented Jan 15, 2025

Thanks for this!

Sounds reasonable to me, do you agree @yrodiere ?

@yrodiere
Copy link
Member

Sounds reasonable to me, do you agree @yrodiere ?

Agreed, this makes sense.

FWIW we've started using @Nullable annotations in Hibernate ORM. It's not for all packages, and I don't know if we did it for the relevant packages (org.hibernate.query I guess?), but if we did, that can help us determine nullability on the Kotlin side.

@geoand geoand merged commit 3f0743c into quarkusio:main Jan 15, 2025
21 checks passed
@quarkus-bot quarkus-bot bot added this to the 3.19 - main milestone Jan 15, 2025
@gsmet gsmet modified the milestones: 3.19 - main, 3.18.0 Jan 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants