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

DeriveSchema macro - inconsistent behaviour for Enum N derivation between Scala 2 and 3 #747

Open
googley42 opened this issue Oct 3, 2024 · 0 comments · May be fixed by #749
Open

DeriveSchema macro - inconsistent behaviour for Enum N derivation between Scala 2 and 3 #747

googley42 opened this issue Oct 3, 2024 · 0 comments · May be fixed by #749

Comments

@googley42
Copy link
Contributor

googley42 commented Oct 3, 2024

There is inconsistent behaviour for EnumN derivation between Scala 2 and 3 when dealing with nested sealed trait hierarchies - also related to issue #668

In summary for the same Scala model:

when we compile in scala 2.13 we get only leaf nodes:

      implicit val schema: zio.schema.Schema.Enum3[
        model.Invoice.Unpaid,
        model.Invoice.Paid.Paid1,
        model.Invoice.Paid.Paid2,
        model.Invoice
      ] = DeriveSchema.gen[Invoice]

when we compile in scala 3.3.1 we get only direct subclasses - which I think is correct:

      implicit val schema: zio.schema.Schema.Enum2[
        model.Invoice.Unpaid,
        model.Invoice.Paid,
        model.Invoice
      ] = DeriveSchema.gen[Invoice]

Full reproducer below:

import zio.schema.Schema
import zio.schema.DeriveSchema

object DeriveEnumSchemaBug extends App {

  object model {

    sealed trait Invoice

    object Invoice {
      final case class Unpaid(id: String) extends Invoice

      object Unpaid {
        implicit val schema: Schema.CaseClass1[String, Unpaid] = DeriveSchema.gen[Unpaid]
      }

      sealed trait Paid extends Invoice

      object Paid {
        final case class Paid1(id: String) extends Paid

        object Paid1 {
          implicit val schema: Schema.CaseClass1[String, Paid1] = DeriveSchema.gen[Paid1]
        }

        final case class Paid2(id: String) extends Paid

        object Paid2 {
          implicit val schema: Schema.CaseClass1[String, Paid2] = DeriveSchema.gen[Paid2]
        }

        implicit val schema: Schema.Enum2[Paid1, Paid2, Paid] = DeriveSchema.gen[Paid]
      }

      /*
when we compile in scala 2.13 we get this:
      implicit val schema: zio.schema.Schema.Enum3[
        model.Invoice.Unpaid,
        model.Invoice.Paid.Paid1,
        model.Invoice.Paid.Paid2,
        model.Invoice
      ] = DeriveSchema.gen[Invoice]

when we compile in scala 3.3.1 we get this - which I think is correct:
      implicit val schema: zio.schema.Schema.Enum2[
        model.Invoice.Unpaid,
        model.Invoice.Paid,
        model.Invoice
      ] = DeriveSchema.gen[Invoice]

       */

      // compiled in scala 2.13
      implicit val schema: zio.schema.Schema.Enum3[
        model.Invoice.Unpaid,
        model.Invoice.Paid.Paid1, // the parent of this leaf node should be present, not the leaf node itself
        model.Invoice.Paid.Paid2, // the parent of this leaf node should be present, not the leaf node itself
        model.Invoice
      ] = DeriveSchema.gen[Invoice]

    }

  }
  println(s"schema ${model.Invoice.schema}")
}
@googley42 googley42 changed the title DeriveSchema macro - inconsistent behaviour for EnumN derivation between Scala 2 and 3 DeriveSchema macro - inconsistent behaviour for Enum N derivation between Scala 2 and 3 Oct 11, 2024
@googley42 googley42 linked a pull request Oct 12, 2024 that will close this issue
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

Successfully merging a pull request may close this issue.

1 participant