-
Notifications
You must be signed in to change notification settings - Fork 175
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #521 from k163377/fix-findValueInstantiator
Fixed an issue that could cause unexpected behavior when an instance that inherits from `StdValueInstantiator` is passed to `defaultInstantiator`.
- Loading branch information
Showing
3 changed files
with
75 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
src/test/kotlin/com/fasterxml/jackson/module/kotlin/KotlinInstantiatorsTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package com.fasterxml.jackson.module.kotlin | ||
|
||
import com.fasterxml.jackson.databind.deser.std.StdValueInstantiator | ||
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), | ||
nullToEmptyCollection = false, | ||
nullToEmptyMap = false, | ||
nullIsSameAsDefault = false, | ||
strictNullChecks = false | ||
) | ||
|
||
@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(javaType), | ||
defaultInstantiator | ||
) | ||
|
||
assertEquals(defaultInstantiator, instantiator) | ||
} | ||
|
||
@Test | ||
fun `Provides KotlinValueInstantiator for Kotlin class`() { | ||
class TestClass | ||
|
||
val javaType = mapper.constructType(TestClass::class.java) | ||
val instantiator = kotlinInstantiators.findValueInstantiator( | ||
deserConfig, | ||
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 | ||
) | ||
} | ||
} | ||
} |