Skip to content

Commit 43f01ea

Browse files
authored
Merge pull request #682 from k163377/fix/617
Remove MissingKotlinParameterException and replace with MismatchedInputException
2 parents 25a54ff + a90463c commit 43f01ea

File tree

9 files changed

+44
-80
lines changed

9 files changed

+44
-80
lines changed

release-notes/CREDITS-2.x

+5
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ Authors:
1515

1616
Contributors:
1717

18+
# 2.16.0 (not yet released)
19+
20+
WrongWrong (@k163377)
21+
* #682: Remove MissingKotlinParameterException and replace with MismatchedInputException
22+
1823
# 2.15.2
1924

2025
WrongWrong (@k163377)

release-notes/VERSION-2.x

+5-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@ Co-maintainers:
1616
=== Releases ===
1717
------------------------------------------------------------------------
1818

19-
2.16.0 (not yet relesed)
19+
2.16.0 (not yet released)
20+
21+
#682: Remove MissingKotlinParameterException and replace with MismatchedInputException
22+
This change removes MissingKotlinParameterException and resolves #617.
23+
This change is a prerequisite for future work to improve performance.
2024

2125
2.15.2 (30-May-2023)
2226

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

-32
This file was deleted.

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

+11-8
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import com.fasterxml.jackson.databind.deser.ValueInstantiators
99
import com.fasterxml.jackson.databind.deser.impl.NullsAsEmptyProvider
1010
import com.fasterxml.jackson.databind.deser.impl.PropertyValueBuffer
1111
import com.fasterxml.jackson.databind.deser.std.StdValueInstantiator
12+
import com.fasterxml.jackson.databind.exc.MismatchedInputException
1213
import java.lang.reflect.TypeVariable
1314
import kotlin.reflect.KParameter
1415
import kotlin.reflect.KType
@@ -81,10 +82,12 @@ internal class KotlinValueInstantiator(
8182
val isMissingAndRequired = paramVal == null && isMissing && jsonProp.isRequired
8283
if (isMissingAndRequired ||
8384
(!isGenericTypeVar && paramVal == null && !paramDef.type.isMarkedNullable)) {
84-
throw MissingKotlinParameterException(
85-
parameter = paramDef,
86-
processor = ctxt.parser,
87-
msg = "Instantiation of ${this.valueTypeDesc} value failed for JSON property ${jsonProp.name} due to missing (therefore NULL) value for creator parameter ${paramDef.name} which is a non-nullable type"
85+
throw MismatchedInputException.from(
86+
ctxt.parser,
87+
jsonProp.type,
88+
"Instantiation of $valueTypeDesc value failed for JSON property ${jsonProp.name} " +
89+
"due to missing (therefore NULL) value for creator parameter ${paramDef.name} " +
90+
"which is a non-nullable type"
8891
).wrapWithPath(this.valueClass, jsonProp.name)
8992
}
9093

@@ -107,10 +110,10 @@ internal class KotlinValueInstantiator(
107110
}
108111

109112
if (paramType != null && itemType != null) {
110-
throw MissingKotlinParameterException(
111-
parameter = paramDef,
112-
processor = ctxt.parser,
113-
msg = "Instantiation of $itemType $paramType failed for JSON property ${jsonProp.name} due to null value in a $paramType that does not allow null values"
113+
throw MismatchedInputException.from(
114+
ctxt.parser,
115+
jsonProp.type,
116+
"Instantiation of $itemType $paramType failed for JSON property ${jsonProp.name} due to null value in a $paramType that does not allow null values"
114117
).wrapWithPath(this.valueClass, jsonProp.name)
115118
}
116119
}

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

-20
This file was deleted.

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package com.fasterxml.jackson.module.kotlin.test
22

33
import com.fasterxml.jackson.databind.ObjectMapper
4+
import com.fasterxml.jackson.databind.exc.MismatchedInputException
45
import com.fasterxml.jackson.module.kotlin.KotlinFeature.NullIsSameAsDefault
5-
import com.fasterxml.jackson.module.kotlin.MissingKotlinParameterException
66
import com.fasterxml.jackson.module.kotlin.kotlinModule
77
import com.fasterxml.jackson.module.kotlin.readValue
88
import org.junit.Assert
@@ -139,7 +139,7 @@ class TestNullToDefault {
139139
Assert.assertEquals(true, item.canBeProcessed)
140140
}
141141

142-
@Test(expected = MissingKotlinParameterException::class)
142+
@Test(expected = MismatchedInputException::class)
143143
fun shouldThrowExceptionWhenProvidedNullForNotNullFieldWithoutDefault() {
144144
createMapper(true).readValue<TestClass>(
145145
"""{

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

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package com.fasterxml.jackson.module.kotlin.test
22

33
import com.fasterxml.jackson.databind.ObjectMapper
4+
import com.fasterxml.jackson.databind.exc.MismatchedInputException
45
import com.fasterxml.jackson.module.kotlin.KotlinFeature.StrictNullChecks
5-
import com.fasterxml.jackson.module.kotlin.MissingKotlinParameterException
66
import com.fasterxml.jackson.module.kotlin.kotlinModule
77
import com.fasterxml.jackson.module.kotlin.readValue
88
import org.hamcrest.CoreMatchers.equalTo
@@ -27,7 +27,7 @@ class StrictNullChecksTest {
2727

2828
private data class ClassWithListOfInt(val samples: List<Int>)
2929

30-
@Test(expected = MissingKotlinParameterException::class)
30+
@Test(expected = MismatchedInputException::class)
3131
fun testListOfInt() {
3232
val json = """{"samples":[1, null]}"""
3333
mapper.readValue<ClassWithListOfInt>(json)
@@ -55,7 +55,7 @@ class StrictNullChecksTest {
5555

5656
private data class ClassWithArrayOfInt(val samples: Array<Int>)
5757

58-
@Test(expected = MissingKotlinParameterException::class)
58+
@Test(expected = MismatchedInputException::class)
5959
fun testArrayOfInt() {
6060
val json = """{"samples":[1, null]}"""
6161
mapper.readValue<ClassWithArrayOfInt>(json)
@@ -83,7 +83,7 @@ class StrictNullChecksTest {
8383

8484
private data class ClassWithMapOfStringToInt(val samples: Map<String, Int>)
8585

86-
@Test(expected = MissingKotlinParameterException::class)
86+
@Test(expected = MismatchedInputException::class)
8787
fun testMapOfStringToIntWithNullValue() {
8888
val json = """{ "samples": { "key": null } }"""
8989
mapper.readValue<ClassWithMapOfStringToInt>(json)
@@ -110,7 +110,7 @@ class StrictNullChecksTest {
110110
}
111111

112112
@Ignore // this is a hard problem to solve and is currently not addressed
113-
@Test(expected = MissingKotlinParameterException::class)
113+
@Test(expected = MismatchedInputException::class)
114114
fun testListOfGenericWithNullValue() {
115115
val json = """{"samples":[1, null]}"""
116116
mapper.readValue<TestClass<List<Int>>>(json)
@@ -124,7 +124,7 @@ class StrictNullChecksTest {
124124
}
125125

126126
@Ignore // this is a hard problem to solve and is currently not addressed
127-
@Test(expected = MissingKotlinParameterException::class)
127+
@Test(expected = MismatchedInputException::class)
128128
fun testMapOfGenericWithNullValue() {
129129
val json = """{ "samples": { "key": null } }"""
130130
mapper.readValue<TestClass<Map<String, Int>>>(json)
@@ -138,7 +138,7 @@ class StrictNullChecksTest {
138138
}
139139

140140
@Ignore // this is a hard problem to solve and is currently not addressed
141-
@Test(expected = MissingKotlinParameterException::class)
141+
@Test(expected = MismatchedInputException::class)
142142
fun testArrayOfGenericWithNullValue() {
143143
val json = """{"samples":[1, null]}"""
144144
mapper.readValue<TestClass<Array<Int>>>(json)

src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github168.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.fasterxml.jackson.module.kotlin.test.github
22

33
import com.fasterxml.jackson.annotation.JsonProperty
4-
import com.fasterxml.jackson.module.kotlin.MissingKotlinParameterException
4+
import com.fasterxml.jackson.databind.exc.MismatchedInputException
55
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
66
import com.fasterxml.jackson.module.kotlin.readValue
77
import org.junit.Test
@@ -17,7 +17,7 @@ class TestGithub168 {
1717
assertEquals("whatever", obj.baz)
1818
}
1919

20-
@Test(expected = MissingKotlinParameterException::class)
20+
@Test(expected = MismatchedInputException::class)
2121
fun testIfRequiredIsReallyRequiredWhenAbsent() {
2222
val obj = jacksonObjectMapper().readValue<TestClass>("""{"baz":"whatever"}""")
2323
assertEquals("whatever", obj.baz)

src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github32.kt

+12-8
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.fasterxml.jackson.module.kotlin.test.github
22

33
import com.fasterxml.jackson.databind.JsonMappingException
4-
import com.fasterxml.jackson.module.kotlin.MissingKotlinParameterException
4+
import com.fasterxml.jackson.databind.exc.MismatchedInputException
55
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
66
import com.fasterxml.jackson.module.kotlin.readValue
77
import org.hamcrest.CustomTypeSafeMatcher
@@ -106,16 +106,20 @@ private data class Crowd(val people: List<Person>)
106106

107107
private fun missingFirstNameParameter() = missingConstructorParam(::Person.parameters[0])
108108

109-
private fun missingConstructorParam(param: KParameter) = object : CustomTypeSafeMatcher<MissingKotlinParameterException>("MissingKotlinParameterException with missing `${param.name}` parameter") {
110-
override fun matchesSafely(e: MissingKotlinParameterException): Boolean = e.parameter.equals(param)
109+
private fun missingConstructorParam(
110+
param: KParameter
111+
) = object : CustomTypeSafeMatcher<MismatchedInputException>(
112+
"MissingKotlinParameterException with missing `${param.name}` parameter"
113+
) {
114+
override fun matchesSafely(e: MismatchedInputException): Boolean = param.name == e.path.last().fieldName
111115
}
112116

113-
private fun pathMatches(path: String) = object : CustomTypeSafeMatcher<MissingKotlinParameterException>("MissingKotlinParameterException with path `$path`") {
114-
override fun matchesSafely(e: MissingKotlinParameterException): Boolean = e.getHumanReadablePath().equals(path)
117+
private fun pathMatches(path: String) = object : CustomTypeSafeMatcher<MismatchedInputException>("MissingKotlinParameterException with path `$path`") {
118+
override fun matchesSafely(e: MismatchedInputException): Boolean = e.getHumanReadablePath().equals(path)
115119
}
116120

117-
private fun location(line: Int, column: Int) = object : CustomTypeSafeMatcher<MissingKotlinParameterException>("MissingKotlinParameterException with location (line=$line, column=$column)") {
118-
override fun matchesSafely(e: MissingKotlinParameterException): Boolean {
121+
private fun location(line: Int, column: Int) = object : CustomTypeSafeMatcher<MismatchedInputException>("MissingKotlinParameterException with location (line=$line, column=$column)") {
122+
override fun matchesSafely(e: MismatchedInputException): Boolean {
119123
return e.location != null && line.equals(e.location.lineNr) && column.equals(e.location.columnNr)
120124
}
121125
}
@@ -131,4 +135,4 @@ private fun JsonMappingException.getHumanReadablePath(): String {
131135
}
132136
}
133137
return builder.toString()
134-
}
138+
}

0 commit comments

Comments
 (0)