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
Using deserialized instance's delegation throws NullPointerException #241
Comments
It is a bug (and current limitation) of compiler plugin: it does not initialize synthetic fields which store delegates, so access to delegated methods leads to access to uninitialized fields and NPE. So right now it's impossible to use interface delegation in serializable classes. |
Delegation does seem to work fine for me, if the Object is instantiated by default in the constructor:
|
@IARI The issue is that for delegation Kotlin actually will use a synthetic field that is initialised to the value of the base constructor parameter. It uses a different field for the base property (so in this case there are two fields holding the same value). If you pass a different version of BaseClass rather than the default the delegated property will not be serialized, only the visible copy. The correct thing would be for the serialization plugin to just reject serialization on delegates. I don't believe that it can be done correctly (as deserialization of two copies will lead to separate copies after deserialization where originally the two copies were references to the same object). |
I do not know what you mean exactly, but I haven't tested all possible use cases here. I am aware of that this is not a clean solution, but just a workaround. I do not advocate the outright rejection of serialization of delegations, since this is right now the only way to serialize "inherited fields" since that is broken (of course they are not inherited, but the behavior is achieved with composition by delegation - see #378) |
I intuitively expected a similar behavior as suggested by @pdvrieze:
In fact, I ended up using class delegation exactly because I didn't want the delegated implementation to be serialized; I wanted it to be constructed as part of object initialization. The use case is constructing command objects for application services. Take a fictional
This strongly ties the command object implementation to the application service definition ( Therefore, I also ran into the issue that this I simply wanted to point out this example use case so it could be considered prior to ruling out generating serializers for classes which use class delegation entirely. Perhaps we should be able to apply |
Starting from Kotlin 1.3.60, this now seems to throw a |
This is an accessible workaround for me, Thanks |
…ialized in a function This fixes the following issues: (not from YouTrack because they are kotlinx.serialization related) Kotlin/kotlinx.serialization#1039 Kotlin/kotlinx.serialization#241
…ialized in a function (JetBrains#4727) Fixes Kotlin/kotlinx.serialization#241
The following code throws NullPointerException. Is it expected behavior?
The text was updated successfully, but these errors were encountered: