Skip to content

Commit

Permalink
Added annotation to Names while decoding unions (#809)
Browse files Browse the repository at this point in the history
Co-authored-by: Vladimir Korchik <[email protected]>
  • Loading branch information
vkorchik and Vladimir Korchik authored Nov 28, 2023
1 parent 8ed2fac commit c2b1625
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ object TypeUnions {
require(schema.isUnion)

val decodersByName = ctx.subtypes.map { st =>
val names = Names(st.typeInfo)
val annos: Annotations = Annotations(st.annotations)
val names = Names(st.typeInfo, annos)
val subschema = SchemaHelper.extractTraitSubschema(names.fullName, schema)
names.fullName -> st.typeclass.decode(subschema)
}.toMap
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.sksamuel.avro4s.record.decoder

import com.sksamuel.avro4s._
import com.sksamuel.avro4s.*
import org.apache.avro.SchemaBuilder
import org.apache.avro.generic.{GenericData, GenericRecord}
import org.apache.avro.util.Utf8
Expand Down Expand Up @@ -58,6 +58,17 @@ class SealedTraitDecoderTest extends AnyFunSuite with Matchers {
napper shouldBe Napper(Nabble("foo", 44))
}

test("support round-trip for sealed traits of case classes") {
val schema = AvroSchema[Fruits]

val fruits = Fruits(Apple(2.45), Orange("blue"))

val record = Encoder[Fruits].encode(schema)(fruits)
val fruitsAgain = Decoder[Fruits].decode(schema)(record)

fruitsAgain shouldBe fruits
}

//test("support sealed traits of case classes") {
//val schema = AvroSchema[Wrapper]
//val record = new GenericData.Record(schema)
Expand Down Expand Up @@ -166,6 +177,8 @@ final case class Apple(weight: Double) extends Fruit
@AvroNamespace("market")
final case class Orange(color: String) extends Fruit

final case class Fruits(fruit1: Fruit, fruit2: Fruit)

@AvroNamespace("market")
final case class Buy(fruit: Fruit)

Expand Down

0 comments on commit c2b1625

Please sign in to comment.