Skip to content

Commit ec2f84e

Browse files
committed
Fixed thrown exception to RuntimeJsonMappingException
1 parent 5d404ea commit ec2f84e

File tree

3 files changed

+18
-18
lines changed

3 files changed

+18
-18
lines changed

src/main/kotlin/com/fasterxml/jackson/module/kotlin/Extensions.kt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ import com.fasterxml.jackson.core.JsonParser
44
import com.fasterxml.jackson.core.TreeNode
55
import com.fasterxml.jackson.core.type.TypeReference
66
import com.fasterxml.jackson.databind.JsonDeserializer
7-
import com.fasterxml.jackson.databind.JsonMappingException
87
import com.fasterxml.jackson.databind.JsonNode
98
import com.fasterxml.jackson.databind.JsonSerializer
109
import com.fasterxml.jackson.databind.MappingIterator
1110
import com.fasterxml.jackson.databind.ObjectMapper
1211
import com.fasterxml.jackson.databind.ObjectReader
12+
import com.fasterxml.jackson.databind.RuntimeJsonMappingException
1313
import com.fasterxml.jackson.databind.json.JsonMapper
1414
import com.fasterxml.jackson.databind.module.SimpleModule
1515
import com.fasterxml.jackson.databind.node.ArrayNode
@@ -57,10 +57,11 @@ inline fun <reified T> jacksonTypeRef(): TypeReference<T> = object: TypeReferenc
5757
inline fun <reified T> Any?.checkTypeMismatch(): T {
5858
// Basically, this check assumes that T is non-null and the value is null.
5959
// Since this can be caused by both input or ObjectMapper implementation errors,
60-
// a more abstract JsonMappingException is thrown.
60+
// a more abstract RuntimeJsonMappingException is thrown.
6161
if (this !is T) {
62-
throw JsonMappingException(
63-
null,
62+
// Since the databind implementation of MappingIterator throws RuntimeJsonMappingException,
63+
// JsonMappingException was not used to unify the behavior.
64+
throw RuntimeJsonMappingException(
6465
"Deserialized value did not match the specified type; " +
6566
"specified ${T::class.qualifiedName} but was ${this?.let { it::class.qualifiedName }}"
6667
)

src/test/kotlin/com/fasterxml/jackson/module/kotlin/ReadValueTest.kt

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.fasterxml.jackson.module.kotlin
22

3-
import com.fasterxml.jackson.databind.JsonMappingException
3+
import com.fasterxml.jackson.databind.RuntimeJsonMappingException
44
import com.fasterxml.jackson.databind.node.NullNode
55
import org.junit.jupiter.api.Nested
66
import org.junit.jupiter.api.Test
@@ -13,15 +13,15 @@ class ReadValueTest {
1313
@Test
1414
fun jsonParser() {
1515
val src = defaultMapper.createParser("null")
16-
assertThrows<JsonMappingException> {
16+
assertThrows<RuntimeJsonMappingException> {
1717
defaultMapper.readValue<String>(src)
18-
}
18+
}.printStackTrace()
1919
}
2020

2121
@Test
2222
fun file() {
2323
val src = createTempJson("null")
24-
assertThrows<JsonMappingException> {
24+
assertThrows<RuntimeJsonMappingException> {
2525
defaultMapper.readValue<String>(src)
2626
}
2727
}
@@ -34,45 +34,45 @@ class ReadValueTest {
3434
@Test
3535
fun string() {
3636
val src = "null"
37-
assertThrows<JsonMappingException> {
37+
assertThrows<RuntimeJsonMappingException> {
3838
defaultMapper.readValue<String>(src)
3939
}
4040
}
4141

4242
@Test
4343
fun reader() {
4444
val src = StringReader("null")
45-
assertThrows<JsonMappingException> {
45+
assertThrows<RuntimeJsonMappingException> {
4646
defaultMapper.readValue<String>(src)
4747
}
4848
}
4949

5050
@Test
5151
fun inputStream() {
5252
val src = "null".byteInputStream()
53-
assertThrows<JsonMappingException> {
53+
assertThrows<RuntimeJsonMappingException> {
5454
defaultMapper.readValue<String>(src)
5555
}
5656
}
5757

5858
@Test
5959
fun byteArray() {
6060
val src = "null".toByteArray()
61-
assertThrows<JsonMappingException> {
61+
assertThrows<RuntimeJsonMappingException> {
6262
defaultMapper.readValue<String>(src)
6363
}
6464
}
6565

6666
@Test
6767
fun treeToValueTreeNode() {
68-
assertThrows<JsonMappingException> {
68+
assertThrows<RuntimeJsonMappingException> {
6969
defaultMapper.treeToValue<String>(NullNode.instance)
7070
}
7171
}
7272

7373
@Test
7474
fun convertValueAny() {
75-
assertThrows<JsonMappingException> {
75+
assertThrows<RuntimeJsonMappingException> {
7676
defaultMapper.convertValue<String>(null)
7777
}
7878
}
@@ -81,7 +81,7 @@ class ReadValueTest {
8181
fun readValueTypedJsonParser() {
8282
val reader = defaultMapper.reader()
8383
val src = reader.createParser("null")
84-
assertThrows<JsonMappingException> {
84+
assertThrows<RuntimeJsonMappingException> {
8585
reader.readValueTyped<String>(src)
8686
}
8787
}

src/test/kotlin/com/fasterxml/jackson/module/kotlin/ReadValuesTest.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package com.fasterxml.jackson.module.kotlin
22

33
import com.fasterxml.jackson.core.JsonParser
44
import com.fasterxml.jackson.databind.DeserializationContext
5-
import com.fasterxml.jackson.databind.JsonMappingException
65
import com.fasterxml.jackson.databind.RuntimeJsonMappingException
76
import com.fasterxml.jackson.databind.deser.std.StdDeserializer
87
import com.fasterxml.jackson.databind.module.SimpleModule
@@ -46,7 +45,7 @@ class ReadValuesTest {
4645
val itr = mapper.readValues<String>(src)
4746

4847
assertEquals("foo", itr.nextValue())
49-
assertThrows<JsonMappingException> {
48+
assertThrows<RuntimeJsonMappingException> {
5049
itr.nextValue()
5150
}
5251
}
@@ -58,7 +57,7 @@ class ReadValuesTest {
5857
val itr = reader.readValuesTyped<String>(src)
5958

6059
assertEquals("foo", itr.next())
61-
assertThrows<JsonMappingException> {
60+
assertThrows<RuntimeJsonMappingException> {
6261
itr.next()
6362
}
6463
}

0 commit comments

Comments
 (0)