From 5ced96cbf01c49a30c0450050eb5b337d3c5188c Mon Sep 17 00:00:00 2001 From: wrongwrong Date: Thu, 25 Nov 2021 12:58:39 +0900 Subject: [PATCH 1/5] Fixed comparison process. --- .../jackson/module/kotlin/KotlinValueInstantiator.kt | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/main/kotlin/com/fasterxml/jackson/module/kotlin/KotlinValueInstantiator.kt b/src/main/kotlin/com/fasterxml/jackson/module/kotlin/KotlinValueInstantiator.kt index 2196d968..519b31ab 100644 --- a/src/main/kotlin/com/fasterxml/jackson/module/kotlin/KotlinValueInstantiator.kt +++ b/src/main/kotlin/com/fasterxml/jackson/module/kotlin/KotlinValueInstantiator.kt @@ -201,8 +201,15 @@ internal class KotlinInstantiators( defaultInstantiator: ValueInstantiator ): ValueInstantiator { return if (beanDescriptor.beanClass.isKotlinClass()) { - if (defaultInstantiator is StdValueInstantiator) { - KotlinValueInstantiator(defaultInstantiator, cache, nullToEmptyCollection, nullToEmptyMap, nullIsSameAsDefault, strictNullChecks) + if (defaultInstantiator::class == StdValueInstantiator::class) { + KotlinValueInstantiator( + defaultInstantiator as StdValueInstantiator, + cache, + nullToEmptyCollection, + nullToEmptyMap, + nullIsSameAsDefault, + strictNullChecks + ) } else { // TODO: return defaultInstantiator and let default method parameters and nullability go unused? or die with exception: throw IllegalStateException("KotlinValueInstantiator requires that the default ValueInstantiator is StdValueInstantiator") From e4f1f96c0cc980c60f70f5047f5edc5777b431fb Mon Sep 17 00:00:00 2001 From: Drew Stephens Date: Fri, 26 Nov 2021 07:54:08 -0500 Subject: [PATCH 2/5] Basic test for KI --- .../module/kotlin/KotlinInstantiatorsTest.kt | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 src/test/kotlin/com/fasterxml/jackson/module/kotlin/KotlinInstantiatorsTest.kt diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/KotlinInstantiatorsTest.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/KotlinInstantiatorsTest.kt new file mode 100644 index 00000000..0ddf1dec --- /dev/null +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/KotlinInstantiatorsTest.kt @@ -0,0 +1,37 @@ +package com.fasterxml.jackson.module.kotlin + +import com.fasterxml.jackson.databind.deser.std.StdValueInstantiator +import org.junit.Assert.assertEquals +import org.junit.Test + +class KotlinInstantiatorsTest { + private val mapper = jacksonObjectMapper() + + private val kotlinInstantiators = KotlinInstantiators( + ReflectionCache(10), + nullToEmptyCollection = false, + nullToEmptyMap = false, + nullIsSameAsDefault = false, + strictNullChecks = false + ) + + private class DefaultClass + + private val deserConfig = mapper.deserializationConfig + private val beanDescription = deserConfig.introspect(mapper.constructType(String::class.java)) + private val defaultInstantiator = object : StdValueInstantiator( + deserConfig, + mapper.constructType(DefaultClass::class.java) + ) {} + + @Test + fun `Provides default instantiator for Java class`() { + val instantiator = kotlinInstantiators.findValueInstantiator( + deserConfig, + beanDescription, + defaultInstantiator + ) + + assertEquals(defaultInstantiator, instantiator) + } +} \ No newline at end of file From fade3f6c32f395ba44ab01b65674b70b1122493b Mon Sep 17 00:00:00 2001 From: Drew Stephens Date: Fri, 26 Nov 2021 08:03:38 -0500 Subject: [PATCH 3/5] Test for Kotlin class --- .../module/kotlin/KotlinInstantiatorsTest.kt | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/KotlinInstantiatorsTest.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/KotlinInstantiatorsTest.kt index 0ddf1dec..92628b84 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/KotlinInstantiatorsTest.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/KotlinInstantiatorsTest.kt @@ -2,6 +2,7 @@ package com.fasterxml.jackson.module.kotlin import com.fasterxml.jackson.databind.deser.std.StdValueInstantiator import org.junit.Assert.assertEquals +import org.junit.Assert.assertTrue import org.junit.Test class KotlinInstantiatorsTest { @@ -18,7 +19,6 @@ class KotlinInstantiatorsTest { private class DefaultClass private val deserConfig = mapper.deserializationConfig - private val beanDescription = deserConfig.introspect(mapper.constructType(String::class.java)) private val defaultInstantiator = object : StdValueInstantiator( deserConfig, mapper.constructType(DefaultClass::class.java) @@ -28,10 +28,24 @@ class KotlinInstantiatorsTest { fun `Provides default instantiator for Java class`() { val instantiator = kotlinInstantiators.findValueInstantiator( deserConfig, - beanDescription, + deserConfig.introspect(mapper.constructType(String::class.java)), defaultInstantiator ) assertEquals(defaultInstantiator, instantiator) } + + @Test + fun `Provides KotlinValueInstantiator for Kotlin class`() { + class TestClass + + val instantiator = kotlinInstantiators.findValueInstantiator( + deserConfig, + deserConfig.introspect(mapper.constructType(TestClass::class.java)), + defaultInstantiator + ) + + assertTrue(instantiator is StdValueInstantiator) + assertTrue(instantiator::class == KotlinValueInstantiator::class) + } } \ No newline at end of file From 9838ab49b2c60d6d665dc96531f87db6556e9480 Mon Sep 17 00:00:00 2001 From: Drew Stephens Date: Fri, 26 Nov 2021 08:22:30 -0500 Subject: [PATCH 4/5] Update test to to work with #521 --- .../module/kotlin/KotlinInstantiatorsTest.kt | 40 +++++++++++++------ 1 file changed, 27 insertions(+), 13 deletions(-) diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/KotlinInstantiatorsTest.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/KotlinInstantiatorsTest.kt index 92628b84..dad0fc60 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/KotlinInstantiatorsTest.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/KotlinInstantiatorsTest.kt @@ -1,12 +1,12 @@ package com.fasterxml.jackson.module.kotlin import com.fasterxml.jackson.databind.deser.std.StdValueInstantiator -import org.junit.Assert.assertEquals -import org.junit.Assert.assertTrue +import org.junit.Assert.* import org.junit.Test class KotlinInstantiatorsTest { private val mapper = jacksonObjectMapper() + private val deserConfig = mapper.deserializationConfig private val kotlinInstantiators = KotlinInstantiators( ReflectionCache(10), @@ -16,19 +16,13 @@ class KotlinInstantiatorsTest { strictNullChecks = false ) - private class DefaultClass - - private val deserConfig = mapper.deserializationConfig - private val defaultInstantiator = object : StdValueInstantiator( - deserConfig, - mapper.constructType(DefaultClass::class.java) - ) {} - @Test fun `Provides default instantiator for Java class`() { + val javaType = mapper.constructType(String::class.java) + val defaultInstantiator = StdValueInstantiator(deserConfig, javaType) val instantiator = kotlinInstantiators.findValueInstantiator( deserConfig, - deserConfig.introspect(mapper.constructType(String::class.java)), + deserConfig.introspect(javaType), defaultInstantiator ) @@ -39,13 +33,33 @@ class KotlinInstantiatorsTest { fun `Provides KotlinValueInstantiator for Kotlin class`() { class TestClass + val javaType = mapper.constructType(TestClass::class.java) val instantiator = kotlinInstantiators.findValueInstantiator( deserConfig, - deserConfig.introspect(mapper.constructType(TestClass::class.java)), - defaultInstantiator + deserConfig.introspect(javaType), + StdValueInstantiator(deserConfig, javaType) ) assertTrue(instantiator is StdValueInstantiator) assertTrue(instantiator::class == KotlinValueInstantiator::class) } + + @Test + fun `Throws for Kotlin class when default instantiator isn't StdValueInstantiator`() { + class TestClass + class DefaultClass + + val subClassInstantiator = object : StdValueInstantiator( + deserConfig, + mapper.constructType(DefaultClass::class.java) + ) {} + + assertThrows(IllegalStateException::class.java) { + kotlinInstantiators.findValueInstantiator( + deserConfig, + deserConfig.introspect(mapper.constructType(TestClass::class.java)), + subClassInstantiator + ) + } + } } \ No newline at end of file From 7b595979462606e748c72ece9a3d712137c2b002 Mon Sep 17 00:00:00 2001 From: Drew Stephens Date: Fri, 26 Nov 2021 08:27:59 -0500 Subject: [PATCH 5/5] Add credit for #521 --- release-notes/CREDITS-2.x | 1 + 1 file changed, 1 insertion(+) diff --git a/release-notes/CREDITS-2.x b/release-notes/CREDITS-2.x index 6fe440f2..309b593f 100644 --- a/release-notes/CREDITS-2.x +++ b/release-notes/CREDITS-2.x @@ -21,6 +21,7 @@ Stefan Schmid (schmist@github) wrongwrong (k163377@github) * #456: Refactor KNAI.findImplicitPropertyName() * #449: Refactor AnnotatedMethod.hasRequiredMarker() +* #521: Fixed lookup of instantiators Dmitri Domanine (novtor@github) * Contributed fix for #490: Missing value of type JsonNode? is deserialized as NullNode instead of null