Skip to content
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

Sealed Classes Serialization Support #190

Closed
julianfalcionelli opened this issue May 31, 2021 · 4 comments
Closed

Sealed Classes Serialization Support #190

julianfalcionelli opened this issue May 31, 2021 · 4 comments

Comments

@julianfalcionelli
Copy link

Seems that Kotlinx serialization is failing when trying to persist Sealed Classes in Firebase Realtime Databases.

I have the following hierarchy classes:

@Serializable
sealed class Activity {
  abstract val type: Type
  
  @Serializable
  enum class Type(
    @Transient
    val shortName: StringResource
  ) {
    BJJ(
      MR.strings.activity_bjj,
    ),
    GRAPPLING(
      MR.strings.activity_grappling,
    )
  }
  
  @Serializable
  data class Bjj(
    override val type: Type = Type.BJJ,
    var belt: Belt? = null,
    val stripes: Int = 0
  ) : Activity() {
    @Serializable
    enum class Belt(
      @Transient
      val color: ColorResource.Single
    ) {
      WHITE(MR.colors.belt_white),
      BLUE(MR.colors.belt_blue),
      PURPLE(MR.colors.belt_purple),
      BROWN(MR.colors.belt_brown),
      BLACK(MR.colors.belt_black)
    }
  }
  
  @Serializable
  data class Grappling(
    override val type: Type = Type.GRAPPLING
  ) : Activity()
}

Then I try to persist a list of Activities:

val ref = database
  .child(ROOT_PATH)
  .child(user.id)
  .child("activities")
ref.setValue(ListSerializer(Activity.serializer()), updatedActivities)

And then this error happens:

 java.lang.ClassCastException: kotlinx.serialization.descriptors.PolymorphicKind$SEALED cannot be cast to kotlinx.serialization.descriptors.StructureKind
    at dev.gitlive.firebase._encodersKt.structureEncoder(_encoders.kt:12)
    ...
@nbransby
Copy link
Member

Thanks for the report, are you able to send a PR with a failing unit test for this?

@julianfalcionelli
Copy link
Author

Yes, I can do it these days 👍

@julianfalcionelli
Copy link
Author

I am not being able to run tests in a KMM project, I have to figure out what's going on. Tried with "Invalidate caches and restart", etc and the issue persists :(

Screen Shot 2021-06-01 at 23 14 08

@nbransby
Copy link
Member

nbransby commented Apr 6, 2022

closing as fixed

@nbransby nbransby closed this as completed Apr 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants