From 187f40a432e8cb9d70ae79f0f7406109499fb53e Mon Sep 17 00:00:00 2001 From: wrongwrong Date: Mon, 13 Mar 2023 10:12:05 +0900 Subject: [PATCH 1/3] Changed so that kotlin-module cannot be used with Kotlin 1.4 or lower. --- .../com/fasterxml/jackson/module/kotlin/KotlinModule.kt | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/main/kotlin/com/fasterxml/jackson/module/kotlin/KotlinModule.kt b/src/main/kotlin/com/fasterxml/jackson/module/kotlin/KotlinModule.kt index 3672aefa..e26ad400 100644 --- a/src/main/kotlin/com/fasterxml/jackson/module/kotlin/KotlinModule.kt +++ b/src/main/kotlin/com/fasterxml/jackson/module/kotlin/KotlinModule.kt @@ -54,6 +54,13 @@ class KotlinModule @Deprecated( val singletonSupport: SingletonSupport = DISABLED, val strictNullChecks: Boolean = false ) : SimpleModule(KotlinModule::class.java.name, PackageVersion.VERSION) { + init { + if (!KotlinVersion.CURRENT.isAtLeast(1, 5)) { + // Kotlin 1.4 was deprecated when this process was introduced(jackson-module-kotlin 2.15). + throw IllegalStateException("jackson-module-kotlin requires Kotlin 1.5 or higher.") + } + } + @Deprecated(level = DeprecationLevel.HIDDEN, message = "For ABI compatibility") constructor( reflectionCacheSize: Int, From 54b1110177b6f0db556f3ff9a594e22234055521 Mon Sep 17 00:00:00 2001 From: wrongwrong Date: Mon, 13 Mar 2023 10:14:32 +0900 Subject: [PATCH 2/3] Removed branching to avoid problems with less than Kotlin 1.5. Support for Kotlin 1.4 and below has been removed. --- .../kotlin/KotlinAnnotationIntrospector.kt | 56 +++++++++---------- 1 file changed, 25 insertions(+), 31 deletions(-) diff --git a/src/main/kotlin/com/fasterxml/jackson/module/kotlin/KotlinAnnotationIntrospector.kt b/src/main/kotlin/com/fasterxml/jackson/module/kotlin/KotlinAnnotationIntrospector.kt index 135cbfbf..29d4459a 100644 --- a/src/main/kotlin/com/fasterxml/jackson/module/kotlin/KotlinAnnotationIntrospector.kt +++ b/src/main/kotlin/com/fasterxml/jackson/module/kotlin/KotlinAnnotationIntrospector.kt @@ -65,40 +65,34 @@ internal class KotlinAnnotationIntrospector(private val context: Module.SetupCon // Find a serializer to handle the case where the getter returns an unboxed value from the value class. override fun findSerializer(am: Annotated): StdSerializer<*>? = when (am) { is AnnotatedMethod -> { - when (KotlinVersion.CURRENT.isAtLeast(1, 5)) { - true -> { - val getter = am.member.apply { - // If the return value of the getter is a value class, - // it will be serialized properly without doing anything. - if (this.returnType.isUnboxableValueClass()) return null + val getter = am.member.apply { + // If the return value of the getter is a value class, + // it will be serialized properly without doing anything. + if (this.returnType.isUnboxableValueClass()) return null + } + + val kotlinProperty = getter + .declaringClass + .kotlin + .let { + // KotlinReflectionInternalError is raised in GitHub167 test, + // but it looks like an edge case, so it is ignored. + try { + it.memberProperties + } catch (e: Error) { + null } + }?.find { it.javaGetter == getter } - val kotlinProperty = getter - .declaringClass - .kotlin - .let { - // KotlinReflectionInternalError is raised in GitHub167 test, - // but it looks like an edge case, so it is ignored. - try { - it.memberProperties - } catch (e: Error) { - null - } - }?.find { it.javaGetter == getter } - - (kotlinProperty?.returnType?.classifier as? KClass<*>) - ?.takeIf { it.isValue } - ?.java - ?.let { outerClazz -> - val innerClazz = getter.returnType - - ValueClassStaticJsonValueSerializer.createdOrNull(outerClazz, innerClazz) - ?: @Suppress("UNCHECKED_CAST") ValueClassBoxSerializer(outerClazz, innerClazz) - } + (kotlinProperty?.returnType?.classifier as? KClass<*>) + ?.takeIf { it.isValue } + ?.java + ?.let { outerClazz -> + val innerClazz = getter.returnType + + ValueClassStaticJsonValueSerializer.createdOrNull(outerClazz, innerClazz) + ?: @Suppress("UNCHECKED_CAST") ValueClassBoxSerializer(outerClazz, innerClazz) } - // Kotlin 1.4 and lower doesn't have value classes and we avoid the NoSuchMethodException on it.isValue - else -> null - } } // Ignore the case of AnnotatedField, because JvmField cannot be set in the field of value class. else -> null From 2a091279160eb61fcead0ef71f635e98ff281aa7 Mon Sep 17 00:00:00 2001 From: wrongwrong Date: Tue, 14 Mar 2023 22:04:36 +0900 Subject: [PATCH 3/3] Change Exception and Message --- .../com/fasterxml/jackson/module/kotlin/KotlinModule.kt | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/main/kotlin/com/fasterxml/jackson/module/kotlin/KotlinModule.kt b/src/main/kotlin/com/fasterxml/jackson/module/kotlin/KotlinModule.kt index e26ad400..25d8d3f8 100644 --- a/src/main/kotlin/com/fasterxml/jackson/module/kotlin/KotlinModule.kt +++ b/src/main/kotlin/com/fasterxml/jackson/module/kotlin/KotlinModule.kt @@ -1,5 +1,6 @@ package com.fasterxml.jackson.module.kotlin +import com.fasterxml.jackson.databind.JsonMappingException import com.fasterxml.jackson.databind.MapperFeature import com.fasterxml.jackson.databind.module.SimpleModule import com.fasterxml.jackson.module.kotlin.KotlinFeature.NullIsSameAsDefault @@ -57,7 +58,10 @@ class KotlinModule @Deprecated( init { if (!KotlinVersion.CURRENT.isAtLeast(1, 5)) { // Kotlin 1.4 was deprecated when this process was introduced(jackson-module-kotlin 2.15). - throw IllegalStateException("jackson-module-kotlin requires Kotlin 1.5 or higher.") + throw JsonMappingException( + null, + "KotlinModule requires Kotlin version >= 1.5 - Found ${KotlinVersion.CURRENT}" + ) } }