From 6787e109fb6f76374da8fdf8d951e0c2f2889f8c Mon Sep 17 00:00:00 2001 From: Tatu Saloranta Date: Wed, 30 Dec 2020 17:24:31 -0800 Subject: [PATCH] Try to merge some of 2.12 changes manually --- .../KotlinNamesAnnotationIntrospector.kt | 37 ++++++++++--------- .../module/kotlin/test/github/Github308.kt | 5 +-- 2 files changed, 21 insertions(+), 21 deletions(-) diff --git a/src/main/kotlin/com/fasterxml/jackson/module/kotlin/KotlinNamesAnnotationIntrospector.kt b/src/main/kotlin/com/fasterxml/jackson/module/kotlin/KotlinNamesAnnotationIntrospector.kt index bea8e432..a84964ff 100644 --- a/src/main/kotlin/com/fasterxml/jackson/module/kotlin/KotlinNamesAnnotationIntrospector.kt +++ b/src/main/kotlin/com/fasterxml/jackson/module/kotlin/KotlinNamesAnnotationIntrospector.kt @@ -4,12 +4,7 @@ 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.AnnotatedParameter -import com.fasterxml.jackson.databind.introspect.NopAnnotationIntrospector +import com.fasterxml.jackson.databind.introspect.* import com.fasterxml.jackson.databind.util.BeanUtil import java.lang.reflect.Constructor import java.lang.reflect.Method @@ -25,18 +20,22 @@ import kotlin.reflect.jvm.javaType import kotlin.reflect.jvm.kotlinFunction internal class KotlinNamesAnnotationIntrospector(val module: KotlinModule, val cache: ReflectionCache, val ignoredClassesForImplyingJsonCreator: Set>) : NopAnnotationIntrospector() { - /* - override public fun findNameForDeserialization(annotated: Annotated?): PropertyName? { - // This should not do introspection here, only for explicit naming by annotations - return null - } - */ - // since 2.4 override fun findImplicitPropertyName(config: MapperConfig<*>, member: AnnotatedMember): String? { - if (member is AnnotatedParameter) { + if (member is AnnotatedMethod && member.isInlineClass()) { + if (member.name.startsWith("get") && + member.name.contains('-') && + member.parameterCount == 0) { + return member.name.substringAfter("get").decapitalize().substringBefore('-') + } else if (member.name.startsWith("is") && + member.name.contains('-') && + member.parameterCount == 0) { + return member.name.substringAfter("is").decapitalize().substringBefore('-') + } + } else if (member is AnnotatedParameter) { return findKotlinParameterName(member) } + return null } @@ -46,7 +45,8 @@ internal class KotlinNamesAnnotationIntrospector(val module: KotlinModule, val c // https://github.com/FasterXML/jackson-databind/issues/2527 // for details) override fun findRenameByField(config: MapperConfig<*>, - field: AnnotatedField, implName: PropertyName): PropertyName? { + field: AnnotatedField, + implName: PropertyName): PropertyName? { val origSimple = implName.simpleName if (field.declaringClass.isKotlinClass() && origSimple.startsWith("is")) { val mangledName: String? = BeanUtil.stdManglePropertyName(origSimple, 2) @@ -63,7 +63,7 @@ internal class KotlinNamesAnnotationIntrospector(val module: KotlinModule, val c if (member is AnnotatedConstructor && !member.declaringClass.isEnum) { // if has parameters, is a Kotlin class, and the parameters all have parameter annotations, then pretend we have a JsonCreator - if (member.getParameterCount() > 0 && member.declaringClass.isKotlinClass()) { + if (member.parameterCount > 0 && member.declaringClass.isKotlinClass()) { val found = cache.checkConstructorIsCreatorAnnotated(member) { val kClass = cache.kotlinFromJava(member.declaringClass as Class) val kConstructor = cache.kotlinFromJava(member.annotated as Constructor) @@ -87,8 +87,7 @@ internal class KotlinNamesAnnotationIntrospector(val module: KotlinModule, val c } val anyConstructorHasJsonCreator = kClass.constructors.filterOutSingleStringCallables() - .any { - it.annotations.any { it.annotationClass.java == JsonCreator::class.java } + .any { it.annotations.any { it.annotationClass.java == JsonCreator::class.java } } val anyCompanionMethodIsJsonCreator = member.type.rawClass.kotlin.companionObject?.declaredFunctions @@ -162,3 +161,5 @@ internal class KotlinNamesAnnotationIntrospector(val module: KotlinModule, val c } } } + +private fun AnnotatedMethod.isInlineClass() = declaringClass.declaredMethods.any { it.name.contains('-') } diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github308.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github308.kt index 5d25ebd4..eb58cb2a 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github308.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github308.kt @@ -10,8 +10,7 @@ import kotlin.test.assertEquals import kotlin.test.assertNotNull import kotlin.test.assertNull -class Github308 { - +class TestGithub308 { @JsonIgnoreProperties(ignoreUnknown = true) data class TestDto( @JsonIgnore @@ -32,4 +31,4 @@ class Github308 { assertNull(dto.id) assertEquals(dto.cityId, 12345) } -} \ No newline at end of file +}