Skip to content

Commit

Permalink
Fix getCodec of matching type argument bug (#1339)
Browse files Browse the repository at this point in the history
bson-kotlin optional type fix
Port bson-kotlin test to bson-kotlinx (#1452)

JAVA-5365
  • Loading branch information
ht-jo authored and rozza committed Jul 17, 2024
1 parent 1aafa7a commit f339684
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,19 @@ package org.bson.codecs.kotlin
import java.lang.reflect.ParameterizedType
import java.lang.reflect.Type
import kotlin.reflect.KClass
import kotlin.reflect.KClassifier
import kotlin.reflect.KFunction
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 @@ -140,7 +142,9 @@ internal data class DataClassCodec<T : Any>(
val primaryConstructor =
kClass.primaryConstructor ?: throw CodecConfigurationException("No primary constructor for $kClass")
val typeMap =
types.mapIndexed { i, k -> primaryConstructor.typeParameters[i].createType() to k }.toMap()
types
.mapIndexed { i, k -> primaryConstructor.typeParameters[i].createType().classifier!! to k }
.toMap()

val propertyModels =
primaryConstructor.parameters.map { kParameter ->
Expand Down Expand Up @@ -191,18 +195,20 @@ internal data class DataClassCodec<T : Any>(
@Suppress("UNCHECKED_CAST")
private fun getCodec(
kParameter: KParameter,
typeMap: Map<KType, Type>,
typeMap: Map<KClassifier, Type>,
codecRegistry: CodecRegistry
): Codec<Any> {
return when (kParameter.type.classifier) {
is KClass<*> -> {
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?.classifier] ?: computeJavaType(it) }
.toList())
}
is KTypeParameter -> {
when (val pType = typeMap[kParameter.type] ?: kParameter.type.javaType) {
when (val pType = typeMap[kParameter.type.classifier] ?: kParameter.type.javaType) {
is Class<*> ->
codecRegistry.getCodec(kParameter, (pType as Class<Any>).kotlin.javaObjectType, emptyList())
is ParameterizedType ->
Expand All @@ -219,6 +225,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 @@ -249,15 +249,17 @@ class DataClassCodecTest {
|"nestedParameterized": {
| "parameterizedDataClass":
| {"number": 4.2, "string": "myString", "parameterizedList": [{"name": "embedded1"}]},
| "other": "myOtherString"
| "other": "myOtherString", "optionalOther": "myOptionalOtherString"
| }
|}"""
.trimMargin()
val dataClass =
DataClassWithNestedParameterizedDataClass(
"myId",
DataClassWithNestedParameterized(
DataClassParameterized(4.2, "myString", listOf(DataClassEmbedded("embedded1"))), "myOtherString"))
DataClassParameterized(4.2, "myString", listOf(DataClassEmbedded("embedded1"))),
"myOtherString",
"myOptionalOtherString"))

assertRoundTrips(expected, dataClass)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ data class DataClassWithNestedParameterizedDataClass(

data class DataClassWithNestedParameterized<A, B, C : Number>(
val parameterizedDataClass: DataClassParameterized<C, A>,
val other: B
val other: B,
val optionalOther: B?
)

data class DataClassWithPair(val pair: Pair<String, Int>)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -373,15 +373,17 @@ class KotlinSerializerCodecTest {
|"nestedParameterized": {
| "parameterizedDataClass":
| {"number": 4.2, "string": "myString", "parameterizedList": [{"name": "embedded1"}]},
| "other": "myOtherString"
| "other": "myOtherString", "optionalOther": "myOptionalOtherString"
| }
|}"""
.trimMargin()
val dataClass =
DataClassWithNestedParameterizedDataClass(
"myId",
DataClassWithNestedParameterized(
DataClassParameterized(4.2, "myString", listOf(DataClassEmbedded("embedded1"))), "myOtherString"))
DataClassParameterized(4.2, "myString", listOf(DataClassEmbedded("embedded1"))),
"myOtherString",
"myOptionalOtherString"))

assertRoundTrips(expected, dataClass)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,8 @@ data class DataClassWithNestedParameterizedDataClass(
@Serializable
data class DataClassWithNestedParameterized<A, B, C : Number>(
val parameterizedDataClass: DataClassParameterized<C, A>,
val other: B
val other: B,
val optionalOther: B?
)

@Serializable data class DataClassWithPair(val pair: Pair<String, Int>)
Expand Down

0 comments on commit f339684

Please sign in to comment.