Repository: avro-kotlin/avro4k
Pull request #117 (2021-09-25 13:44:09)

Fix contextual serialization inside value classes
Fixes serialization of:

```Kotlin
@JvmInline
value class SomeValueClass(val a: TypeWithContextualSerializer)
```



Patch:
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 0b98451..2b058bd 100644
--- a/src/main/kotlin/com/github/avrokotlin/avro4k/schema/SchemaFor.kt
+++ b/src/main/kotlin/com/github/avrokotlin/avro4k/schema/SchemaFor.kt
@@ -187,7 +187,7 @@ fun schemaFor(serializersModule: SerializersModule,
          SerialKind.CONTEXTUAL -> schemaFor(
             serializersModule,
             requireNotNull(
-               serializersModule.getContextualDescriptor(descriptor)
+               serializersModule.getContextualDescriptor(descriptor.carrierDescriptor)
                   ?: descriptor.capturedKClass?.serializerOrNull()?.descriptor
             ) {
                "Contextual or default serializer not found for $descriptor "


Test patch:
diff --git a/src/test/kotlin/com/github/avrokotlin/avro4k/decoder/ValueClassDecoderTest.kt b/src/test/kotlin/com/github/avrokotlin/avro4k/decoder/ValueClassDecoderTest.kt
index 73fb7e4..7f339c2 100644
--- a/src/test/kotlin/com/github/avrokotlin/avro4k/decoder/ValueClassDecoderTest.kt
+++ b/src/test/kotlin/com/github/avrokotlin/avro4k/decoder/ValueClassDecoderTest.kt
@@ -1,21 +1,25 @@
 package com.github.avrokotlin.avro4k.decoder
 
 import com.github.avrokotlin.avro4k.Avro
+import com.github.avrokotlin.avro4k.schema.ValueClassSchemaTest
 import com.github.avrokotlin.avro4k.schema.ValueClassSchemaTest.ContainsInlineTest
 import com.github.avrokotlin.avro4k.schema.ValueClassSchemaTest.StringWrapper
-import io.kotest.matchers.shouldBe
 import io.kotest.core.spec.style.StringSpec
+import io.kotest.matchers.shouldBe
 import org.apache.avro.generic.GenericData
+import java.util.UUID
 
 class ValueClassDecoderTest : StringSpec({
 
    "decode value class" {
 
       val id = StringWrapper("100500")
+      val uuid = ValueClassSchemaTest.UuidWrapper(UUID.randomUUID())
       val schema = Avro.default.schema(ContainsInlineTest.serializer())
       val record = GenericData.Record(schema)
       record.put("id", id.a)
+      record.put("uuid", uuid.uuid.toString())
 
-      Avro.default.fromRecord(ContainsInlineTest.serializer(), record) shouldBe ContainsInlineTest(id)
+      Avro.default.fromRecord(ContainsInlineTest.serializer(), record) shouldBe ContainsInlineTest(id, uuid)
    }
 })
\ No newline at end of file
diff --git a/src/test/kotlin/com/github/avrokotlin/avro4k/encoder/ValueClassEncoderTest.kt b/src/test/kotlin/com/github/avrokotlin/avro4k/encoder/ValueClassEncoderTest.kt
index fb87d7b..17a83fb 100644
--- a/src/test/kotlin/com/github/avrokotlin/avro4k/encoder/ValueClassEncoderTest.kt
+++ b/src/test/kotlin/com/github/avrokotlin/avro4k/encoder/ValueClassEncoderTest.kt
@@ -6,14 +6,18 @@ import com.github.avrokotlin.avro4k.schema.ValueClassSchemaTest
 import io.kotest.core.spec.style.StringSpec
 import io.kotest.matchers.shouldBe
 import org.apache.avro.util.Utf8
+import java.util.UUID
 
 class ValueClassEncoderTest : StringSpec({
 
    "encode value class" {
 
       val id = ValueClassSchemaTest.StringWrapper("100500")
+      val uuid = UUID.randomUUID()
+      val uuidStr = uuid.toString()
+      val uuidW = ValueClassSchemaTest.UuidWrapper(uuid)
       val schema = Avro.default.schema(ValueClassSchemaTest.ContainsInlineTest.serializer())
       Avro.default.toRecord(ValueClassSchemaTest.ContainsInlineTest.serializer(),
-         ValueClassSchemaTest.ContainsInlineTest(id)) shouldBe ListRecord(schema, Utf8(id.a))
+         ValueClassSchemaTest.ContainsInlineTest(id, uuidW)) shouldBe ListRecord(schema, Utf8(id.a), Utf8(uuidStr))
    }
 })
\ No newline at end of file
diff --git a/src/test/kotlin/com/github/avrokotlin/avro4k/schema/ValueClassSchemaTest.kt b/src/test/kotlin/com/github/avrokotlin/avro4k/schema/ValueClassSchemaTest.kt
index 925626b..0b77c9d 100644
--- a/src/test/kotlin/com/github/avrokotlin/avro4k/schema/ValueClassSchemaTest.kt
+++ b/src/test/kotlin/com/github/avrokotlin/avro4k/schema/ValueClassSchemaTest.kt
@@ -1,9 +1,13 @@
+@file:UseContextualSerialization(forClasses = [UUID::class])
+
 package com.github.avrokotlin.avro4k.schema
 
 import com.github.avrokotlin.avro4k.Avro
 import io.kotest.core.spec.style.StringSpec
 import io.kotest.matchers.shouldBe
 import kotlinx.serialization.Serializable
+import kotlinx.serialization.UseContextualSerialization
+import java.util.UUID
 
 class ValueClassSchemaTest : StringSpec({
 
@@ -19,5 +23,9 @@ class ValueClassSchemaTest : StringSpec({
    value class StringWrapper(val a: String)
 
    @Serializable
-   data class ContainsInlineTest(val id: StringWrapper)
+   @JvmInline
+   value class UuidWrapper(val uuid: UUID)
+
+   @Serializable
+   data class ContainsInlineTest(val id: StringWrapper, val uuid: UuidWrapper)
 }
\ No newline at end of file
diff --git a/src/test/resources/value_class.json b/src/test/resources/value_class.json
index 5097658..1d08277 100644
--- a/src/test/resources/value_class.json
+++ b/src/test/resources/value_class.json
@@ -6,6 +6,12 @@
       {
          "name": "id",
          "type": "string"
+      }, {
+         "name" : "uuid",
+         "type" : {
+            "type" : "string",
+            "logicalType" : "uuid"
+         }
       }
    ]
 }