From 44d34e89ae5e9022391356e3922c5f8532a2f310 Mon Sep 17 00:00:00 2001 From: Drew Stephens Date: Sat, 7 Mar 2020 11:32:30 -0500 Subject: [PATCH 1/4] Add Builder for module --- .../jackson/module/kotlin/KotlinModule.kt | 32 ++++++++++++++++++- 1 file changed, 31 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 2832bb5e..ebb479ca 100644 --- a/src/main/kotlin/com/fasterxml/jackson/module/kotlin/KotlinModule.kt +++ b/src/main/kotlin/com/fasterxml/jackson/module/kotlin/KotlinModule.kt @@ -16,7 +16,6 @@ class KotlinModule constructor ( val nullToEmptyMap: Boolean = false, val nullisSameAsDefault: Boolean = false ) : SimpleModule(PackageVersion.VERSION) { - @Deprecated(level = DeprecationLevel.HIDDEN, message = "For ABI compatibility") constructor( reflectionCacheSize: Int = 512, @@ -60,6 +59,37 @@ class KotlinModule constructor ( addMixIn(LongRange::class.java, ClosedRangeMixin::class.java) addMixIn(ClosedRange::class.java, ClosedRangeMixin::class.java) } + + private constructor(builder: Builder) : this( + builder.reflectionCacheSize, + builder.nullToEmptyCollection, + builder.nullToEmptyMap, + builder.nullisSameAsDefault + ) + + class Builder { + var reflectionCacheSize: Int = 512 + private set + + var nullToEmptyCollection: Boolean = false + private set + + var nullToEmptyMap: Boolean = false + private set + + var nullisSameAsDefault: Boolean = false + private set + + fun reflectionCacheSize(reflectionCacheSize: Int) = apply { this.reflectionCacheSize = reflectionCacheSize } + + fun nullToEmptyCollection(nullToEmptyCollection: Boolean) = apply { this.nullToEmptyCollection = nullToEmptyCollection } + + fun nullToEmptyMap(nullToEmptyMap: Boolean) = apply { this.nullToEmptyMap = nullToEmptyMap } + + fun nullisSameAsDefault(nullisSameAsDefault: Boolean) = apply { this.nullisSameAsDefault = nullisSameAsDefault } + + fun build() = KotlinModule(this) + } } From 733a70fcebc0dfb86f27e7637110f15780500076 Mon Sep 17 00:00:00 2001 From: Drew Stephens Date: Sun, 8 Mar 2020 10:50:50 -0600 Subject: [PATCH 2/4] Test builder; correct camel case for nullIsSameAsDefault --- .../jackson/module/kotlin/KotlinModule.kt | 24 +++---- .../jackson/module/kotlin/KotlinModuleTest.kt | 67 +++++++++++++++++++ .../module/kotlin/test/NullToDefaultTests.kt | 7 +- 3 files changed, 81 insertions(+), 17 deletions(-) create mode 100644 src/test/kotlin/com/fasterxml/jackson/module/kotlin/KotlinModuleTest.kt 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 ebb479ca..7c4a917f 100644 --- a/src/main/kotlin/com/fasterxml/jackson/module/kotlin/KotlinModule.kt +++ b/src/main/kotlin/com/fasterxml/jackson/module/kotlin/KotlinModule.kt @@ -14,7 +14,7 @@ class KotlinModule constructor ( val reflectionCacheSize: Int = 512, val nullToEmptyCollection: Boolean = false, val nullToEmptyMap: Boolean = false, - val nullisSameAsDefault: Boolean = false + val nullIsSameAsDefault: Boolean = false ) : SimpleModule(PackageVersion.VERSION) { @Deprecated(level = DeprecationLevel.HIDDEN, message = "For ABI compatibility") constructor( @@ -23,6 +23,13 @@ class KotlinModule constructor ( nullToEmptyMap: Boolean = false ) : this(reflectionCacheSize, nullToEmptyCollection, nullToEmptyMap, false) + private constructor(builder: Builder) : this( + builder.reflectionCacheSize, + builder.nullToEmptyCollection, + builder.nullToEmptyMap, + builder.nullIsSameAsDefault + ) + companion object { const val serialVersionUID = 1L } @@ -38,12 +45,12 @@ class KotlinModule constructor ( val cache = ReflectionCache(reflectionCacheSize) - context.addValueInstantiators(KotlinInstantiators(cache, nullToEmptyCollection, nullToEmptyMap, nullisSameAsDefault)) + context.addValueInstantiators(KotlinInstantiators(cache, nullToEmptyCollection, nullToEmptyMap, nullIsSameAsDefault)) // [module-kotlin#225]: keep Kotlin singletons as singletons context.addBeanDeserializerModifier(KotlinBeanDeserializerModifier) - context.insertAnnotationIntrospector(KotlinAnnotationIntrospector(context, cache, nullToEmptyCollection, nullToEmptyMap, nullisSameAsDefault)) + context.insertAnnotationIntrospector(KotlinAnnotationIntrospector(context, cache, nullToEmptyCollection, nullToEmptyMap, nullIsSameAsDefault)) context.appendAnnotationIntrospector(KotlinNamesAnnotationIntrospector(this, cache, ignoredClassesForImplyingJsonCreator)) context.addDeserializers(KotlinDeserializers()) @@ -60,13 +67,6 @@ class KotlinModule constructor ( addMixIn(ClosedRange::class.java, ClosedRangeMixin::class.java) } - private constructor(builder: Builder) : this( - builder.reflectionCacheSize, - builder.nullToEmptyCollection, - builder.nullToEmptyMap, - builder.nullisSameAsDefault - ) - class Builder { var reflectionCacheSize: Int = 512 private set @@ -77,7 +77,7 @@ class KotlinModule constructor ( var nullToEmptyMap: Boolean = false private set - var nullisSameAsDefault: Boolean = false + var nullIsSameAsDefault: Boolean = false private set fun reflectionCacheSize(reflectionCacheSize: Int) = apply { this.reflectionCacheSize = reflectionCacheSize } @@ -86,7 +86,7 @@ class KotlinModule constructor ( fun nullToEmptyMap(nullToEmptyMap: Boolean) = apply { this.nullToEmptyMap = nullToEmptyMap } - fun nullisSameAsDefault(nullisSameAsDefault: Boolean) = apply { this.nullisSameAsDefault = nullisSameAsDefault } + fun nullIsSameAsDefault(nullIsSameAsDefault: Boolean) = apply { this.nullIsSameAsDefault = nullIsSameAsDefault } fun build() = KotlinModule(this) } diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/KotlinModuleTest.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/KotlinModuleTest.kt new file mode 100644 index 00000000..daf926ce --- /dev/null +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/KotlinModuleTest.kt @@ -0,0 +1,67 @@ +package com.fasterxml.jackson.module.kotlin + +import org.junit.Assert.* +import org.junit.Test + +class KotlinModuleTest { + @Test + fun builder_Defaults() { + val module = KotlinModule.Builder().build() + + assertEquals(512, module.reflectionCacheSize) + assertFalse(module.nullToEmptyCollection) + assertFalse(module.nullToEmptyMap) + assertFalse(module.nullIsSameAsDefault) + } + + @Test + fun builder_SetAll() { + val module = KotlinModule.Builder().apply { + reflectionCacheSize(123) + nullToEmptyCollection(true) + nullToEmptyMap(true) + nullIsSameAsDefault(true) + }.build() + + assertEquals(123, module.reflectionCacheSize) + assertTrue(module.nullToEmptyCollection) + assertTrue(module.nullToEmptyMap) + assertTrue(module.nullIsSameAsDefault) + } + + @Test + fun builder_NullToEmptyCollection() { + val module = KotlinModule.Builder().apply { + nullToEmptyCollection(true) + }.build() + + assertEquals(512, module.reflectionCacheSize) + assertTrue(module.nullToEmptyCollection) + assertFalse(module.nullToEmptyMap) + assertFalse(module.nullIsSameAsDefault) + } + + @Test + fun builder_NullToEmptyMap() { + val module = KotlinModule.Builder().apply { + nullToEmptyMap(true) + }.build() + + assertEquals(512, module.reflectionCacheSize) + assertFalse(module.nullToEmptyCollection) + assertTrue(module.nullToEmptyMap) + assertFalse(module.nullIsSameAsDefault) + } + + @Test + fun builder_NullIsSameAsDefault() { + val module = KotlinModule.Builder().apply { + nullIsSameAsDefault(true) + }.build() + + assertEquals(512, module.reflectionCacheSize) + assertFalse(module.nullToEmptyCollection) + assertFalse(module.nullToEmptyMap) + assertTrue(module.nullIsSameAsDefault) + } +} diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/NullToDefaultTests.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/NullToDefaultTests.kt index 50a17852..628ea5e6 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/NullToDefaultTests.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/NullToDefaultTests.kt @@ -3,16 +3,13 @@ package com.fasterxml.jackson.module.kotlin.test import com.fasterxml.jackson.databind.ObjectMapper import com.fasterxml.jackson.module.kotlin.KotlinModule import com.fasterxml.jackson.module.kotlin.MissingKotlinParameterException -import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper import com.fasterxml.jackson.module.kotlin.readValue import org.junit.Assert -import org.junit.Ignore import org.junit.Test -import org.junit.internal.runners.statements.ExpectException class TestNullToDefault { - private fun createMapper(allowDefaultingByNull: Boolean) = ObjectMapper().registerModule(KotlinModule(nullisSameAsDefault = allowDefaultingByNull)) + private fun createMapper(allowDefaultingByNull: Boolean) = ObjectMapper().registerModule(KotlinModule(nullIsSameAsDefault = allowDefaultingByNull)) private data class TestClass(val sku: Int = -1, val text: String, @@ -81,4 +78,4 @@ class TestNullToDefault { Assert.assertTrue(item.name != null) Assert.assertTrue(item.order == -1) } -} \ No newline at end of file +} From e0c4fd55a9627719ab94e4c24525e1bf675c7b93 Mon Sep 17 00:00:00 2001 From: Drew Stephens Date: Sun, 8 Mar 2020 14:10:04 -0600 Subject: [PATCH 3/4] Update test for offset change in databind #2643 --- .../jackson/module/kotlin/test/ParameterNameTests.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/ParameterNameTests.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/ParameterNameTests.kt index 1ff0a6e6..52b15410 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/ParameterNameTests.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/ParameterNameTests.kt @@ -36,8 +36,8 @@ class TestJacksonWithKotlin { } } - private val normalCasedJson = """{"name":"Frank","age":30,"primaryAddress":"something here","renamed":true,"createdDt":"2016-10-25T18:25:48.000+0000"}""" - private val pascalCasedJson = """{"Name":"Frank","Age":30,"PrimaryAddress":"something here","Renamed":true,"CreatedDt":"2016-10-25T18:25:48.000+0000"}""" + private val normalCasedJson = """{"name":"Frank","age":30,"primaryAddress":"something here","renamed":true,"createdDt":"2016-10-25T18:25:48.000+00:00"}""" + private val pascalCasedJson = """{"Name":"Frank","Age":30,"PrimaryAddress":"something here","Renamed":true,"CreatedDt":"2016-10-25T18:25:48.000+00:00"}""" private val normalCasedMapper = jacksonObjectMapper() .configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false) From a1142dc7d8113a3b26cb1b54963cb33570ba8be4 Mon Sep 17 00:00:00 2001 From: Drew Stephens Date: Sun, 8 Mar 2020 14:13:12 -0600 Subject: [PATCH 4/4] Describe change in version file --- release-notes/VERSION-2.x | 1 + 1 file changed, 1 insertion(+) diff --git a/release-notes/VERSION-2.x b/release-notes/VERSION-2.x index 8c261afc..3ae95b0e 100644 --- a/release-notes/VERSION-2.x +++ b/release-notes/VERSION-2.x @@ -7,6 +7,7 @@ Project: jackson-module-kotlin 2.11.0 (not yet released) #284: Use `AnnotationIntrospector.findRenameByField()` to support "is properties" +- Add Builder for KotlinModule Kotlin updated to 1.3.61