Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create Builder for KotlinModule #306

Merged
merged 4 commits into from
Mar 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,22 @@ class KotlinModule constructor (
val reflectionCacheSize: Int = 512,
val nullToEmptyCollection: Boolean = false,
val nullToEmptyMap: Boolean = false,
val nullisSameAsDefault: Boolean = false
val nullIsSameAsDefault: Boolean = false
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I fixed the camelCasing on this property.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good, thanks.

) : SimpleModule(PackageVersion.VERSION) {

@Deprecated(level = DeprecationLevel.HIDDEN, message = "For ABI compatibility")
constructor(
reflectionCacheSize: Int = 512,
nullToEmptyCollection: Boolean = false,
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
}
Expand All @@ -39,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())
Expand All @@ -60,6 +66,30 @@ class KotlinModule constructor (
addMixIn(LongRange::class.java, ClosedRangeMixin::class.java)
addMixIn(ClosedRange::class.java, ClosedRangeMixin::class.java)
}

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)
}
}


Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.fasterxml.jackson.module.kotlin

import org.junit.Assert.*
import org.junit.Test

class KotlinModuleTest {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test because mis-wiring one of these booleans would be easy to do.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea as well.

@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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -81,4 +78,4 @@ class TestNullToDefault {
Assert.assertTrue(item.name != null)
Assert.assertTrue(item.order == -1)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down