Skip to content

Commit 96b9e4a

Browse files
committed
Added test case for FAIL_ON_NULL_FOR_PRIMITIVES
from FasterXML/jackson-module-kotlin#868
1 parent c405372 commit 96b9e4a

File tree

1 file changed

+25
-7
lines changed

1 file changed

+25
-7
lines changed

src/test/kotlin/io/github/projectmapk/jackson/module/kogera/zIntegration/deser/FailNullForPrimitiveTest.kt

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,42 @@ import org.junit.jupiter.api.Test
99
import org.junit.jupiter.api.assertThrows
1010

1111
private class FailNullForPrimitiveTest {
12-
data class Dto(
12+
val mapper = jacksonObjectMapper()
13+
.enable(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES)
14+
15+
data class NoDefaultValue(
1316
val foo: Int,
1417
val bar: Int?
1518
)
1619

1720
@Test
18-
fun test() {
19-
val mapper = jacksonObjectMapper()
20-
.enable(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES)
21+
fun noDefaultValueTest() {
22+
// If no default value is set, it will fail if undefined or null is entered
23+
assertThrows<MismatchedInputException> {
24+
mapper.readValue<NoDefaultValue>("{}")
25+
}
2126

2227
assertThrows<MismatchedInputException> {
23-
mapper.readValue<Dto>("{}")
28+
mapper.readValue<NoDefaultValue>("""{"foo":null}""")
2429
}
2530

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

30-
assertEquals(Dto(0, null), mapper.readValue<Dto>("""{"foo":0}"""))
48+
assertEquals(HasDefaultValue(0, null), mapper.readValue<HasDefaultValue>("""{"foo":0, "bar":null}"""))
3149
}
3250
}

0 commit comments

Comments
 (0)