@@ -4,29 +4,47 @@ import com.fasterxml.jackson.databind.DeserializationFeature
44import com.fasterxml.jackson.databind.exc.MismatchedInputException
55import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
66import com.fasterxml.jackson.module.kotlin.readValue
7- import org.junit.jupiter.api.Assertions.assertEquals
7+ import org.junit.jupiter.api.Assertions.assertEquals
88import org.junit.jupiter.api.Assertions.assertThrows
99import org.junit.jupiter.api.Test
1010
1111class 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 ::class .java) {
24+ mapper.readValue<NoDefaultValue >(" {}" )
25+ }
2126
2227 assertThrows(MismatchedInputException ::class .java) {
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 ::class .java) {
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