@@ -4,29 +4,47 @@ import com.fasterxml.jackson.databind.DeserializationFeature
4
4
import com.fasterxml.jackson.databind.exc.MismatchedInputException
5
5
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
6
6
import com.fasterxml.jackson.module.kotlin.readValue
7
- import org.junit.jupiter.api.Assertions.assertEquals
7
+ import org.junit.jupiter.api.Assertions.assertEquals
8
8
import org.junit.jupiter.api.Assertions.assertThrows
9
9
import org.junit.jupiter.api.Test
10
10
11
11
class FailNullForPrimitiveTest {
12
- data class Dto (
12
+ val mapper = jacksonObjectMapper()
13
+ .enable(DeserializationFeature .FAIL_ON_NULL_FOR_PRIMITIVES )
14
+
15
+ data class NoDefaultValue (
13
16
val foo : Int ,
14
17
val bar : Int?
15
18
)
16
19
17
20
@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
+ }
21
26
22
27
assertThrows(MismatchedInputException ::class .java) {
23
- mapper.readValue<Dto >(" {} " )
28
+ mapper.readValue<NoDefaultValue >(""" {"foo":null} "" " )
24
29
}
25
30
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
+
26
44
assertThrows(MismatchedInputException ::class .java) {
27
- mapper.readValue<Dto >(""" {"foo":null}""" )
45
+ mapper.readValue<HasDefaultValue >(""" {"foo":null}""" )
28
46
}
29
47
30
- assertEquals(Dto (0 , null ), mapper.readValue<Dto >(""" {"foo":0}""" ))
48
+ assertEquals(HasDefaultValue (0 , null ), mapper.readValue<HasDefaultValue >(""" {"foo":0, "bar":null }""" ))
31
49
}
32
50
}
0 commit comments