Skip to content

Commit

Permalink
Try to merge some of 2.12 changes manually
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Dec 31, 2020
1 parent 22a8923 commit 6787e10
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<KClass<*>>) : 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
}

Expand All @@ -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)
Expand All @@ -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<Any>)
val kConstructor = cache.kotlinFromJava(member.annotated as Constructor<Any>)
Expand All @@ -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
Expand Down Expand Up @@ -162,3 +161,5 @@ internal class KotlinNamesAnnotationIntrospector(val module: KotlinModule, val c
}
}
}

private fun AnnotatedMethod.isInlineClass() = declaringClass.declaredMethods.any { it.name.contains('-') }
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -32,4 +31,4 @@ class Github308 {
assertNull(dto.id)
assertEquals(dto.cityId, 12345)
}
}
}

0 comments on commit 6787e10

Please sign in to comment.