Skip to content

Commit

Permalink
Merge pull request #720 from k163377/fix/617
Browse files Browse the repository at this point in the history
Revert "Merge pull request #682 from k163377/fix/617" with comment correction.
  • Loading branch information
k163377 authored Nov 3, 2023
2 parents 49369a3 + 865cef2 commit 16e523a
Show file tree
Hide file tree
Showing 10 changed files with 88 additions and 39 deletions.
2 changes: 0 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,6 @@
<!-- public -->
<!-- options added -->
<exclude>com.fasterxml.jackson.module.kotlin.KotlinModule</exclude>
<!-- class removed -->
<exclude>com.fasterxml.jackson.module.kotlin.MissingKotlinParameterException</exclude>
<!-- internal -->
<exclude>com.fasterxml.jackson.module.kotlin.KotlinNamesAnnotationIntrospector</exclude>
<exclude>com.fasterxml.jackson.module.kotlin.KotlinAnnotationIntrospector</exclude>
Expand Down
1 change: 0 additions & 1 deletion release-notes/CREDITS-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ WrongWrong (@k163377)
* #686: Add KotlinPropertyNameAsImplicitName option
* #685: Streamline default value management for KotlinFeatures
* #684: Update Kotlin Version to 1.6
* #682: Remove MissingKotlinParameterException and replace with MismatchedInputException

# 2.15.2

Expand Down
3 changes: 0 additions & 3 deletions release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@ Co-maintainers:
#685: Streamline default value management for KotlinFeatures.
This improves the initialization cost of kotlin-module a little.
#684: Kotlin 1.5 has been deprecated and the minimum supported Kotlin version will be updated to 1.6.
#682: Remove MissingKotlinParameterException and replace with MismatchedInputException
This change removes MissingKotlinParameterException and resolves #617.
This change is a prerequisite for future work to improve performance.

2.15.3 (12-Oct-2023)

Expand Down
41 changes: 41 additions & 0 deletions src/main/kotlin/com/fasterxml/jackson/module/kotlin/Exceptions.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.fasterxml.jackson.module.kotlin

import com.fasterxml.jackson.core.JsonParser
import com.fasterxml.jackson.databind.JsonMappingException
import com.fasterxml.jackson.databind.exc.MismatchedInputException
import java.io.Closeable
import kotlin.reflect.KParameter

/**
* Specialized [JsonMappingException] sub-class used to indicate that a mandatory Kotlin constructor
* parameter was missing or null.
*/
@Deprecated(
"It is recommended that MismatchedInputException be referenced when possible," +
" as the change is discussed for 2.17 and later." +
" See #617 for details.",
ReplaceWith(
"MismatchedInputException",
"com.fasterxml.jackson.databind.exc.MismatchedInputException"
),
DeprecationLevel.WARNING
)
// When deserialized by the JDK, the parameter property will be null, ignoring nullability.
// This is a temporary workaround for #572 and we will eventually remove this class.
class MissingKotlinParameterException(
@property:Deprecated(
"KParameter is not serializable and will be removed in 2.17 or later. See #572 for details.",
level = DeprecationLevel.WARNING
)
@Transient
val parameter: KParameter,
processor: JsonParser? = null,
msg: String
) : MismatchedInputException(processor, msg) {
@Deprecated("Use main constructor", ReplaceWith("MissingKotlinParameterException(KParameter, JsonParser?, String)"))
constructor(
parameter: KParameter,
processor: Closeable? = null,
msg: String
) : this(parameter, processor as JsonParser, msg)
}
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,10 @@ internal class KotlinValueInstantiator(

// Since #310 reported that the calculation cost is high, isGenericTypeVar is determined last.
if (isMissingAndRequired || (!paramType.isMarkedNullable && !paramType.isGenericTypeVar())) {
throw MismatchedInputException.from(
ctxt.parser,
propType,
"Instantiation of $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"
throw MissingKotlinParameterException(
parameter = paramDef,
processor = ctxt.parser,
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"
).wrapWithPath(this.valueClass, jsonProp.name)
}
}
Expand All @@ -129,10 +127,10 @@ internal class KotlinValueInstantiator(
}

if (paramTypeStr != null && itemType != null) {
throw MismatchedInputException.from(
ctxt.parser,
propType,
"Instantiation of $itemType $paramTypeStr failed for JSON property ${jsonProp.name} due to null value in a $paramTypeStr that does not allow null values"
throw MissingKotlinParameterException(
parameter = paramDef,
processor = ctxt.parser,
msg = "Instantiation of $itemType $paramType failed for JSON property ${jsonProp.name} due to null value in a $paramType that does not allow null values"
).wrapWithPath(this.valueClass, jsonProp.name)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.fasterxml.jackson.module.kotlin

import org.junit.Test
import kotlin.test.assertNotNull
import kotlin.test.assertNull

class MissingKotlinParameterExceptionTest {
@Test
fun jdkSerializabilityTest() {
val param = ::MissingKotlinParameterException.parameters.first()
val ex = MissingKotlinParameterException(param, null, "test")

val serialized = jdkSerialize(ex)
val deserialized = jdkDeserialize<MissingKotlinParameterException>(serialized)

assertNotNull(deserialized)
// see comment at MissingKotlinParameterException
assertNull(deserialized.parameter)
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.fasterxml.jackson.module.kotlin.test

import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.databind.exc.MismatchedInputException
import com.fasterxml.jackson.module.kotlin.KotlinFeature.NullIsSameAsDefault
import com.fasterxml.jackson.module.kotlin.MissingKotlinParameterException
import com.fasterxml.jackson.module.kotlin.kotlinModule
import com.fasterxml.jackson.module.kotlin.readValue
import org.junit.Assert
Expand Down Expand Up @@ -139,7 +139,7 @@ class TestNullToDefault {
Assert.assertEquals(true, item.canBeProcessed)
}

@Test(expected = MismatchedInputException::class)
@Test(expected = MissingKotlinParameterException::class)
fun shouldThrowExceptionWhenProvidedNullForNotNullFieldWithoutDefault() {
createMapper(true).readValue<TestClass>(
"""{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.fasterxml.jackson.module.kotlin.test

import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.databind.exc.MismatchedInputException
import com.fasterxml.jackson.module.kotlin.KotlinFeature.StrictNullChecks
import com.fasterxml.jackson.module.kotlin.MissingKotlinParameterException
import com.fasterxml.jackson.module.kotlin.kotlinModule
import com.fasterxml.jackson.module.kotlin.readValue
import org.hamcrest.CoreMatchers.equalTo
Expand All @@ -27,7 +27,7 @@ class StrictNullChecksTest {

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

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

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

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

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

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

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

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

@Ignore // this is a hard problem to solve and is currently not addressed
@Test(expected = MismatchedInputException::class)
@Test(expected = MissingKotlinParameterException::class)
fun testArrayOfGenericWithNullValue() {
val json = """{"samples":[1, null]}"""
mapper.readValue<TestClass<Array<Int>>>(json)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.fasterxml.jackson.module.kotlin.test.github

import com.fasterxml.jackson.annotation.JsonProperty
import com.fasterxml.jackson.databind.exc.MismatchedInputException
import com.fasterxml.jackson.module.kotlin.MissingKotlinParameterException
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import com.fasterxml.jackson.module.kotlin.readValue
import org.junit.Test
Expand All @@ -17,7 +17,7 @@ class TestGithub168 {
assertEquals("whatever", obj.baz)
}

@Test(expected = MismatchedInputException::class)
@Test(expected = MissingKotlinParameterException::class)
fun testIfRequiredIsReallyRequiredWhenAbsent() {
val obj = jacksonObjectMapper().readValue<TestClass>("""{"baz":"whatever"}""")
assertEquals("whatever", obj.baz)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.fasterxml.jackson.module.kotlin.test.github

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

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

private fun missingConstructorParam(
param: KParameter
) = object : CustomTypeSafeMatcher<MismatchedInputException>(
"MissingKotlinParameterException with missing `${param.name}` parameter"
) {
override fun matchesSafely(e: MismatchedInputException): Boolean = param.name == e.path.last().fieldName
private fun missingConstructorParam(param: KParameter) = object : CustomTypeSafeMatcher<MissingKotlinParameterException>("MissingKotlinParameterException with missing `${param.name}` parameter") {
override fun matchesSafely(e: MissingKotlinParameterException): Boolean = e.parameter.equals(param)
}

private fun pathMatches(path: String) = object : CustomTypeSafeMatcher<MismatchedInputException>("MissingKotlinParameterException with path `$path`") {
override fun matchesSafely(e: MismatchedInputException): Boolean = e.getHumanReadablePath().equals(path)
private fun pathMatches(path: String) = object : CustomTypeSafeMatcher<MissingKotlinParameterException>("MissingKotlinParameterException with path `$path`") {
override fun matchesSafely(e: MissingKotlinParameterException): Boolean = e.getHumanReadablePath().equals(path)
}

private fun location(line: Int, column: Int) = object : CustomTypeSafeMatcher<MismatchedInputException>("MissingKotlinParameterException with location (line=$line, column=$column)") {
override fun matchesSafely(e: MismatchedInputException): Boolean {
private fun location(line: Int, column: Int) = object : CustomTypeSafeMatcher<MissingKotlinParameterException>("MissingKotlinParameterException with location (line=$line, column=$column)") {
override fun matchesSafely(e: MissingKotlinParameterException): Boolean {
return e.location != null && line.equals(e.location.lineNr) && column.equals(e.location.columnNr)
}
}
Expand All @@ -135,4 +131,4 @@ private fun JsonMappingException.getHumanReadablePath(): String {
}
}
return builder.toString()
}
}

0 comments on commit 16e523a

Please sign in to comment.