Skip to content

Commit e9463e9

Browse files
committed
Revert "Merge pull request #682 from k163377/fix/617"
This reverts commit 43f01ea, reversing changes made to 25a54ff.
1 parent 49369a3 commit e9463e9

File tree

9 files changed

+79
-37
lines changed

9 files changed

+79
-37
lines changed

release-notes/CREDITS-2.x

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ WrongWrong (@k163377)
2929
* #686: Add KotlinPropertyNameAsImplicitName option
3030
* #685: Streamline default value management for KotlinFeatures
3131
* #684: Update Kotlin Version to 1.6
32-
* #682: Remove MissingKotlinParameterException and replace with MismatchedInputException
3332

3433
# 2.15.2
3534

release-notes/VERSION-2.x

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,6 @@ Co-maintainers:
3232
#685: Streamline default value management for KotlinFeatures.
3333
This improves the initialization cost of kotlin-module a little.
3434
#684: Kotlin 1.5 has been deprecated and the minimum supported Kotlin version will be updated to 1.6.
35-
#682: Remove MissingKotlinParameterException and replace with MismatchedInputException
36-
This change removes MissingKotlinParameterException and resolves #617.
37-
This change is a prerequisite for future work to improve performance.
3835

3936
2.15.3 (12-Oct-2023)
4037

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.fasterxml.jackson.module.kotlin
2+
3+
import com.fasterxml.jackson.core.JsonParser
4+
import com.fasterxml.jackson.databind.JsonMappingException
5+
import com.fasterxml.jackson.databind.exc.MismatchedInputException
6+
import java.io.Closeable
7+
import kotlin.reflect.KParameter
8+
9+
/**
10+
* Specialized [JsonMappingException] sub-class used to indicate that a mandatory Kotlin constructor
11+
* parameter was missing or null.
12+
*/
13+
@Deprecated(
14+
"It will be removed in jackson-module-kotlin 2.16. See #617 for details.",
15+
ReplaceWith(
16+
"MismatchedInputException",
17+
"com.fasterxml.jackson.databind.exc.MismatchedInputException"
18+
),
19+
DeprecationLevel.WARNING
20+
)
21+
// When deserialized by the JDK, the parameter property will be null, ignoring nullability.
22+
// This is a temporary workaround for #572 and we will eventually remove this class.
23+
class MissingKotlinParameterException(@Transient val parameter: KParameter,
24+
processor: JsonParser? = null,
25+
msg: String) : MismatchedInputException(processor, msg) {
26+
@Deprecated("Use main constructor", ReplaceWith("MissingKotlinParameterException(KParameter, JsonParser?, String)"))
27+
constructor(
28+
parameter: KParameter,
29+
processor: Closeable? = null,
30+
msg: String
31+
) : this(parameter, processor as JsonParser, msg)
32+
}

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

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,10 @@ internal class KotlinValueInstantiator(
9898

9999
// Since #310 reported that the calculation cost is high, isGenericTypeVar is determined last.
100100
if (isMissingAndRequired || (!paramType.isMarkedNullable && !paramType.isGenericTypeVar())) {
101-
throw MismatchedInputException.from(
102-
ctxt.parser,
103-
propType,
104-
"Instantiation of $valueTypeDesc value failed for JSON property ${jsonProp.name} " +
105-
"due to missing (therefore NULL) value for creator parameter ${paramDef.name} " +
106-
"which is a non-nullable type"
101+
throw MissingKotlinParameterException(
102+
parameter = paramDef,
103+
processor = ctxt.parser,
104+
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"
107105
).wrapWithPath(this.valueClass, jsonProp.name)
108106
}
109107
}
@@ -129,10 +127,10 @@ internal class KotlinValueInstantiator(
129127
}
130128

131129
if (paramTypeStr != null && itemType != null) {
132-
throw MismatchedInputException.from(
133-
ctxt.parser,
134-
propType,
135-
"Instantiation of $itemType $paramTypeStr failed for JSON property ${jsonProp.name} due to null value in a $paramTypeStr that does not allow null values"
130+
throw MissingKotlinParameterException(
131+
parameter = paramDef,
132+
processor = ctxt.parser,
133+
msg = "Instantiation of $itemType $paramType failed for JSON property ${jsonProp.name} due to null value in a $paramType that does not allow null values"
136134
).wrapWithPath(this.valueClass, jsonProp.name)
137135
}
138136
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.fasterxml.jackson.module.kotlin
2+
3+
import org.junit.Test
4+
import kotlin.test.assertNotNull
5+
import kotlin.test.assertNull
6+
7+
class MissingKotlinParameterExceptionTest {
8+
@Test
9+
fun jdkSerializabilityTest() {
10+
val param = ::MissingKotlinParameterException.parameters.first()
11+
val ex = MissingKotlinParameterException(param, null, "test")
12+
13+
val serialized = jdkSerialize(ex)
14+
val deserialized = jdkDeserialize<MissingKotlinParameterException>(serialized)
15+
16+
assertNotNull(deserialized)
17+
// see comment at MissingKotlinParameterException
18+
assertNull(deserialized.parameter)
19+
}
20+
}

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

Lines changed: 2 additions & 2 deletions
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
54
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 = MismatchedInputException::class)
142+
@Test(expected = MissingKotlinParameterException::class)
143143
fun shouldThrowExceptionWhenProvidedNullForNotNullFieldWithoutDefault() {
144144
createMapper(true).readValue<TestClass>(
145145
"""{

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

Lines changed: 7 additions & 7 deletions
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
54
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 = MismatchedInputException::class)
30+
@Test(expected = MissingKotlinParameterException::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 = MismatchedInputException::class)
58+
@Test(expected = MissingKotlinParameterException::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 = MismatchedInputException::class)
86+
@Test(expected = MissingKotlinParameterException::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 = MismatchedInputException::class)
113+
@Test(expected = MissingKotlinParameterException::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 = MismatchedInputException::class)
127+
@Test(expected = MissingKotlinParameterException::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 = MismatchedInputException::class)
141+
@Test(expected = MissingKotlinParameterException::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

Lines changed: 2 additions & 2 deletions
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.databind.exc.MismatchedInputException
4+
import com.fasterxml.jackson.module.kotlin.MissingKotlinParameterException
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 = MismatchedInputException::class)
20+
@Test(expected = MissingKotlinParameterException::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

Lines changed: 8 additions & 12 deletions
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.databind.exc.MismatchedInputException
4+
import com.fasterxml.jackson.module.kotlin.MissingKotlinParameterException
55
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
66
import com.fasterxml.jackson.module.kotlin.readValue
77
import org.hamcrest.CustomTypeSafeMatcher
@@ -106,20 +106,16 @@ private data class Crowd(val people: List<Person>)
106106

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

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
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)
115111
}
116112

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

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 {
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 {
123119
return e.location != null && line.equals(e.location.lineNr) && column.equals(e.location.columnNr)
124120
}
125121
}
@@ -135,4 +131,4 @@ private fun JsonMappingException.getHumanReadablePath(): String {
135131
}
136132
}
137133
return builder.toString()
138-
}
134+
}

0 commit comments

Comments
 (0)