Skip to content

Commit

Permalink
fix record default value cannot be a collection/map of records (#605)
Browse files Browse the repository at this point in the history
  • Loading branch information
ex-ratt authored Aug 14, 2024
1 parent 6841970 commit 189ae44
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
7 changes: 7 additions & 0 deletions modules/core/src/main/scala/vulcan/internal/schema.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import java.nio.ByteBuffer
import org.apache.avro.generic.{GenericEnumSymbol, GenericFixed, IndexedRecord}
import vulcan.internal.converters.collection._

import java.{util => ju}

private[vulcan] object schema {
final def adaptForSchema(encoded: Any): Any =
encoded match {
Expand All @@ -26,6 +28,11 @@ private[vulcan] object schema {
map.updated(field.name, adaptForSchema(record.get(index)))
}
.asJava
case array: ju.Collection[?] =>
array.asScala.map(adaptForSchema).asJava
case map: ju.Map[?, ?] =>
// Avro only supports maps with String key, so we only have to adapt the values
map.asScala.map { case (key, value) => key -> adaptForSchema(value) }.asJava
case _ =>
encoded
}
Expand Down
36 changes: 36 additions & 0 deletions modules/core/src/test/scala/vulcan/CodecSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2050,6 +2050,24 @@ final class CodecSpec extends BaseSpec with CodecSpecHelpers {
}
}

it("should support record array default value") {
case class Test(value: List[Element])
case class Element(value: Int)

implicit val elementCodec: Codec[Element] =
Codec.record("Element", "") { field =>
field("value", _.value).map(Element(_))
}
implicit val testCodec: Codec[Test] =
Codec.record("Test", "") { field =>
field("value", _.value, default = Some(List(Element(123), Element(456)))).map(Test(_))
}

assertSchemaIs[Test] {
"""{"type":"record","name":"Test","fields":[{"name":"value","type":{"type":"array","items":{"type":"record","name":"Element","fields":[{"name":"value","type":"int"}]}},"default":[{"value":123},{"value":456}]}]}"""
}
}

it("should support map default value") {
case class Test(value: Map[String, Int])

Expand All @@ -2063,6 +2081,24 @@ final class CodecSpec extends BaseSpec with CodecSpecHelpers {
}
}

it("should support record map default value") {
case class Test(value: Map[String, Value])
case class Value(value: Int)

implicit val valueCodec: Codec[Value] =
Codec.record("Value", "") { field =>
field("value", _.value).map(Value(_))
}
implicit val testCodec: Codec[Test] =
Codec.record("Test", "") { field =>
field("value", _.value, default = Some(Map("key" -> Value(0)))).map(Test(_))
}

assertSchemaIs[Test] {
"""{"type":"record","name":"Test","fields":[{"name":"value","type":{"type":"map","values":{"type":"record","name":"Value","fields":[{"name":"value","type":"int"}]}},"default":{"key":{"value":0}}}]}"""
}
}

it("should support fixed default value") {
case class Inner(value: Array[Byte])

Expand Down

0 comments on commit 189ae44

Please sign in to comment.