Skip to content

Commit

Permalink
Merge remote-tracking branch 'FasterXML/2.16'
Browse files Browse the repository at this point in the history
  • Loading branch information
k163377 committed Jul 8, 2023
2 parents bc23427 + 43f01ea commit 01082da
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 78 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
31 changes: 0 additions & 31 deletions src/main/kotlin/tools/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 tools.jackson.databind.deser.ValueInstantiators
import tools.jackson.databind.deser.bean.PropertyValueBuffer
import tools.jackson.databind.deser.impl.NullsAsEmptyProvider
import tools.jackson.databind.deser.std.StdValueInstantiator
import tools.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 tools.jackson.module.kotlin.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 tools.jackson.module.kotlin.test

import tools.jackson.databind.json.JsonMapper
import tools.jackson.databind.exc.MismatchedInputException
import tools.jackson.module.kotlin.KotlinFeature.NullIsSameAsDefault
import tools.jackson.module.kotlin.MissingKotlinParameterException
import tools.jackson.module.kotlin.kotlinModule
import tools.jackson.module.kotlin.readValue
import org.junit.Assert
Expand Down Expand Up @@ -144,7 +144,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 tools.jackson.module.kotlin.test

import tools.jackson.databind.exc.MismatchedInputException
import tools.jackson.databind.json.JsonMapper
import tools.jackson.module.kotlin.KotlinFeature.StrictNullChecks
import tools.jackson.module.kotlin.MissingKotlinParameterException
import tools.jackson.module.kotlin.kotlinModule
import tools.jackson.module.kotlin.readValue
import org.hamcrest.CoreMatchers.equalTo
Expand All @@ -29,7 +29,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 @@ -57,7 +57,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 @@ -85,7 +85,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 @@ -112,7 +112,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 @@ -126,7 +126,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 @@ -140,7 +140,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
Expand Up @@ -2,7 +2,7 @@ package tools.jackson.module.kotlin.test.github

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

@Test(expected = tools.jackson.module.kotlin.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 tools.jackson.module.kotlin.test.github

import tools.jackson.databind.DatabindException
import tools.jackson.module.kotlin.MissingKotlinParameterException
import tools.jackson.databind.exc.MismatchedInputException
import tools.jackson.module.kotlin.jacksonObjectMapper
import tools.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<tools.jackson.module.kotlin.MissingKotlinParameterException>("MissingKotlinParameterException with missing `${param.name}` parameter") {
override fun matchesSafely(e: tools.jackson.module.kotlin.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().propertyName
}

private fun pathMatches(path: String) = object : CustomTypeSafeMatcher<tools.jackson.module.kotlin.MissingKotlinParameterException>("MissingKotlinParameterException with path `$path`") {
override fun matchesSafely(e: tools.jackson.module.kotlin.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<tools.jackson.module.kotlin.MissingKotlinParameterException>("MissingKotlinParameterException with location (line=$line, column=$column)") {
override fun matchesSafely(e: tools.jackson.module.kotlin.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 Down

0 comments on commit 01082da

Please sign in to comment.