Skip to content

Commit

Permalink
Merge pull request #682 from k163377/fix/617
Browse files Browse the repository at this point in the history
Remove MissingKotlinParameterException and replace with MismatchedInputException
  • Loading branch information
k163377 committed Jul 8, 2023
2 parents 25a54ff + a90463c commit 43f01ea
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 80 deletions.
5 changes: 5 additions & 0 deletions release-notes/CREDITS-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ Authors:

Contributors:

# 2.16.0 (not yet released)

WrongWrong (@k163377)
* #682: Remove MissingKotlinParameterException and replace with MismatchedInputException

# 2.15.2

WrongWrong (@k163377)
Expand Down
6 changes: 5 additions & 1 deletion release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ Co-maintainers:
=== Releases ===
------------------------------------------------------------------------

2.16.0 (not yet relesed)
2.16.0 (not yet released)

#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.2 (30-May-2023)

Expand Down
32 changes: 0 additions & 32 deletions src/main/kotlin/com/fasterxml/jackson/module/kotlin/Exceptions.kt

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import com.fasterxml.jackson.databind.deser.ValueInstantiators
import com.fasterxml.jackson.databind.deser.impl.NullsAsEmptyProvider
import com.fasterxml.jackson.databind.deser.impl.PropertyValueBuffer
import com.fasterxml.jackson.databind.deser.std.StdValueInstantiator
import com.fasterxml.jackson.databind.exc.MismatchedInputException
import java.lang.reflect.TypeVariable
import kotlin.reflect.KParameter
import kotlin.reflect.KType
Expand Down Expand Up @@ -81,10 +82,12 @@ internal class KotlinValueInstantiator(
val isMissingAndRequired = paramVal == null && isMissing && jsonProp.isRequired
if (isMissingAndRequired ||
(!isGenericTypeVar && paramVal == null && !paramDef.type.isMarkedNullable)) {
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"
throw MismatchedInputException.from(
ctxt.parser,
jsonProp.type,
"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"
).wrapWithPath(this.valueClass, jsonProp.name)
}

Expand All @@ -107,10 +110,10 @@ internal class KotlinValueInstantiator(
}

if (paramType != null && itemType != null) {
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"
throw MismatchedInputException.from(
ctxt.parser,
jsonProp.type,
"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

This file was deleted.

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

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

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

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

0 comments on commit 43f01ea

Please sign in to comment.