Skip to content

Commit

Permalink
Handle kotlin / JVM erasure of types (#1295)
Browse files Browse the repository at this point in the history
* Handle kotlin / JVM erasure of types

It has been observed that the Kotlin can behave
differently during reflection and return Type<Object>
where normally it would report the correct type.

Checking for erasure allows the bson-kotlin library to handle
these cases and return the expected type.

JAVA-5292
  • Loading branch information
rozza authored Jan 24, 2024
1 parent d80e9c1 commit 0d8cc80
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@ import kotlin.reflect.KParameter
import kotlin.reflect.KProperty1
import kotlin.reflect.KType
import kotlin.reflect.KTypeParameter
import kotlin.reflect.KTypeProjection
import kotlin.reflect.full.createType
import kotlin.reflect.full.findAnnotation
import kotlin.reflect.full.findAnnotations
import kotlin.reflect.full.hasAnnotation
import kotlin.reflect.full.primaryConstructor
import kotlin.reflect.jvm.javaType
import kotlin.reflect.jvm.jvmErasure
import org.bson.BsonReader
import org.bson.BsonType
import org.bson.BsonWriter
Expand Down Expand Up @@ -199,7 +201,7 @@ internal data class DataClassCodec<T : Any>(
codecRegistry.getCodec(
kParameter,
(kParameter.type.classifier as KClass<Any>).javaObjectType,
kParameter.type.arguments.mapNotNull { typeMap[it.type] ?: it.type?.javaType }.toList())
kParameter.type.arguments.mapNotNull { typeMap[it.type] ?: computeJavaType(it) }.toList())
}
is KTypeParameter -> {
when (val pType = typeMap[kParameter.type] ?: kParameter.type.javaType) {
Expand All @@ -219,6 +221,13 @@ internal data class DataClassCodec<T : Any>(
"Could not find codec for ${kParameter.name} with type ${kParameter.type}")
}

private fun computeJavaType(kTypeProjection: KTypeProjection): Type? {
val javaType: Type = kTypeProjection.type?.javaType!!
return if (javaType == Any::class.java) {
kTypeProjection.type?.jvmErasure?.javaObjectType
} else javaType
}

@Suppress("UNCHECKED_CAST")
private fun CodecRegistry.getCodec(kParameter: KParameter, clazz: Class<Any>, types: List<Type>): Codec<Any> {
val codec =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,22 @@ import kotlin.test.assertEquals
import kotlin.test.assertNotNull
import kotlin.test.assertNull
import kotlin.test.assertTrue
import kotlin.time.Duration
import org.bson.BsonReader
import org.bson.BsonWriter
import org.bson.codecs.Codec
import org.bson.codecs.DecoderContext
import org.bson.codecs.EncoderContext
import org.bson.codecs.configuration.CodecConfigurationException
import org.bson.codecs.configuration.CodecRegistries.fromCodecs
import org.bson.codecs.configuration.CodecRegistries.fromProviders
import org.bson.codecs.configuration.CodecRegistries.fromRegistries
import org.bson.codecs.kotlin.samples.DataClassParameterized
import org.bson.codecs.kotlin.samples.DataClassWithJVMErasure
import org.bson.codecs.kotlin.samples.DataClassWithSimpleValues
import org.bson.conversions.Bson
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertDoesNotThrow
import org.junit.jupiter.api.assertThrows

class DataClassCodecProviderTest {
Expand Down Expand Up @@ -59,4 +70,23 @@ class DataClassCodecProviderTest {
assertTrue { codec is DataClassCodec }
assertEquals(DataClassWithSimpleValues::class.java, codec.encoderClass)
}

@Test
fun shouldBeAbleHandleDataClassWithJVMErasure() {

class DurationCodec : Codec<Duration> {
override fun encode(writer: BsonWriter, value: Duration, encoderContext: EncoderContext) = TODO()
override fun getEncoderClass(): Class<Duration> = Duration::class.java
override fun decode(reader: BsonReader, decoderContext: DecoderContext): Duration = TODO()
}

val registry =
fromRegistries(
fromCodecs(DurationCodec()), fromProviders(DataClassCodecProvider()), Bson.DEFAULT_CODEC_REGISTRY)

val codec = assertDoesNotThrow { registry.get(DataClassWithJVMErasure::class.java) }
assertNotNull(codec)
assertTrue { codec is DataClassCodec }
assertEquals(DataClassWithJVMErasure::class.java, codec.encoderClass)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.bson.codecs.kotlin.samples

import kotlin.time.Duration
import org.bson.BsonDocument
import org.bson.BsonMaxKey
import org.bson.BsonType
Expand Down Expand Up @@ -159,3 +160,5 @@ data class DataClassWithFailingInit(val id: String) {
}

data class DataClassWithSequence(val value: Sequence<String>)

data class DataClassWithJVMErasure(val duration: Duration, val ints: List<Int>)

0 comments on commit 0d8cc80

Please sign in to comment.