Skip to content

Commit

Permalink
Merge pull request #710 from k163377/fix/default-prop
Browse files Browse the repository at this point in the history
Fixed KotlinPropertyNameAsImplicitName not working in edge case
  • Loading branch information
k163377 authored Sep 15, 2023
2 parents 56dcfc6 + 9004f37 commit 00b320f
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 6 deletions.
1 change: 1 addition & 0 deletions release-notes/CREDITS-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ kkurczewski
* #689: Add KotlinDuration support

WrongWrong (@k163377)
* #710: Fixed KotlinPropertyNameAsImplicitName not working in edge case
* #709: Add failing test for #242
* #707: Changed to use default argument on null if JsonSetter(nulls = Nulls.SKIP) is specified.
* #700: Reduce the load on the search process for serializers
Expand Down
1 change: 1 addition & 0 deletions release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Co-maintainers:
#686: Added KotlinPropertyNameAsImplicitName feature to use Kotlin property names as implicit names for getters.
Enabling this feature eliminates some of the problems summarized in #630,
but also causes some behavioral changes and performance degradation.
A minor correction has been made to this option in #710.
#685: Streamline default value management for KotlinFeatures.
This improves the initialization cost of kotlin-module a little.
#684: Kotlin 1.5 has been deprecated and the minimum supported Kotlin version will be updated to 1.6.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,12 @@ package com.fasterxml.jackson.module.kotlin

import com.fasterxml.jackson.annotation.JsonCreator
import com.fasterxml.jackson.annotation.JsonProperty
import com.fasterxml.jackson.databind.PropertyName
import com.fasterxml.jackson.databind.cfg.MapperConfig
import com.fasterxml.jackson.databind.introspect.Annotated
import com.fasterxml.jackson.databind.introspect.AnnotatedConstructor
import com.fasterxml.jackson.databind.introspect.AnnotatedField
import com.fasterxml.jackson.databind.introspect.AnnotatedMember
import com.fasterxml.jackson.databind.introspect.AnnotatedMethod
import com.fasterxml.jackson.databind.introspect.AnnotatedParameter
import com.fasterxml.jackson.databind.introspect.NopAnnotationIntrospector
import com.fasterxml.jackson.databind.util.BeanUtil
import java.lang.reflect.Constructor
import java.lang.reflect.Method
import java.util.Locale
Expand Down Expand Up @@ -57,10 +53,11 @@ internal class KotlinNamesAnnotationIntrospector(
}

private fun getterNameFromKotlin(member: AnnotatedMethod): String? {
val getter = member.member
val getterName = member.member.name

return member.member.declaringClass.takeIf { it.isKotlinClass() }?.let { clazz ->
clazz.kotlin.memberProperties.find { it.javaGetter == getter }
// For edge case, methods must be compared by name, not directly.
clazz.kotlin.memberProperties.find { it.javaGetter?.name == getterName }
?.let { it.name }
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.fasterxml.jackson.module.kotlin.test.github

import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.kotlin.KotlinFeature
import com.fasterxml.jackson.module.kotlin.KotlinModule
import org.junit.Test
import kotlin.test.assertEquals

class Github710 {
interface I<T> {
val foo: T
val bAr: T get() = foo
}

class C(override val foo: Int) : I<Int>

@Test
fun test() {
val mapper = KotlinModule.Builder().enable(KotlinFeature.KotlinPropertyNameAsImplicitName)
.let { ObjectMapper().registerModule(it.build()) }
val result = mapper.writeValueAsString(C(1))

assertEquals("""{"foo":1,"bAr":1}""", result)
}
}

0 comments on commit 00b320f

Please sign in to comment.