Skip to content

Commit 093842d

Browse files
committed
Add UseKotlinPropertyNameForGetter option
1 parent ef6737d commit 093842d

File tree

3 files changed

+35
-5
lines changed

3 files changed

+35
-5
lines changed

src/main/kotlin/com/fasterxml/jackson/module/kotlin/KotlinFeature.kt

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,23 @@ enum class KotlinFeature(private val enabledByDefault: Boolean) {
4242
* may contain null values after deserialization.
4343
* Enabling it protects against this but has significant performance impact.
4444
*/
45-
StrictNullChecks(enabledByDefault = false);
45+
StrictNullChecks(enabledByDefault = false),
46+
47+
/**
48+
* By enabling this feature, the property name on Kotlin will be used as the getter name.
49+
*
50+
* By default, the name based on the getter name on the JVM is used as the accessor name.
51+
* This name may be different from the parameter/field name, in which case serialization results
52+
* may be incorrect or annotations may malfunction.
53+
* See [jackson-module-kotlin#630] for details.
54+
*
55+
* By enabling this feature, such malfunctions will not occur.
56+
*
57+
* On the other hand, enabling this option increases the amount of reflection processing,
58+
* which may result in performance degradation for both serialization and deserialization.
59+
* In addition, the adjustment of behavior using get:JvmName is disabled.
60+
*/
61+
UseKotlinPropertyNameForGetter(enabledByDefault = false);
4662

4763
internal val bitSet: BitSet = (1 shl ordinal).toBitSet()
4864

src/main/kotlin/com/fasterxml/jackson/module/kotlin/KotlinModule.kt

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import com.fasterxml.jackson.module.kotlin.KotlinFeature.NullIsSameAsDefault
77
import com.fasterxml.jackson.module.kotlin.KotlinFeature.NullToEmptyCollection
88
import com.fasterxml.jackson.module.kotlin.KotlinFeature.NullToEmptyMap
99
import com.fasterxml.jackson.module.kotlin.KotlinFeature.StrictNullChecks
10+
import com.fasterxml.jackson.module.kotlin.KotlinFeature.UseKotlinPropertyNameForGetter
1011
import com.fasterxml.jackson.module.kotlin.SingletonSupport.CANONICALIZE
1112
import com.fasterxml.jackson.module.kotlin.SingletonSupport.DISABLED
1213
import java.util.*
@@ -53,7 +54,8 @@ class KotlinModule @Deprecated(
5354
val nullToEmptyMap: Boolean = false,
5455
val nullIsSameAsDefault: Boolean = false,
5556
val singletonSupport: SingletonSupport = DISABLED,
56-
val strictNullChecks: Boolean = false
57+
val strictNullChecks: Boolean = false,
58+
val useKotlinPropertyNameForGetter: Boolean = false
5759
) : SimpleModule(KotlinModule::class.java.name, PackageVersion.VERSION) {
5860
init {
5961
if (!KotlinVersion.CURRENT.isAtLeast(1, 5)) {
@@ -102,7 +104,8 @@ class KotlinModule @Deprecated(
102104
builder.isEnabled(KotlinFeature.SingletonSupport) -> CANONICALIZE
103105
else -> DISABLED
104106
},
105-
builder.isEnabled(StrictNullChecks)
107+
builder.isEnabled(StrictNullChecks),
108+
builder.isEnabled(UseKotlinPropertyNameForGetter)
106109
)
107110

108111
companion object {
@@ -130,7 +133,13 @@ class KotlinModule @Deprecated(
130133
}
131134

132135
context.insertAnnotationIntrospector(KotlinAnnotationIntrospector(context, cache, nullToEmptyCollection, nullToEmptyMap, nullIsSameAsDefault))
133-
context.appendAnnotationIntrospector(KotlinNamesAnnotationIntrospector(this, cache, ignoredClassesForImplyingJsonCreator))
136+
context.appendAnnotationIntrospector(
137+
KotlinNamesAnnotationIntrospector(
138+
this,
139+
cache,
140+
ignoredClassesForImplyingJsonCreator,
141+
useKotlinPropertyNameForGetter)
142+
)
134143

135144
context.addDeserializers(KotlinDeserializers())
136145
context.addKeyDeserializers(KotlinKeyDeserializers)

src/main/kotlin/com/fasterxml/jackson/module/kotlin/KotlinNamesAnnotationIntrospector.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,12 @@ import kotlin.reflect.jvm.internal.KotlinReflectionInternalError
2727
import kotlin.reflect.jvm.javaType
2828
import kotlin.reflect.jvm.kotlinFunction
2929

30-
internal class KotlinNamesAnnotationIntrospector(val module: KotlinModule, val cache: ReflectionCache, val ignoredClassesForImplyingJsonCreator: Set<KClass<*>>) : NopAnnotationIntrospector() {
30+
internal class KotlinNamesAnnotationIntrospector(
31+
val module: KotlinModule,
32+
val cache: ReflectionCache,
33+
val ignoredClassesForImplyingJsonCreator: Set<KClass<*>>,
34+
val useKotlinPropertyNameForGetter: Boolean
35+
) : NopAnnotationIntrospector() {
3136
// since 2.4
3237
override fun findImplicitPropertyName(member: AnnotatedMember): String? {
3338
if (!member.declaringClass.isKotlinClass()) return null

0 commit comments

Comments
 (0)