-
-
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 remote-tracking branch 'FasterXML/2.19'
- Loading branch information
Showing
9 changed files
with
334 additions
and
8 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
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
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
118 changes: 118 additions & 0 deletions
118
...le/kotlin/kogeraIntegration/deser/valueClass/mapKey/WithoutCustomDeserializeMethodTest.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,118 @@ | ||
package tools.jackson.module.kotlin.kogeraIntegration.deser.valueClass.mapKey | ||
|
||
import tools.jackson.module.kotlin.defaultMapper | ||
import tools.jackson.module.kotlin.readValue | ||
import tools.jackson.module.kotlin.kogeraIntegration.deser.valueClass.NonNullObject | ||
import tools.jackson.module.kotlin.kogeraIntegration.deser.valueClass.NullableObject | ||
import tools.jackson.module.kotlin.kogeraIntegration.deser.valueClass.Primitive | ||
import org.junit.jupiter.api.Assertions.assertEquals | ||
import org.junit.jupiter.api.Assertions.assertTrue | ||
import org.junit.jupiter.api.Nested | ||
import org.junit.jupiter.api.Test | ||
import org.junit.jupiter.api.assertThrows | ||
import tools.jackson.databind.DatabindException | ||
import tools.jackson.databind.DeserializationContext | ||
import tools.jackson.databind.exc.InvalidDefinitionException | ||
import tools.jackson.databind.module.SimpleModule | ||
import tools.jackson.module.kotlin.jacksonMapperBuilder | ||
import java.lang.reflect.InvocationTargetException | ||
import tools.jackson.databind.KeyDeserializer as JacksonKeyDeserializer | ||
|
||
class WithoutCustomDeserializeMethodTest { | ||
companion object { | ||
val throwable = IllegalArgumentException("test") | ||
} | ||
|
||
@Nested | ||
inner class DirectDeserialize { | ||
@Test | ||
fun primitive() { | ||
val result = defaultMapper.readValue<Map<Primitive, String?>>("""{"1":null}""") | ||
assertEquals(mapOf(Primitive(1) to null), result) | ||
} | ||
|
||
@Test | ||
fun nonNullObject() { | ||
val result = defaultMapper.readValue<Map<NonNullObject, String?>>("""{"foo":null}""") | ||
assertEquals(mapOf(NonNullObject("foo") to null), result) | ||
} | ||
|
||
@Test | ||
fun nullableObject() { | ||
val result = defaultMapper.readValue<Map<NullableObject, String?>>("""{"bar":null}""") | ||
assertEquals(mapOf(NullableObject("bar") to null), result) | ||
} | ||
} | ||
|
||
data class Dst( | ||
val p: Map<Primitive, String?>, | ||
val nn: Map<NonNullObject, String?>, | ||
val n: Map<NullableObject, String?> | ||
) | ||
|
||
@Test | ||
fun wrapped() { | ||
val src = """ | ||
{ | ||
"p":{"1":null}, | ||
"nn":{"foo":null}, | ||
"n":{"bar":null} | ||
} | ||
""".trimIndent() | ||
val result = defaultMapper.readValue<Dst>(src) | ||
val expected = Dst( | ||
mapOf(Primitive(1) to null), | ||
mapOf(NonNullObject("foo") to null), | ||
mapOf(NullableObject("bar") to null) | ||
) | ||
|
||
assertEquals(expected, result) | ||
} | ||
|
||
@JvmInline | ||
value class HasCheckConstructor(val value: Int) { | ||
init { | ||
if (value < 0) throw throwable | ||
} | ||
} | ||
|
||
@Test | ||
fun callConstructorCheckTest() { | ||
val e = assertThrows<InvocationTargetException> { | ||
defaultMapper.readValue<Map<HasCheckConstructor, String?>>("""{"-1":null}""") | ||
} | ||
assertTrue(e.cause === throwable) | ||
} | ||
|
||
data class Wrapped(val first: String, val second: String) { | ||
class KeyDeserializer : JacksonKeyDeserializer() { | ||
override fun deserializeKey(key: String, ctxt: DeserializationContext) = | ||
key.split("-").let { Wrapped(it[0], it[1]) } | ||
} | ||
} | ||
|
||
@JvmInline | ||
value class Wrapper(val w: Wrapped) | ||
|
||
@Test | ||
fun wrappedCustomObject() { | ||
// If a type that cannot be deserialized is specified, the default is an error. | ||
val thrown = assertThrows<DatabindException> { | ||
defaultMapper.readValue<Map<Wrapper, String?>>("""{"foo-bar":null}""") | ||
} | ||
assertTrue(thrown.cause is InvalidDefinitionException) | ||
|
||
val mapper = jacksonMapperBuilder() | ||
.addModule( | ||
object : SimpleModule() { | ||
init { addKeyDeserializer(Wrapped::class.java, Wrapped.KeyDeserializer()) } | ||
} | ||
) | ||
.build() | ||
|
||
val result = mapper.readValue<Map<Wrapper, String?>>("""{"foo-bar":null}""") | ||
val expected = mapOf(Wrapper(Wrapped("foo", "bar")) to null) | ||
|
||
assertEquals(expected, result) | ||
} | ||
} |
70 changes: 70 additions & 0 deletions
70
...kogeraIntegration/deser/valueClass/mapKey/keyDeserializer/SpecifiedForObjectMapperTest.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,70 @@ | ||
package tools.jackson.module.kotlin.kogeraIntegration.deser.valueClass.mapKey.keyDeserializer | ||
|
||
import tools.jackson.module.kotlin.readValue | ||
import tools.jackson.module.kotlin.kogeraIntegration.deser.valueClass.NonNullObject | ||
import tools.jackson.module.kotlin.kogeraIntegration.deser.valueClass.NullableObject | ||
import tools.jackson.module.kotlin.kogeraIntegration.deser.valueClass.Primitive | ||
import org.junit.jupiter.api.Assertions.assertEquals | ||
import org.junit.jupiter.api.Nested | ||
import org.junit.jupiter.api.Test | ||
import tools.jackson.databind.module.SimpleModule | ||
import tools.jackson.module.kotlin.jacksonMapperBuilder | ||
|
||
class SpecifiedForObjectMapperTest { | ||
companion object { | ||
val mapper = jacksonMapperBuilder().apply { | ||
val module = SimpleModule().apply { | ||
this.addKeyDeserializer(Primitive::class.java, Primitive.KeyDeserializer()) | ||
this.addKeyDeserializer(NonNullObject::class.java, NonNullObject.KeyDeserializer()) | ||
this.addKeyDeserializer(NullableObject::class.java, NullableObject.KeyDeserializer()) | ||
} | ||
this.addModule(module) | ||
}.build() | ||
} | ||
|
||
@Nested | ||
inner class DirectDeserialize { | ||
@Test | ||
fun primitive() { | ||
val result = mapper.readValue<Map<Primitive, String?>>("""{"1":null}""") | ||
assertEquals(mapOf(Primitive(101) to null), result) | ||
} | ||
|
||
@Test | ||
fun nonNullObject() { | ||
val result = mapper.readValue<Map<NonNullObject, String?>>("""{"foo":null}""") | ||
assertEquals(mapOf(NonNullObject("foo-deser") to null), result) | ||
} | ||
|
||
@Test | ||
fun nullableObject() { | ||
val result = mapper.readValue<Map<NullableObject, String?>>("""{"bar":null}""") | ||
assertEquals(mapOf(NullableObject("bar-deser") to null), result) | ||
} | ||
} | ||
|
||
data class Dst( | ||
val p: Map<Primitive, String?>, | ||
val nn: Map<NonNullObject, String?>, | ||
val n: Map<NullableObject, String?> | ||
) | ||
|
||
@Test | ||
fun wrapped() { | ||
val src = """ | ||
{ | ||
"p":{"1":null}, | ||
"nn":{"foo":null}, | ||
"n":{"bar":null} | ||
} | ||
""".trimIndent() | ||
val result = mapper.readValue<Dst>(src) | ||
val expected = Dst( | ||
mapOf(Primitive(101) to null), | ||
mapOf(NonNullObject("foo-deser") to null), | ||
mapOf(NullableObject("bar-deser") to null) | ||
) | ||
|
||
assertEquals(expected, result) | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
...Integration/deser/valueClass/mapKey/keyDeserializer/byAnnotation/SpecifiedForClassTest.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,37 @@ | ||
package tools.jackson.module.kotlin.kogeraIntegration.deser.valueClass.mapKey.keyDeserializer.byAnnotation | ||
|
||
import tools.jackson.databind.DeserializationContext | ||
import tools.jackson.module.kotlin.defaultMapper | ||
import tools.jackson.module.kotlin.jacksonObjectMapper | ||
import tools.jackson.module.kotlin.readValue | ||
import org.junit.jupiter.api.Assertions.assertEquals | ||
import org.junit.jupiter.api.Test | ||
import tools.jackson.databind.annotation.JsonDeserialize | ||
import tools.jackson.databind.KeyDeserializer as JacksonKeyDeserializer | ||
|
||
class SpecifiedForClassTest { | ||
@JsonDeserialize(keyUsing = Value.KeyDeserializer::class) | ||
@JvmInline | ||
value class Value(val v: Int) { | ||
class KeyDeserializer : JacksonKeyDeserializer() { | ||
override fun deserializeKey(key: String, ctxt: DeserializationContext) = Value(key.toInt() + 100) | ||
} | ||
} | ||
|
||
@Test | ||
fun directDeserTest() { | ||
val result = defaultMapper.readValue<Map<Value, String?>>("""{"1":null}""") | ||
|
||
assertEquals(mapOf(Value(101) to null), result) | ||
} | ||
|
||
data class Wrapper(val v: Map<Value, String?>) | ||
|
||
@Test | ||
fun paramDeserTest() { | ||
val mapper = jacksonObjectMapper() | ||
val result = mapper.readValue<Wrapper>("""{"v":{"1":null}}""") | ||
|
||
assertEquals(Wrapper(mapOf(Value(101) to null)), result) | ||
} | ||
} |
Oops, something went wrong.