-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Need to add @Nullable
annotation for supporting kotlin 1.9 and above.
#3091
Comments
Probably this isn't the only method that would require retrofitting. Do you want to check all the other adapter methods as well? As workaround, have you tried using the |
I also considered using the To ensure long-term compatibility and alignment with Kotlin’s null-safety features, retrofitting If needed, I’m happy to assist in identifying the relevant methods. Thank you for considering this! |
Would you like to submit a pull request so that we can include the necessary changes for our service releases shipping on Friday? |
Sure! I'll submit the PR as soon as the work is done, either today or tomorrow. |
I apologize for the late progress on the task. While working on the adapter, I noticed many other classes (such as RedisKeyValueTemplate, GenericJackson2JsonRedisSerializer, GenericToStringSerializer, etc.) that also use generics in a similar way. As I started fixing them together, it felt like the scope of the work kept growing. Moreover, I found numerous instances where a method in a parent class (or interface) is annotated with @nullable, but the subclass or implementing class does not include that annotation. If we extend or implement this in Kotlin, it leads to compilation errors. As a result, I’m trying to decide whether to address everything at once, or just focus on the adapter that uses generics—mentioned as the main issue—and fix the rest later. I would like to hear your thoughts on this. |
Problem Description
Hello Spring Data Redis team,
I am using Spring Data Redis in a Kotlin 1.9-based project and have run into an issue when overriding the
delete()
method fromRedisKeyValueAdapter
. While the abstract class thatRedisKeyValueAdapter
implements (or extends) includes a@Nullable
annotation on this method, it appears that the actual implementation inRedisKeyValueAdapter
does not. As a result, Kotlin 1.9 does not recognize that the method can returnnull
, which leads to a compile-time error when trying to override it with a nullable return type.Background
In the following excerpt from
RedisKeyValueAdapter
, thedelete()
method may return null, as shown in the example logic. However, there is no@Nullable
annotation to indicate this possibility:spring-data-redis/src/main/java/org/springframework/data/redis/core/RedisKeyValueAdapter.java
Lines 311 to 346 in 0c90dea
Because there is no
@Nullable
annotation here, Kotlin sees T as non-null. However, the method may actually return null. When we attempt to override it in Kotlin, such as:Kotlin 1.9 raises an error along the lines of:
This stricter nullability check is documented in KT-36770, kotlin docs,where Kotlin 1.9 enforces a stronger rule about Java methods that appear to return non-null. Older Kotlin versions often allowed a nullable return type with just a warning, but 1.9 treats it as a compilation error.
Why
@Nullable
?@Nullable
directly on thedelete()
method in RedisKeyValueAdapter
would help Kotlin accurately recognize this possibility.@Nullable
were present, Kotlin would interpret the method as returningT?
, preventing the error when overriding.Example Change
If the delete() method were annotated like this:
Then Kotlin code such as:
I understand there might be other recommended workarounds or best practices, so please feel free to correct me if I am misunderstanding anything. Any advice on handling this situation would be greatly appreciated. My goal is simply to ensure that Spring Data Redis integrates smoothly with Kotlin 1.9 and higher.
Thank you very much for taking the time to review this issue, and for all your hard work on the Spring Data projects.
The text was updated successfully, but these errors were encountered: