Skip to content

Commit 919568c

Browse files
committed
Merge remote-tracking branch 'FasterXML/2.19'
2 parents 27b9977 + d94e3d9 commit 919568c

File tree

2 files changed

+28
-9
lines changed

2 files changed

+28
-9
lines changed

release-notes/CREDITS-2.x

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Contributors:
1818
# 2.19.0 (not yet released)
1919

2020
WrongWrong (@k163377)
21+
* #868: Added test case for FAIL_ON_NULL_FOR_PRIMITIVES
2122
* #866: Upgrade to JUnit5
2223
* #861: Update Kotlin to 1.9.24
2324
* #858: Refactor findDefaultCreator

src/test/kotlin/tools/jackson/module/kotlin/test/FailNullForPrimitiveTest.kt

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,48 @@ import tools.jackson.databind.DeserializationFeature
44
import tools.jackson.databind.exc.MismatchedInputException
55
import tools.jackson.module.kotlin.jacksonMapperBuilder
66
import tools.jackson.module.kotlin.readValue
7-
import org.junit.jupiter.api.Assertions.assertEquals
7+
import org.junit.jupiter.api.Assertions.assertEquals
88
import org.junit.jupiter.api.Assertions.assertThrows
99
import org.junit.jupiter.api.Test
1010

1111
class FailNullForPrimitiveTest {
12-
data class Dto(
12+
val mapper = jacksonMapperBuilder()
13+
.configure(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES, true)
14+
.build()
15+
16+
data class NoDefaultValue(
1317
val foo: Int,
1418
val bar: Int?
1519
)
1620

1721
@Test
18-
fun test() {
19-
val mapper = jacksonMapperBuilder()
20-
.configure(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES, true)
21-
.build()
22+
fun noDefaultValueTest() {
23+
// If no default value is set, it will fail if undefined or null is entered
24+
assertThrows(MismatchedInputException::class.java) {
25+
mapper.readValue<NoDefaultValue>("{}")
26+
}
2227

2328
assertThrows(MismatchedInputException::class.java) {
24-
mapper.readValue<Dto>("{}")
29+
mapper.readValue<NoDefaultValue>("""{"foo":null}""")
2530
}
2631

32+
assertEquals(NoDefaultValue(0, null), mapper.readValue<NoDefaultValue>("""{"foo":0}"""))
33+
}
34+
35+
data class HasDefaultValue(
36+
val foo: Int = -1,
37+
val bar: Int? = -1
38+
)
39+
40+
@Test
41+
fun hasDefaultValueTest() {
42+
// If a default value is set, an input of undefined will succeed, but null will fail
43+
assertEquals(HasDefaultValue(-1, -1), mapper.readValue<HasDefaultValue>("{}"))
44+
2745
assertThrows(MismatchedInputException::class.java) {
28-
mapper.readValue<Dto>("""{"foo":null}""")
46+
mapper.readValue<HasDefaultValue>("""{"foo":null}""")
2947
}
3048

31-
assertEquals(Dto(0, null), mapper.readValue<Dto>("""{"foo":0}"""))
49+
assertEquals(HasDefaultValue(0, null), mapper.readValue<HasDefaultValue>("""{"foo":0, "bar":null}"""))
3250
}
3351
}

0 commit comments

Comments
 (0)