From 9e7d546a6ab543966ab35e3d40c0f09134e6194a Mon Sep 17 00:00:00 2001 From: wrongwrong Date: Sun, 15 Dec 2024 13:05:51 +0900 Subject: [PATCH 1/2] Added test case for FAIL_ON_NULL_FOR_PRIMITIVES --- .../kotlin/test/FailNullForPrimitiveTest.kt | 34 ++++++++++++++----- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/FailNullForPrimitiveTest.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/FailNullForPrimitiveTest.kt index adac2679c..7e97f4304 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/FailNullForPrimitiveTest.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/FailNullForPrimitiveTest.kt @@ -4,29 +4,47 @@ import com.fasterxml.jackson.databind.DeserializationFeature import com.fasterxml.jackson.databind.exc.MismatchedInputException import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper import com.fasterxml.jackson.module.kotlin.readValue - import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Assertions.assertEquals import org.junit.jupiter.api.Assertions.assertThrows import org.junit.jupiter.api.Test class FailNullForPrimitiveTest { - data class Dto( + val mapper = jacksonObjectMapper() + .enable(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES) + + data class NoDefaultValue( val foo: Int, val bar: Int? ) @Test - fun test() { - val mapper = jacksonObjectMapper() - .enable(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES) + fun noDefaultValueTest() { + // If no default value is set, it will fail if undefined or null is entered + assertThrows(MismatchedInputException::class.java) { + mapper.readValue("{}") + } assertThrows(MismatchedInputException::class.java) { - mapper.readValue("{}") + mapper.readValue("""{"foo":null}""") } + assertEquals(NoDefaultValue(0, null), mapper.readValue("""{"foo":0}""")) + } + + data class HasDefaultValue( + val foo: Int = -1, + val bar: Int? = -1 + ) + + @Test + fun hasDefaultValueTest() { + // If a default value is set, an input of undefined will succeed, but null will fail + assertEquals(HasDefaultValue(-1, -1), mapper.readValue("{}")) + assertThrows(MismatchedInputException::class.java) { - mapper.readValue("""{"foo":null}""") + mapper.readValue("""{"foo":null}""") } - assertEquals(Dto(0, null), mapper.readValue("""{"foo":0}""")) + assertEquals(HasDefaultValue(0, null), mapper.readValue("""{"foo":0, "bar":null}""")) } } From a1ace2fa6d26afe44fab45b70f8e2beb3f532200 Mon Sep 17 00:00:00 2001 From: wrongwrong Date: Sun, 15 Dec 2024 13:08:32 +0900 Subject: [PATCH 2/2] Update release notes wrt #868 --- release-notes/CREDITS-2.x | 1 + 1 file changed, 1 insertion(+) diff --git a/release-notes/CREDITS-2.x b/release-notes/CREDITS-2.x index 4aeb44a85..5013320f6 100644 --- a/release-notes/CREDITS-2.x +++ b/release-notes/CREDITS-2.x @@ -18,6 +18,7 @@ Contributors: # 2.19.0 (not yet released) WrongWrong (@k163377) +* #868: Added test case for FAIL_ON_NULL_FOR_PRIMITIVES * #866: Upgrade to JUnit5 * #861: Update Kotlin to 1.9.24 * #858: Refactor findDefaultCreator