Skip to content

(fix) Respect avro.java.string type #709

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

Merged
merged 1 commit into from
Feb 28, 2024
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 @@ -72,11 +72,17 @@ trait AvroGeneratorOps {
* Arbitrary [0-39] range and directly creating Utf-8 chosen to mimic [[RandomData]]. Also avoids
* some ser/de issues with IndexOutOfBounds decoding with CoderUtils & Kryo
*/
private def boundedLengthGen = Gen.chooseNum(0, 39)
private def utf8Gen = for {
n <- boundedLengthGen
cs <- Gen.listOfN(n, Arbitrary.arbChar.arbitrary)
} yield new Utf8(cs.mkString)
private def boundedLengthGen: Gen[Int] = Gen.chooseNum(0, 39)
private def avroStringGen(tpe: Schema): Gen[CharSequence] = {
val stringGen = Gen.oneOf(
Gen.oneOf(" ", "", "foo"),
Arbitrary.arbString.arbitrary
)
Option(tpe.getProp(GenericData.STRING_PROP)) match {
case Some("String") => stringGen
case _ => stringGen.map(new Utf8(_))
}
}

private def avroValueOf(schema: Schema)(implicit data: GenericData): Gen[Any] = {
import scala.jdk.CollectionConverters._
Expand Down Expand Up @@ -137,7 +143,7 @@ trait AvroGeneratorOps {
case Schema.Type.MAP =>
import HashMapBuildable._
val map = Gen.buildableOf[util.HashMap[CharSequence, Any], (CharSequence, Any)](
(utf8Gen, avroValueOf(schema.getValueType)).tupled
(avroStringGen(schema), avroValueOf(schema.getValueType)).tupled
)
conversion match {
case Some(c) => map.map(m => c.fromMap(m, schema, schema.getLogicalType))
Expand All @@ -155,11 +161,10 @@ trait AvroGeneratorOps {
}

case Schema.Type.STRING =>
val charSequence = Gen.oneOf(Gen.oneOf(" ", "", "foo "), utf8Gen)
val str = avroStringGen(schema)
conversion match {
case Some(c) =>
charSequence.map(cs => c.fromCharSequence(cs, schema, schema.getLogicalType))
case None => charSequence
case Some(c) => str.map(cs => c.fromCharSequence(cs, schema, schema.getLogicalType))
case None => str
}

case Schema.Type.BYTES =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package com.spotify.ratatool.scalacheck

import com.spotify.ratatool.avro.specific.{RequiredNestedRecord, TestRecord}
import org.apache.avro.generic.GenericData
import org.apache.avro.util.Utf8
import org.apache.avro.{Conversions, LogicalTypes, SchemaBuilder}
import org.scalacheck._
import org.scalatest.flatspec.AnyFlatSpec
Expand Down Expand Up @@ -83,4 +84,39 @@ class AvroGeneratorTest extends AnyFlatSpec with Matchers with ScalaCheckPropert
r.get("cost") shouldBe a[java.math.BigDecimal]
}
}

it should "respect string type" in {
// format: off
val schema = SchemaBuilder
.builder()
.record("TestStringType")
.fields()
.name("defaultStringField").`type`().stringType().noDefault()
.name("javaStringField").`type`().stringBuilder().prop(GenericData.STRING_PROP, "String").endString().noDefault()
.name("charSequenceField").`type`().stringBuilder().prop(GenericData.STRING_PROP, "CharSequence").endString().noDefault()
.name("utf8Field").`type`().stringBuilder().prop(GenericData.STRING_PROP, "CharSequence").endString().noDefault()
.name("mapDefaultKeyField").`type`().map().values().longType().noDefault()
.name("mapJavaStringKeyField").`type`().map().prop(GenericData.STRING_PROP, "String").values().longType().noDefault()
.endRecord()
// format: on

val gen = avroOf(schema)

forAll(gen) { r =>
r.get("defaultStringField") shouldBe an[Utf8]
r.get("javaStringField") shouldBe a[String]
r.get("charSequenceField") shouldBe an[Utf8]
r.get("utf8Field") shouldBe an[Utf8]

{
val m = r.get("mapDefaultKeyField").asInstanceOf[java.util.Map[_, _]]
if (!m.isEmpty) m.keySet().iterator().next() shouldBe an[Utf8]
}

{
val m = r.get("mapJavaStringKeyField").asInstanceOf[java.util.Map[_, _]]
if (!m.isEmpty) m.keySet().iterator().next() shouldBe a[String]
}
}
}
}