@@ -268,6 +268,11 @@ object DeriveSchemaSpec extends ZIOSpecDefault with VersionSpecificDeriveSchemaS
268268 sealed abstract class MiddleClass (override val x : Int , val y : Int ) extends AbstractBaseClass2 (x)
269269 final case class ConcreteClass3 (override val x : Int , override val y : Int , s : String ) extends MiddleClass (x, y)
270270
271+ sealed trait TraitWithMiddleTrait
272+ case object TraitLeaf extends TraitWithMiddleTrait
273+ sealed trait MiddleTrait extends TraitWithMiddleTrait
274+ case object MiddleTraitLeaf extends MiddleTrait
275+
271276 override def spec : Spec [Environment , Any ] = suite(" DeriveSchemaSpec" )(
272277 suite(" Derivation" )(
273278 test(" correctly derives case class 0" ) {
@@ -546,6 +551,16 @@ object DeriveSchemaSpec extends ZIOSpecDefault with VersionSpecificDeriveSchemaS
546551 )
547552 assert(derived)(hasSameSchema(expected))
548553 },
554+ test(
555+ " correctly derives schema for sealed trait with intermediate traits, having leaf classes for Scala2"
556+ ) {
557+ intermediateTraitTest(enum2Annotations = Chunk .empty)
558+ } @@ TestAspect .scala2Only,
559+ test(
560+ " correctly derives schema for sealed trait with intermediate traits, having leaf classes for Scala3"
561+ ) {
562+ intermediateTraitTest(enum2Annotations = Chunk (simpleEnum(automaticallyAdded = true )))
563+ } @@ TestAspect .scala3Only,
549564 test(
550565 " correctly derives schema for abstract sealed class with intermediate subclasses, having case class leaf classes"
551566 ) {
@@ -599,4 +614,63 @@ object DeriveSchemaSpec extends ZIOSpecDefault with VersionSpecificDeriveSchemaS
599614 ),
600615 versionSpecificSuite
601616 )
617+
618+ // Needed as I think is an unrelated existing bug in Scala 3 DeriveSchema whereby it adds simpleEnum annotation at the
619+ // top level of the EnumN schema when one of the cases is a simple enum - however this does not happen with the Scala 2 macro.
620+ // I think the Scala2 behavior is correct ie this should be a the leaf schema level.
621+ // Create issue https://github.com/zio/zio-schema/issues/750 to track this
622+ private def intermediateTraitTest (enum2Annotations : Chunk [Annotation ]) = {
623+ val derived : Schema .Enum2 [TraitLeaf .type , MiddleTrait , TraitWithMiddleTrait ] =
624+ DeriveSchema .gen[TraitWithMiddleTrait ]
625+
626+ val middleTraitLeafSchema = Schema .CaseClass0 (
627+ TypeId .fromTypeName(" zio.schema.DeriveSchemaSpec.MiddleTraitLeaf" ),
628+ () => MiddleTraitLeaf ,
629+ Chunk .empty
630+ )
631+ val middleTraitLeafCase = Schema .Case [MiddleTrait , MiddleTraitLeaf .type ](
632+ " MiddleTraitLeaf" ,
633+ middleTraitLeafSchema,
634+ (a : MiddleTrait ) => a.asInstanceOf [MiddleTraitLeaf .type ],
635+ (a : MiddleTraitLeaf .type ) => a.asInstanceOf [MiddleTrait ],
636+ (a : MiddleTrait ) => a.isInstanceOf [MiddleTraitLeaf .type ]
637+ )
638+ val middleTraitSchema = Schema .Enum1 [MiddleTraitLeaf .type , MiddleTrait ](
639+ TypeId .parse(" zio.schema.DeriveSchemaSpec.MiddleTrait" ),
640+ middleTraitLeafCase,
641+ Chunk (simpleEnum(automaticallyAdded = true ))
642+ )
643+
644+ val traitLeafSchema = Schema .CaseClass0 (
645+ TypeId .fromTypeName(" zio.schema.DeriveSchemaSpec.TraitLeaf" ),
646+ () => TraitLeaf ,
647+ Chunk .empty
648+ )
649+ val traitLeafCase = Schema .Case [TraitWithMiddleTrait , TraitLeaf .type ](
650+ " TraitLeaf" ,
651+ traitLeafSchema,
652+ (a : TraitWithMiddleTrait ) => a.asInstanceOf [TraitLeaf .type ],
653+ (a : TraitLeaf .type ) => a.asInstanceOf [TraitWithMiddleTrait ],
654+ (a : TraitWithMiddleTrait ) => a.isInstanceOf [TraitLeaf .type ]
655+ )
656+
657+ val middleTraitCase = Schema .Case [TraitWithMiddleTrait , MiddleTrait ](
658+ " MiddleTrait" ,
659+ middleTraitSchema,
660+ (a : TraitWithMiddleTrait ) => a.asInstanceOf [MiddleTrait ],
661+ (a : MiddleTrait ) => a.asInstanceOf [TraitWithMiddleTrait ],
662+ (a : TraitWithMiddleTrait ) => a.isInstanceOf [MiddleTrait ]
663+ )
664+
665+ val expected =
666+ Schema .Enum2 [TraitLeaf .type , MiddleTrait , TraitWithMiddleTrait ](
667+ TypeId .parse(" zio.schema.DeriveSchemaSpec.TraitWithMiddleTrait" ),
668+ traitLeafCase,
669+ middleTraitCase,
670+ enum2Annotations
671+ )
672+
673+ assert(derived)(hasSameSchema(expected))
674+ }
675+
602676}
0 commit comments