ZacSweers / MoshiX

Extensions for Moshi including IR plugins, moshi-sealed, and more.
Apache License 2.0
492 stars 37 forks source link

Enum support for sealed interfaces #536

Closed georgi-mirchev closed 1 month ago

georgi-mirchev commented 6 months ago

We cannot get Moshi sealed to work correctly with enums that implement a sealed interface. We have the following example:

@JsonClass(generateAdapter = true, generator = "sealed:outcome")
sealed interface Outcome {

    @TypeLabel("Complete")
    enum class Complete : Outcome {
        AUTHORISED,
        DECLINED,
    }

 @TypeLabel("Error")
    enum class Error : Outcome {
        USER_FAILURE,
        SYSTEM_FAILURE,
    }

Does it support such cases?

ZacSweers commented 6 months ago

Enums as sealed subtypes isn't a case I'd considered. PR is welcome though!

ZacSweers commented 6 months ago

I don't think it would work the way you've described in your sample though. They'd be more akin to a nested sealed subtype and would work functionally the same as this

sealed class Error : Outcome {
  @TypeLabel(...)
  object USER_FAILURE : Error()
  @TypeLabel(...)
  object SYSTEM_FAILURE : Error()
}
ZacSweers commented 1 month ago

I'm going to close this as wontfix, the example you've put forward doesn't actually make much practical sense as there's no way to decide which enum member to use in your example. Feel free to revisit if you have an idea.