@@ -11,12 +11,14 @@ import java.lang.reflect.AccessibleObject
11
11
import java.lang.reflect.Constructor
12
12
import java.lang.reflect.Field
13
13
import java.lang.reflect.Method
14
+ import kotlin.reflect.KClass
14
15
import kotlin.reflect.KFunction
15
16
import kotlin.reflect.KMutableProperty1
16
17
import kotlin.reflect.KProperty1
17
18
import kotlin.reflect.KType
18
19
import kotlin.reflect.full.createType
19
20
import kotlin.reflect.full.declaredMemberProperties
21
+ import kotlin.reflect.full.memberProperties
20
22
import kotlin.reflect.jvm.*
21
23
22
24
@@ -59,6 +61,39 @@ internal class KotlinAnnotationIntrospector(private val context: Module.SetupCon
59
61
return super .findCreatorAnnotation(config, a)
60
62
}
61
63
64
+ // Find a serializer to handle the case where the getter returns an unboxed value from the value class.
65
+ override fun findSerializer (am : Annotated ): ValueClassBoxSerializer ? = when (am) {
66
+ is AnnotatedMethod -> {
67
+ val getter = am.member.apply {
68
+ // If the return value of the getter is a value class,
69
+ // it will be serialized properly without doing anything.
70
+ if (this .returnType.isUnboxableValueClass()) return null
71
+ }
72
+
73
+ val kotlinProperty = getter
74
+ .declaringClass
75
+ .kotlin
76
+ .let {
77
+ // KotlinReflectionInternalError is raised in GitHub167 test,
78
+ // but it looks like an edge case, so it is ignored.
79
+ try {
80
+ it.memberProperties
81
+ } catch (e: Error ) {
82
+ null
83
+ }
84
+ }?.find { it.javaGetter == getter }
85
+
86
+ (kotlinProperty?.returnType?.classifier as ? KClass <* >)
87
+ ?.takeIf { it.isValue }
88
+ ?.let { outerClazz ->
89
+ @Suppress(" UNCHECKED_CAST" )
90
+ ValueClassBoxSerializer (outerClazz as KClass <Any >, getter.returnType)
91
+ }
92
+ }
93
+ // Ignore the case of AnnotatedField, because JvmField cannot be set in the field of value class.
94
+ else -> null
95
+ }
96
+
62
97
/* *
63
98
* Subclasses can be detected automatically for sealed classes, since all possible subclasses are known
64
99
* at compile-time to Kotlin. This makes [com.fasterxml.jackson.annotation.JsonSubTypes] redundant.
0 commit comments