Skip to content

Commit f96ce3b

Browse files
authored
fix namespace inheritance for sealed traits (#836)
1 parent 21daa58 commit f96ce3b

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

avro4s-core/src/main/scala/com/sksamuel/avro4s/decoders/unions.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ object TypeUnions {
1717
require(schema.isUnion)
1818

1919
val decodersByName = ctx.subtypes.map { st =>
20-
val annos: Annotations = Annotations(st.annotations)
20+
val annos: Annotations = new Annotations(st.annotations, st.inheritedAnnotations)
2121
val names = Names(st.typeInfo, annos)
2222
val subschema = SchemaHelper.extractTraitSubschema(names.fullName, schema)
2323
names.fullName -> st.typeclass.decode(subschema)

avro4s-core/src/main/scala/com/sksamuel/avro4s/encoders/unions.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ object TypeUnions {
1818

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

21-
val annos: Annotations = Annotations(st.annotations)
21+
val annos: Annotations = new Annotations(st.annotations, st.inheritedAnnotations)
2222
val names = Names(st.typeInfo, annos)
2323

2424
val subschema: Schema = SchemaHelper.extractTraitSubschema(names.fullName, schema)

avro4s-core/src/test/scala/com/sksamuel/avro4s/record/decoder/SealedTraitDecoderTest.scala

+17
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import org.apache.avro.generic.{GenericData, GenericRecord}
66
import org.apache.avro.util.Utf8
77
import org.scalatest.funsuite.AnyFunSuite
88
import org.scalatest.matchers.should.Matchers
9+
import org.apache.avro.generic.GenericContainer
910

1011
class SealedTraitDecoderTest extends AnyFunSuite with Matchers {
1112

@@ -69,6 +70,22 @@ class SealedTraitDecoderTest extends AnyFunSuite with Matchers {
6970
fruitsAgain shouldBe fruits
7071
}
7172

73+
test("support round-trip for sealed traits with inheritable namespace") {
74+
@AvroNamespace("spam")
75+
sealed trait Foo
76+
object Foo {
77+
case class Bar(i: Int) extends Foo
78+
}
79+
80+
val value: Foo = Foo.Bar(42)
81+
val encoded = Encoder[Foo].encode(AvroSchema[Foo])(value)
82+
encoded.asInstanceOf[GenericContainer].getSchema().getNamespace() shouldBe "spam"
83+
84+
val decoded = Decoder[Foo].decode(AvroSchema[Foo])(encoded)
85+
86+
decoded shouldBe value
87+
}
88+
7289
//test("support sealed traits of case classes") {
7390
//val schema = AvroSchema[Wrapper]
7491
//val record = new GenericData.Record(schema)

0 commit comments

Comments
 (0)