-
Notifications
You must be signed in to change notification settings - Fork 624
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
Instruct Kotlin serialization to always serialize a third party interface using a specific serializer #2060
Comments
Unfortunately, there isn't a straightforward way now to override serializer for third-party class globally. I wonder why Spring doesn't support open polymorphism though. |
Spring typical arrangement is to have both kotlinx.serialization and Jackson in the classpath, with kotlinx.serialization converter used first to evaluate if if should be used, if not, Jackson is used. So the behavior of Spring Support is to determine early a signal that shows that kotlinx.serialization should be used. In our unit tests, you can see what use case we support or not. That's not exactly the ask from this issue to support third party interface, by in spring-projects/spring-framework#28389 we have an issue about serialization of The ask from this issue could also be helpful in order to allow people to indicate : I know for sure how to serialize this interface with kotlinx.serialization, unless @sandwwraith has a better suggestion. |
@sandwwraith Any thoughts? |
I'm a bit confused because it seems we're discussing a bit different things. kotlinx.serialization indeed does not work well with polymorphism for |
Ok I will double check tomorrow that the use case described here works as expected (as you I would expected it does) and if confirmed I will comment asking closing this issue and will create a dedicated one for the need described in #2060 (comment) which is from my POV the real need for Spring use cases. |
Hello, @sdeleuze Reference comment: #151 (comment)
So it seems to be implemented as below (Reference): [Order of method calls] => A PolymorphicSerializer is returned due to the In order to solve the serialization of Please check the contents and review this spring-projects/spring-framework/pull/29761 again. 🙏 |
@sandwwraith I had a deeper look and I think I am not confortable adding custom logic in Spring to prefer a specific contextual serializer and tend to agree with users ask in #1870 and #2026. I mean, it is not just about Spring, Ktor had to implement custom logic like https://github.com/ktorio/ktor/pull/2865/files. In the discussion we had, you said
But that's kind of contradict with
And I agree with what the documentation says as it is a common use case. I understand you don't want to change the existing behavior as it would break use cases, but I am still confused with it. In #1870 (comment), you said:
If you are really not up to implement this proposal which sounds attractive to me, could we maybe discuss the introduction of such new function to resolve the need described in this issue "Instruct Kotlin serialization to always serialize a third party interface using a specific serializer"? |
Okay, I see — this problem indeed needs some design investigation from our side to determine whether a module-prioritizing function is enough for 'always serialize a third party interface using a specific serializer' or not |
hello. @sandwwraith could you please share? 🙏 |
We've discussed the matter and cam to a conclusion that |
Thank you for the explanation. is it okay if i understand the meaning of a next major release as version 1.5.0? |
No, because 1.5.0-RC is already released. It'll be 1.6.0 |
@sandwwraith Is it available in |
Now, we look up serializers for non-sealed interfaces in SerializersModule first, and only then unconditionally return PolymorphicSerializer. This is done because with old behavior, it was impossible to override interface serializer with context, as interfaces were treated as always having PolymorphicSerializer. This is a behavioral change, although impact should be minimal, as for most usecases (without modules), serializer will be the same and special workarounds are performed so cache would also work as expected. Note that KClass.serializer() function will no longer return PolymorphicSerializer for interfaces at all and return null instead. It is OK, because this function is marked with @InternalSerializationApi, and we can change its behavior. Intrinsics in plugin with changed functionality are expected to be merged in 2.0.0-RC. Fixes #2060
Now, we look up serializers for non-sealed interfaces in SerializersModule first, and only then unconditionally return PolymorphicSerializer. This is done because with old behavior, it was impossible to override interface serializer with context, as interfaces were treated as always having PolymorphicSerializer. This is a behavioral change, although impact should be minimal, as for most usecases (without modules), serializer will be the same and special workarounds are performed so cache would also work as expected. Note that KClass.serializer() function will no longer return PolymorphicSerializer for interfaces at all and return null instead. It is OK, because this function is marked with @InternalSerializationApi, and we can change its behavior. Intrinsics in plugin with changed functionality are expected to be merged in 2.0.0-RC. Fixes #2060
…rovided for interface. This prioritizes module contents over default polymorphic serializer. See Kotlin/kotlinx.serialization#2060 and Kotlin/kotlinx.serialization#2565
…rovided for interface. This prioritizes module contents over default polymorphic serializer. See Kotlin/kotlinx.serialization#2060 and Kotlin/kotlinx.serialization#2565
…rovided for interface. This prioritizes module contents over default polymorphic serializer. See Kotlin/kotlinx.serialization#2060 and Kotlin/kotlinx.serialization#2565
What is your use-case and why do you need this feature?
I need to serialize using kotlinx.serialization a third party interface
IApiError
returned by a Spring controller advice :Spring will execute later the following code that will fail because
SerializersKt.serializer(type)
will return a Polymorphic serializer and when the serializer is polymorphic Spring failsDescribe the solution you'd like
I would like to have a way to instruct Kotlinx.serialization that everytime the interface
IApiError
needs to be serialized it should use a specific serializer, in this case theKApiError
class serializer.For example, with Jackson it is possible to do the following :
I've tried declaring the serializer as polymorphic but Spring does not support Open polyphormism.
Also I've tried declaring a custom serializer:
Thanks for your help 🙏
The text was updated successfully, but these errors were encountered: