Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove MissingKotlinParameterException and replace with MismatchedInputException #682

Merged
merged 3 commits into from
Jul 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
}
}