diff --git a/src/main/kotlin/com/github/avrokotlin/avro4k/schema/SchemaFor.kt b/src/main/kotlin/com/github/avrokotlin/avro4k/schema/SchemaFor.kt index bd8916f6..f702d554 100644 --- a/src/main/kotlin/com/github/avrokotlin/avro4k/schema/SchemaFor.kt +++ b/src/main/kotlin/com/github/avrokotlin/avro4k/schema/SchemaFor.kt @@ -7,7 +7,14 @@ import com.github.avrokotlin.avro4k.RecordNaming import kotlinx.serialization.ExperimentalSerializationApi import kotlinx.serialization.InternalSerializationApi import kotlinx.serialization.SerializationException -import kotlinx.serialization.descriptors.* +import kotlinx.serialization.descriptors.PolymorphicKind +import kotlinx.serialization.descriptors.PrimitiveKind +import kotlinx.serialization.descriptors.SerialDescriptor +import kotlinx.serialization.descriptors.SerialKind +import kotlinx.serialization.descriptors.StructureKind +import kotlinx.serialization.descriptors.capturedKClass +import kotlinx.serialization.descriptors.elementNames +import kotlinx.serialization.descriptors.getContextualDescriptor import kotlinx.serialization.modules.SerializersModule import kotlinx.serialization.serializerOrNull import org.apache.avro.Schema @@ -63,36 +70,6 @@ class EnumSchemaFor( } } -@ExperimentalSerializationApi -class PairSchemaFor(private val descriptor: SerialDescriptor, - private val configuration: AvroConfiguration, - private val serializersModule: SerializersModule, - private val resolvedSchemas: MutableMap -) : SchemaFor { - - override fun schema(): Schema { - val a = schemaFor( - serializersModule, - descriptor.getElementDescriptor(0), - descriptor.getElementAnnotations(0), - configuration, - resolvedSchemas - ) - val b = schemaFor( - serializersModule, - descriptor.getElementDescriptor(1), - descriptor.getElementAnnotations(1), - configuration, - resolvedSchemas - ) - return SchemaBuilder.unionOf() - .type(a.schema()) - .and() - .type(b.schema()) - .endUnion() - } -} - @ExperimentalSerializationApi class ListSchemaFor(private val descriptor: SerialDescriptor, private val serializersModule: SerializersModule, @@ -206,11 +183,7 @@ fun schemaFor(serializersModule: SerializersModule, resolvedSchemas ) - StructureKind.CLASS, StructureKind.OBJECT -> when (descriptor.serialName) { - "kotlin.Pair" -> PairSchemaFor(descriptor, configuration, serializersModule, resolvedSchemas) - else -> ClassSchemaFor(descriptor, configuration, serializersModule, resolvedSchemas) - } - + StructureKind.CLASS, StructureKind.OBJECT -> ClassSchemaFor(descriptor, configuration, serializersModule, resolvedSchemas) StructureKind.LIST -> ListSchemaFor(descriptor, serializersModule, configuration, resolvedSchemas) StructureKind.MAP -> MapSchemaFor(descriptor, serializersModule, configuration, resolvedSchemas) is PolymorphicKind -> UnionSchemaFor(descriptor, configuration, serializersModule, resolvedSchemas) @@ -224,4 +197,4 @@ fun schemaFor(serializersModule: SerializersModule, // copy-paste from kotlinx serialization because it internal @ExperimentalSerializationApi internal val SerialDescriptor.unwrapValueClass: SerialDescriptor - get() = if (isInline) getElementDescriptor(0) else this \ No newline at end of file + get() = if (isInline) getElementDescriptor(0) else this diff --git a/src/test/kotlin/com/github/avrokotlin/avro4k/io/RecursiveIoTest.kt b/src/test/kotlin/com/github/avrokotlin/avro4k/io/RecursiveIoTest.kt index 4a468800..7a2b6c92 100644 --- a/src/test/kotlin/com/github/avrokotlin/avro4k/io/RecursiveIoTest.kt +++ b/src/test/kotlin/com/github/avrokotlin/avro4k/io/RecursiveIoTest.kt @@ -8,11 +8,9 @@ import com.github.avrokotlin.avro4k.schema.Level4 import com.github.avrokotlin.avro4k.schema.RecursiveClass import com.github.avrokotlin.avro4k.schema.RecursiveListItem import com.github.avrokotlin.avro4k.schema.RecursiveMapValue -import com.github.avrokotlin.avro4k.schema.RecursivePair -import io.kotest.matchers.shouldBe import io.kotest.core.spec.style.StringSpec +import io.kotest.matchers.shouldBe import io.kotest.matchers.types.shouldBeInstanceOf -import org.apache.avro.generic.GenericData import org.apache.avro.generic.GenericRecord import org.apache.avro.util.Utf8 @@ -54,24 +52,6 @@ class RecursiveIoTest : StringSpec({ } } - "read / write direct recursive pair" { - writeRead(RecursivePair(1, (RecursivePair(2, null) to RecursivePair(3, null))), RecursivePair.serializer()) - writeRead(RecursivePair(1, (RecursivePair(2, null) to RecursivePair(3, null))), RecursivePair.serializer()) { - it["payload"] shouldBe 1 - it["pair"].shouldBeInstanceOf() - val first = (it["pair"] as GenericData.Record)["first"] - first.shouldBeInstanceOf() - first.schema shouldBe Avro.default.schema(RecursivePair.serializer()) - first["payload"] shouldBe 2 - first["pair"] shouldBe null - val second = (it["pair"] as GenericData.Record)["second"] - second.shouldBeInstanceOf() - second.schema shouldBe Avro.default.schema(RecursivePair.serializer()) - second["payload"] shouldBe 3 - second["pair"] shouldBe null - } - } - "read / write nested recursive classes" { writeRead(Level1(Level2(Level3(Level4(Level1(null))))), Level1.serializer()) writeRead(Level1(Level2(Level3(Level4(Level1(null))))), Level1.serializer()) { diff --git a/src/test/kotlin/com/github/avrokotlin/avro4k/schema/PairSchemaTest.kt b/src/test/kotlin/com/github/avrokotlin/avro4k/schema/PairSchemaTest.kt deleted file mode 100644 index deba7380..00000000 --- a/src/test/kotlin/com/github/avrokotlin/avro4k/schema/PairSchemaTest.kt +++ /dev/null @@ -1,48 +0,0 @@ -package com.github.avrokotlin.avro4k.schema - -import com.github.avrokotlin.avro4k.Avro -import io.kotest.matchers.shouldBe -import io.kotest.core.spec.style.FunSpec -import kotlinx.serialization.Serializable - -class PairSchemaTest : FunSpec({ - - test("!generate union:T,U for Pair[T,U] of primitives") { - - val expected = org.apache.avro.Schema.Parser().parse(javaClass.getResourceAsStream("/pair.json")) - val schema = Avro.default.schema(StringDoubleTest.serializer()) - schema.toString(true) shouldBe expected.toString(true) - } - test("!generate union:T,U for Either[T,U] of records") { - - val expected = org.apache.avro.Schema.Parser().parse(javaClass.getResourceAsStream("/pair_records.json")) - val schema = Avro.default.schema(GooFooTest.serializer()) - schema.toString(true) shouldBe expected.toString(true) - } -// "generate union:T,U for Either[T,U] of records using @AvroNamespace" in { -// @AvroNamespace("mm") -// data class Goo(s: String) -// @AvroNamespace("nn") -// data class Foo(b: Boolean) -// data class Test(either: Either[Goo, Foo]) -// val expected = new org.apache.avro.Schema.Parser().parse(getClass.getResourceAsStream("/either_record_with_avro_namespace.json")) -// val schema = AvroSchema[Test] -// schema.toString(true) shouldBe expected.toString(true) -// } -// test("flatten nested unions and move null to first position") { -// AvroSchema[Either[String, Option[Int]]].toString shouldBe """["null","string","int"]""" -// } - -}) { - @Serializable - data class StringDoubleTest(val p: Pair) - - @Serializable - data class Goo(val s: String) - - @Serializable - data class Foo(val b: Boolean) - - @Serializable - data class GooFooTest(val p: Pair) -} diff --git a/src/test/kotlin/com/github/avrokotlin/avro4k/schema/RecursiveSchemaTest.kt b/src/test/kotlin/com/github/avrokotlin/avro4k/schema/RecursiveSchemaTest.kt index b4bd8eb6..63b54302 100644 --- a/src/test/kotlin/com/github/avrokotlin/avro4k/schema/RecursiveSchemaTest.kt +++ b/src/test/kotlin/com/github/avrokotlin/avro4k/schema/RecursiveSchemaTest.kt @@ -1,8 +1,8 @@ package com.github.avrokotlin.avro4k.schema import com.github.avrokotlin.avro4k.Avro -import io.kotest.matchers.shouldBe import io.kotest.core.spec.style.FunSpec +import io.kotest.matchers.shouldBe import kotlinx.serialization.Serializable @Serializable @@ -14,9 +14,6 @@ data class RecursiveListItem(val payload: Int, val list: List @Serializable data class RecursiveMapValue(val payload: Int, val map: Map?) -@Serializable -data class RecursivePair(val payload: Int, val pair: Pair?) - @Serializable data class Level4(val level1: Level1) @@ -49,12 +46,6 @@ class RecursiveSchemaTest : FunSpec({ schema.toString(true) shouldBe expected.toString(true) } - test("accept direct recursive pairs") { - val expected = org.apache.avro.Schema.Parser().parse(this::class.java.getResourceAsStream("/recursive_pair.json")) - val schema = Avro.default.schema(RecursivePair.serializer()) - schema.toString(true) shouldBe expected.toString(true) - } - test("accept nested recursive classes") { val expected = org.apache.avro.Schema.Parser().parse(this::class.java.getResourceAsStream("/recursive_nested.json")) val schema = Avro.default.schema(Level1.serializer()) diff --git a/src/test/resources/pair.json b/src/test/resources/pair.json deleted file mode 100644 index fbb75895..00000000 --- a/src/test/resources/pair.json +++ /dev/null @@ -1,11 +0,0 @@ -{ - "type": "record", - "name": "Annotated", - "namespace": "com.github.avrokotlin.avro4k.schema.PairSchemaTest", - "fields": [ - { - "name": "p", - "type": "string" - } - ] -} diff --git a/src/test/resources/pair_records.json b/src/test/resources/pair_records.json deleted file mode 100644 index ce6ef1bd..00000000 --- a/src/test/resources/pair_records.json +++ /dev/null @@ -1,14 +0,0 @@ -{ - "type": "record", - "name": "Annotated", - "namespace": "com.github.avrokotlin.avro4k.schema.AvroAliasSchemaTest", - "fields": [ - { - "name": "str", - "type": "string", - "aliases": [ - "cold" - ] - } - ] -} diff --git a/src/test/resources/recursive_pair.json b/src/test/resources/recursive_pair.json deleted file mode 100644 index b5e6b994..00000000 --- a/src/test/resources/recursive_pair.json +++ /dev/null @@ -1,23 +0,0 @@ -{ - "type" : "record", - "name" : "RecursivePair", - "namespace" : "com.github.avrokotlin.avro4k.schema", - "fields" : [ { - "name" : "payload", - "type" : "int" - }, { - "name" : "pair", - "type" : [ "null", { - "type" : "record", - "name" : "Pair", - "namespace" : "kotlin", - "fields" : [ { - "name" : "first", - "type" : "com.github.avrokotlin.avro4k.schema.RecursivePair" - }, { - "name" : "second", - "type" : "com.github.avrokotlin.avro4k.schema.RecursivePair" - } ] - } ] - } ] -}