Skip to content
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

Fix contextual serialization inside value classes #117

Merged
merged 1 commit into from
Sep 27, 2021
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 @@ -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 "
Expand Down
Original file line number Diff line number Diff line change
@@ -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)
}
})
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
})
Original file line number Diff line number Diff line change
@@ -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({

Expand All @@ -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)
}
6 changes: 6 additions & 0 deletions src/test/resources/value_class.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
{
"name": "id",
"type": "string"
}, {
"name" : "uuid",
"type" : {
"type" : "string",
"logicalType" : "uuid"
}
}
]
}