Skip to content

Commit

Permalink
fix namespace inheritance for sealed traits (#836)
Browse files Browse the repository at this point in the history
  • Loading branch information
mberndt123 authored May 17, 2024
1 parent 21daa58 commit f96ce3b
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ object TypeUnions {
require(schema.isUnion)

val decodersByName = ctx.subtypes.map { st =>
val annos: Annotations = Annotations(st.annotations)
val annos: Annotations = new Annotations(st.annotations, st.inheritedAnnotations)
val names = Names(st.typeInfo, annos)
val subschema = SchemaHelper.extractTraitSubschema(names.fullName, schema)
names.fullName -> st.typeclass.decode(subschema)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ object TypeUnions {

val encoderBySubtype = ctx.subtypes.sorted(SubtypeOrdering).map(st => {

val annos: Annotations = Annotations(st.annotations)
val annos: Annotations = new Annotations(st.annotations, st.inheritedAnnotations)
val names = Names(st.typeInfo, annos)

val subschema: Schema = SchemaHelper.extractTraitSubschema(names.fullName, schema)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import org.apache.avro.generic.{GenericData, GenericRecord}
import org.apache.avro.util.Utf8
import org.scalatest.funsuite.AnyFunSuite
import org.scalatest.matchers.should.Matchers
import org.apache.avro.generic.GenericContainer

class SealedTraitDecoderTest extends AnyFunSuite with Matchers {

Expand Down Expand Up @@ -69,6 +70,22 @@ class SealedTraitDecoderTest extends AnyFunSuite with Matchers {
fruitsAgain shouldBe fruits
}

test("support round-trip for sealed traits with inheritable namespace") {
@AvroNamespace("spam")
sealed trait Foo
object Foo {
case class Bar(i: Int) extends Foo
}

val value: Foo = Foo.Bar(42)
val encoded = Encoder[Foo].encode(AvroSchema[Foo])(value)
encoded.asInstanceOf[GenericContainer].getSchema().getNamespace() shouldBe "spam"

val decoded = Decoder[Foo].decode(AvroSchema[Foo])(encoded)

decoded shouldBe value
}

//test("support sealed traits of case classes") {
//val schema = AvroSchema[Wrapper]
//val record = new GenericData.Record(schema)
Expand Down

0 comments on commit f96ce3b

Please sign in to comment.