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

Added annotation to Names while decoding unions #809

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading