Allow null for query parameters in Kotlin panache #45609
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 likevararg 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:
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 toAny?
, 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.