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 getCodec of matching type argument bug #1339

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ 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
Expand Down Expand Up @@ -142,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 @@ -193,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] ?: computeJavaType(it) }.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 Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,15 +228,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