chRyNaN / serialization-parcelable

Android Parcelable support for the Kotlinx Serialization library.
Apache License 2.0
70 stars 3 forks source link

Parcelize serializable data is broken #5

Closed MichalKlusak closed 1 year ago

MichalKlusak commented 1 year ago

Describe the bug When I'm trying to transfer Serializable data through Bundle, I use methods putParcelable and getParcelable to parcelize it but the result data is broken.

To Reproduce Params:

@Serializable
data class Params(val intro: Boolean)

1)

val params = Params(true)
val bundle = Bundle()
bundle.putParcelable("key", params)
bundle.getParcelable("key")

Result behavior Params(false)

Expected behavior Params(true)

2)

val params = Params(false)
val bundle = Bundle()
bundle.putParcelable("key", params)
bundle.getParcelable("key")

Result behavior null

Expected behavior Params(false)

Additional context Serialization-parcelable - 0.5.1 (version 0.5.0 works fine) Kotlin - 1.7.20 Kotlin-serialization - 1.4.1

github-actions[bot] commented 1 year ago

Welcome and thanks for contributing! Make sure to leave a detailed explanation of the issue.

chRyNaN commented 1 year ago

I was able to reproduce the issue. I'm not positive about the cause, but it seemed to be an incorrect function call. It was resolved after the changes made in the following commit where I removed the AndroidParceler class: https://github.com/chRyNaN/serialization-parcelable/commit/92fd90cc5f6aa9365c1e306592f8b79c7dde11ac. I will try and write some tests to make sure that this issue doesn't occur again. The fix will be released in the next version.

chRyNaN commented 1 year ago

The issue has to do with the Parcel(ByteArray) constructor function implementation for the Android target. The decodeFromParcel function was updated previously to get the parcel.toByteArray() from the parcel parameter and create a new Parcel using that constructor function. Then that Parcel was decoded with a ParcelDecoder. This was added to help some issue with the ByteArrayParcel implementation for other platforms, but it seems that it broke the Android usage. I will investigate further and ensure that both implementations work.

chRyNaN commented 1 year ago

The issue had to do with the Android implementation of the Parcel(ByteArray) calling setDataPosition(0) and the above mentioned use of creating a new Parcel from the provided parcel in the Parcelable.decodeFromParcel function. The Parcelable.encodeToBundle/Parcelable.decodeFromBundle have to internally manage a size and magic numbers set inside the android.os.Parcel class. Because of this, the size should have been set to 8 (two four byte int values were set). Instead of hardcoding this, since it is TBD which component should be responsible for setting that, I think removing the wrapper creation of a new parcel in the decodeFromParcel function is the solution and seems to work. However, we should be on the lookout for issues on other Kotlin targets.

chRyNaN commented 1 year ago

Resolved with release 0.6.0 (10): https://github.com/chRyNaN/serialization-parcelable/releases/tag/0.6.0