Skip to content

Commit

Permalink
Enable json values for @AvroProp. (#855)
Browse files Browse the repository at this point in the history
Co-authored-by: Daniel Fagerstrom <[email protected]>
  • Loading branch information
danielfagerstrom and Daniel Fagerstrom authored Dec 2, 2024
1 parent c2e1cd8 commit 887ac3c
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.sksamuel.avro4s

import com.fasterxml.jackson.databind.JsonNode
import scala.annotation.StaticAnnotation

case class AvroAlias(override val alias: String) extends AvroAliasable
Expand Down Expand Up @@ -79,11 +80,11 @@ trait AvroNamespaceable extends AvroFieldReflection {
val namespace: String
}

case class AvroProp(override val key: String, override val value:String) extends AvroProperty
case class AvroProp(override val key: String, override val value: String | JsonNode) extends AvroProperty

trait AvroProperty extends AvroFieldReflection {
val key: String
val value: String
val value: String | JsonNode
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.sksamuel.avro4s.typeutils

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

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

def props: Map[String, String] = annos.collect {
def props: Map[String, String | JsonNode] = annos.collect {
case t: AvroProperty => (t.key, t.value)
}.toMap

Expand Down
15 changes: 15 additions & 0 deletions avro4s-core/src/test/resources/props_annotation_json_field.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"type": "record",
"name": "Annotated",
"namespace": "com.sksamuel.avro4s.schema.AvroPropSchemaTest",
"fields": [
{
"name": "str",
"type": "string",
"terms": [
"foo",
"bar"
]
}
]
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.sksamuel.avro4s.schema

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

0 comments on commit 887ac3c

Please sign in to comment.