Skip to content

Commit 887ac3c

Browse files
danielfagerstromDaniel Fagerstrom
and
Daniel Fagerstrom
authored
Enable json values for @AvroProp. (#855)
Co-authored-by: Daniel Fagerstrom <daniel.fagerstrom@svenskaspel.se>
1 parent c2e1cd8 commit 887ac3c

File tree

4 files changed

+28
-3
lines changed

4 files changed

+28
-3
lines changed

avro4s-core/src/main/scala/com/sksamuel/avro4s/annotations.scala

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.sksamuel.avro4s
22

3+
import com.fasterxml.jackson.databind.JsonNode
34
import scala.annotation.StaticAnnotation
45

56
case class AvroAlias(override val alias: String) extends AvroAliasable
@@ -79,11 +80,11 @@ trait AvroNamespaceable extends AvroFieldReflection {
7980
val namespace: String
8081
}
8182

82-
case class AvroProp(override val key: String, override val value:String) extends AvroProperty
83+
case class AvroProp(override val key: String, override val value: String | JsonNode) extends AvroProperty
8384

8485
trait AvroProperty extends AvroFieldReflection {
8586
val key: String
86-
val value: String
87+
val value: String | JsonNode
8788
}
8889

8990
/**

avro4s-core/src/main/scala/com/sksamuel/avro4s/typeutils/Annotations.scala

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.sksamuel.avro4s.typeutils
22

3+
import com.fasterxml.jackson.databind.JsonNode
34
import com.sksamuel.avro4s.{AvroAliasable, AvroDoc, AvroDocumentable, AvroErasedName, AvroError, AvroFixed, AvroName, AvroNameable, AvroNamespace, AvroNoDefault, AvroProp, AvroProperty, AvroSortPriority, AvroTransient, AvroUnionPosition}
45
import magnolia1.{CaseClass, TypeInfo}
56

@@ -18,7 +19,7 @@ class Annotations(annos: Seq[Any], inheritedAnnos: Seq[Any] = Nil) {
1819
case t: AvroAliasable => t.alias
1920
}.filterNot(_.trim.isEmpty)
2021

21-
def props: Map[String, String] = annos.collect {
22+
def props: Map[String, String | JsonNode] = annos.collect {
2223
case t: AvroProperty => (t.key, t.value)
2324
}.toMap
2425

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"type": "record",
3+
"name": "Annotated",
4+
"namespace": "com.sksamuel.avro4s.schema.AvroPropSchemaTest",
5+
"fields": [
6+
{
7+
"name": "str",
8+
"type": "string",
9+
"terms": [
10+
"foo",
11+
"bar"
12+
]
13+
}
14+
]
15+
}

avro4s-core/src/test/scala/com/sksamuel/avro4s/schema/AvroPropSchemaTest.scala

+8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.sksamuel.avro4s.schema
22

3+
import com.fasterxml.jackson.databind.{JsonNode, ObjectMapper}
34
import com.sksamuel.avro4s.{AvroProp, AvroSchema}
45
import org.scalatest.matchers.should.Matchers
56
import org.scalatest.wordspec.AnyWordSpec
@@ -19,6 +20,13 @@ class AvroPropSchemaTest extends AnyWordSpec with Matchers {
1920
val schema = AvroSchema[Annotated]
2021
schema.toString(true) shouldBe expected.toString(true)
2122
}
23+
"support json prop annotation on field" in {
24+
val jsonArray = (new ObjectMapper()).createArrayNode().add("foo").add("bar")
25+
case class Annotated(@AvroProp("terms", jsonArray) str: String)
26+
val expected = new org.apache.avro.Schema.Parser().parse(getClass.getResourceAsStream("/props_annotation_json_field.json"))
27+
val schema = AvroSchema[Annotated]
28+
schema.toString(true) shouldBe expected.toString(true)
29+
}
2230
// "support props annotations on scala enums" in {
2331
// case class Annotated(@AvroProp("cold", "play") colours: Colours.Value)
2432
// val expected = new org.apache.avro.Schema.Parser().parse(getClass.getResourceAsStream("/props_annotation_scala_enum.json"))

0 commit comments

Comments
 (0)