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

Infer null-safety from type variables in Kotlin extensions when possible #22687

Closed
sdeleuze opened this issue Mar 26, 2019 · 1 comment
Closed
Assignees
Labels
type: enhancement A general enhancement
Milestone

Comments

@sdeleuze
Copy link
Contributor

sdeleuze commented Mar 26, 2019

As discussed in #22682 for specific JdbcOperations Kotlin extensions case, we could make null-safety more accurate by inferring null-safety from type variable in order to leverage the null-safety declared by the user instead of hardcoding the nullable nature of the return value, which forces for example Kotlin developers to use !! systematically and is not consistent with Java original signature.

PropertyResolver, JdbcOperations and RestOperations Kotlin extensions should be modified accordingly.

Based on my tests, it seems Java type inference is not clever enough to infer null-safety from type variable level @Nullable annotation so we will have to wait #20496 to apply that as well to Java methods.

@sdeleuze sdeleuze added this to the 5.2 M1 milestone Mar 26, 2019
@sdeleuze sdeleuze self-assigned this Mar 26, 2019
@sdeleuze sdeleuze changed the title Infer return type nullability in JdbcOperations Kotlin extensions Apply @Nullable on type variable when possible Mar 26, 2019
@sdeleuze sdeleuze added the type: enhancement A general enhancement label Mar 26, 2019
@sdeleuze sdeleuze changed the title Apply @Nullable on type variable when possible Infer null-safety from type variables in Kotlin extensions when possible Mar 26, 2019
sdeleuze added a commit to sdeleuze/spring-framework that referenced this issue Mar 28, 2019
@mplain
Copy link

mplain commented Mar 2, 2021

@sdeleuze hello!
In this commit you changed this:

inline fun <reified T: Any> RestOperations.getForObject(url: String, vararg uriVariables: Any): T? = 
   getForObject(url, T::class.java, *uriVariables)

to this:

inline fun <reified T> RestOperations.getForObject(url: String, vararg uriVariables: Any): T = 
   getForObject(url, T::class.java, *uriVariables) as T

As a result, when I use it like getForObject<String>(), if the response body is null, I get a ClassCastException (null cannot be cast to non-null type). Not even a NullPointerException, but a ClassCastException. Is this intended?
In http communication there's always a risk of getting a 200 response without a body. I'd rather check for nullability myself.
If I want to get a nullable result, I could write getForObject<String?>(), or call the java function directly (which is @nullable)
I'm just trying to understand what happened here, was it intended or not...

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

No branches or pull requests

2 participants