extension-kotlin
extension-kotlin copied to clipboard
Add `kotlin.serialization` implementation of `Serializer`
(Followup from #121 which had to be closed because I deleted my Github fork.)
This PR adds support for Kotlin serialization, see issue #13.
This PR includes #120 (this is required in order to use the stable version of kotlin.serialization).
In summary:
- Adds a class
KotlinSerializerwhich implements AxonSerializer. - Configurable
RevisionResolver,Converter, andJson, with the same defaults as the Jackson serializer implementation. - Because
Jsonis configurable, applications can define how JSON should be serialized based on business requirements. - Adds a DSL function
kotlinSerializerto create aKotlinSerializerwith configuration options. - Useful errors when the
.serializer()method cannot be found on a companion object of a to-serialize object. Class -> KSerializeris cached for performance and to avoid too much reflection.- Simple test to show that the implementation works.
Ref https://kotlinlang.org/docs/serialization.html with basic overview. Ref https://github.com/Kotlin/kotlinx.serialization/blob/master/docs/serialization-guide.md with serialization guide.
Kudos, SonarCloud Quality Gate passed!
0 Bugs
0 Vulnerabilities
0 Security Hotspots
0 Code Smells
No Coverage information
0.0% Duplication
Thank you for the great comments @smcvb. The discussion in https://github.com/AxonFramework/extension-kotlin/pull/124/files#r645048334 remains unresolved which will require more changes whatever solution is chosen.
No problem :)
I investigated the compilation of the @Serializable annotation in Axon Java source code. That does not look like it will work.
Then I found https://github.com/Kotlin/kotlinx.serialization/blob/f673bc2/docs/serializers.md#deriving-external-serializer-for-another-kotlin-class-experimental, which could mean that we create singleton objects marked as @Serializer(forClass = ...) that would auto-generate the needed serializers within the compilation process of this library. Unfortunately it doesn't work yet: the compilation crashes.
FYI, @hiddewie, I've adjusted the milestone to 0.3.0 instead of 0.2.0.
The intent for this is the desire to release 0.2.0 of the Kotlin extension. We felt we couldn't wait much longer until this release.
This, by the way, doesn't mean a 0.3.0 release couldn't be around the corner, which might contain this serializer. ;-)
Sorry for the long wait, I didn't forget the PR but it took some time until I could dive into it again!
I updated this branch to include all changes in master.
I updated the Kotlin serialization version to 1.3.2, but this makes no changes to everything I have observed so far.
In particular I experimented with a Surrogate serializer (https://github.com/AxonFramework/extension-kotlin/pull/124#discussion_r817801799). This works, but still requires a lot of maintenance work to 'wrap' each Axon internal class with a surrogate class in this library, while copying the public fields manually into the surrogate class for serialization. Performance-wise this is also not ideal.
https://github.com/AxonFramework/extension-kotlin/pull/124/files#diff-cc9e7866a7929cd55740daaa367352b89517dc56787fa6eb48f5c2fcb783b1bbR12-R31
I tested the automatically-generated serializer for external classes functionality again, but this still gives compiler errors:
@Serializer(forClass = ConfigToken::class)
object ConfigTokenSerializer
If I manually type the simple class like ConfigToken using Kotlin, it does work. So there might again be a Kotlin <-> Java interop problem here, causing errors in the Kotlin compiler.
Removing the milestone as this is still under discussion, while the release is intended to come soon. Will add this to the following release milestone for further discussions.
Marking this pull request as obsolete through the introduction of #338.