@@ -4,30 +4,48 @@ import tools.jackson.databind.DeserializationFeature
44import tools.jackson.databind.exc.MismatchedInputException
55import tools.jackson.module.kotlin.jacksonMapperBuilder
66import tools.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 = 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