-
Notifications
You must be signed in to change notification settings - Fork 37.7k
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
Make BodyBuilder.bodyValueAndAwait() a reified type of function #32652
Make BodyBuilder.bodyValueAndAwait() a reified type of function #32652
Conversation
11087b8
to
b64980b
Compare
b64980b
to
e8bd038
Compare
e8bd038
to
c9c29c6
Compare
message = "Use `bodyValueAndAwait<T>()` instead, which is not subject to type erasure.", | ||
level = DeprecationLevel.WARNING | ||
) | ||
@JvmName("bodyValueAndAwait0") // to avoid platform declaration clash |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you provide more details on the issue you see if you remove this @JvmName("bodyValueAndAwait0")
annotation?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you remove this annotation then the compilation will fail with error:
Platform declaration clash: The following declarations have the same JVM signature (bodyValueAndAwait(Lorg/springframework/web/reactive/function/server/ServerResponse$BodyBuilder;Ljava/lang/Object;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;):
suspend inline fun <reified T : Any> ServerResponse.BodyBuilder.bodyValueAndAwait(body: T, `$completion`: Continuation<ServerResponse>): Any? defined in org.springframework.web.reactive.function.server
suspend fun ServerResponse.BodyBuilder.bodyValueAndAwait(body: Any, `$completion`: Continuation<ServerResponse>): Any? defined in org.springframework.web.reactive.function.server
When the bytecode is generated the generics are erased so the compiler just sees two functions with the same type, because in the end T
is replaced with Any
.
c9c29c6
to
6c5442d
Compare
Function `bodyValueAndAwait(body: Any)` is not a reified function and is subject to type erasure. If an application uses for serialization the `kotlinx.serialization` library and the provided body uses generics, then the library is unable to serialize the body and spring falls back to default one. This could lead to unexpected behaviors. Signed-off-by: George Papadopoulos <george.719pap@gmail.com>
6c5442d
to
b7f69af
Compare
Sorry for force-push while in-review. Minor changes in KDocs and error message. |
After a deeper look, I am going to implement this feature differently on both Java and Kotlin side via #32713 which supersedes this issue. |
Summary
ServerResponse.BodyBuilder.bodyValueAndAwait
API is broken when it is used with an application ituses for serialization the
kotlinx.serialization
library and the provided body uses generics.The library is unable to serialize the body since it is a type of
Any
and spring falls backto default one. This could lead to unexpected responses.
For example:
A minimum reproducible example can be found here.
Proposal
Make BodyBuilder.bodyValueAndAwait() a reified type of function. We can also migrate the API without
breaking existing functionality.
Alternatives
Only document the behavior and accept it as the intended behavior.