Skip to content

Commit 80d289f

Browse files
committed
Modified the name of isGetter to be changed by findImplicitPropertyName
Because problems like #340 occur when using findRenameByField
1 parent 8e7aeb9 commit 80d289f

File tree

1 file changed

+27
-27
lines changed

1 file changed

+27
-27
lines changed

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

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -29,34 +29,34 @@ import kotlin.reflect.jvm.kotlinFunction
2929

3030
internal class KotlinNamesAnnotationIntrospector(val module: KotlinModule, val cache: ReflectionCache, val ignoredClassesForImplyingJsonCreator: Set<KClass<*>>) : NopAnnotationIntrospector() {
3131
// since 2.4
32-
override fun findImplicitPropertyName(member: AnnotatedMember): String? = when (member) {
33-
is AnnotatedMethod -> if (member.name.contains('-') && member.parameterCount == 0) {
34-
when {
35-
member.name.startsWith("get") -> member.name.substringAfter("get")
36-
member.name.startsWith("is") -> member.name.substringAfter("is")
37-
else -> null
38-
}?.replaceFirstChar { it.lowercase(Locale.getDefault()) }?.substringBefore('-')
39-
} else null
40-
is AnnotatedParameter -> findKotlinParameterName(member)
41-
else -> null
42-
}
43-
44-
// since 2.11: support Kotlin's way of handling "isXxx" backed properties where
45-
// logical property name needs to remain "isXxx" and not become "xxx" as with Java Beans
46-
// (see https://kotlinlang.org/docs/reference/java-to-kotlin-interop.html and
47-
// https://github.com/FasterXML/jackson-databind/issues/2527
48-
// for details)
49-
override fun findRenameByField(config: MapperConfig<*>,
50-
field: AnnotatedField,
51-
implName: PropertyName): PropertyName? {
52-
val origSimple = implName.simpleName
53-
if (field.declaringClass.isKotlinClass() && origSimple.startsWith("is")) {
54-
val mangledName: String? = BeanUtil.stdManglePropertyName(origSimple, 2)
55-
if ((mangledName != null) && !mangledName.equals(origSimple)) {
56-
return PropertyName.construct(mangledName)
57-
}
32+
override fun findImplicitPropertyName(member: AnnotatedMember): String? {
33+
if (!member.declaringClass.isKotlinClass()) return null
34+
35+
val name = member.name
36+
37+
return when (member) {
38+
is AnnotatedMethod -> if (member.parameterCount == 0) {
39+
// The reason for truncating after `-` is to truncate the random suffix
40+
// given after the value class accessor name.
41+
when {
42+
name.startsWith("get") -> name.takeIf { it.contains("-") }?.let { _ ->
43+
name.substringAfter("get")
44+
.replaceFirstChar { it.lowercase(Locale.getDefault()) }
45+
.substringBefore('-')
46+
}
47+
// since 2.15: support Kotlin's way of handling "isXxx" backed properties where
48+
// logical property name needs to remain "isXxx" and not become "xxx" as with Java Beans
49+
// (see https://kotlinlang.org/docs/reference/java-to-kotlin-interop.html and
50+
// https://github.com/FasterXML/jackson-databind/issues/2527 and
51+
// https://github.com/FasterXML/jackson-module-kotlin/issues/340
52+
// for details)
53+
name.startsWith("is") -> if (name.contains("-")) name.substringAfter("-") else name
54+
else -> null
55+
}
56+
} else null
57+
is AnnotatedParameter -> findKotlinParameterName(member)
58+
else -> null
5859
}
59-
return null
6060
}
6161

6262
@Suppress("UNCHECKED_CAST")

0 commit comments

Comments
 (0)