1
+ package com.fasterxml.jackson.module.kotlin.kogeraIntegration.deser
2
+
3
+ import com.fasterxml.jackson.annotation.JsonSetter
4
+ import com.fasterxml.jackson.annotation.Nulls
5
+ import com.fasterxml.jackson.databind.ObjectMapper
6
+ import com.fasterxml.jackson.databind.exc.InvalidNullException
7
+ import com.fasterxml.jackson.module.kotlin.KotlinFeature
8
+ import com.fasterxml.jackson.module.kotlin.KotlinModule
9
+ import com.fasterxml.jackson.module.kotlin.readValue
10
+ import org.junit.jupiter.api.Assertions
11
+ import org.junit.jupiter.api.Nested
12
+ import org.junit.jupiter.api.Test
13
+ import org.junit.jupiter.api.assertThrows
14
+
15
+ class StrictNullChecksTest {
16
+ val mapper: ObjectMapper = ObjectMapper ()
17
+ .registerModule(
18
+ KotlinModule .Builder ()
19
+ .enable(KotlinFeature .NewStrictNullChecks )
20
+ .build()
21
+ )
22
+
23
+ class ArrayWrapper (val value : Array <Int >)
24
+ data class ListWrapper (val value : List <Int >)
25
+ data class MapWrapper (val value : Map <String , Int >)
26
+
27
+ @Nested
28
+ inner class NonNullInput {
29
+ @Test
30
+ fun array () {
31
+ val expected = ArrayWrapper (arrayOf(1 ))
32
+ val src = mapper.writeValueAsString(expected)
33
+ val result = mapper.readValue<ArrayWrapper >(src)
34
+
35
+ Assertions .assertArrayEquals(expected.value, result.value)
36
+ }
37
+
38
+ @Test
39
+ fun list () {
40
+ val expected = ListWrapper (listOf (1 ))
41
+ val src = mapper.writeValueAsString(expected)
42
+ val result = mapper.readValue<ListWrapper >(src)
43
+
44
+ Assertions .assertEquals(expected, result)
45
+ }
46
+
47
+ @Test
48
+ fun map () {
49
+ val expected = MapWrapper (mapOf (" foo" to 1 ))
50
+ val src = mapper.writeValueAsString(expected)
51
+ val result = mapper.readValue<MapWrapper >(src)
52
+
53
+ Assertions .assertEquals(expected, result)
54
+ }
55
+ }
56
+
57
+ data class AnyWrapper (val value : Any )
58
+
59
+ @Nested
60
+ inner class NullInput {
61
+ @Test
62
+ fun array () {
63
+ val src = mapper.writeValueAsString(AnyWrapper (arrayOf<Int ?>(null )))
64
+ assertThrows<InvalidNullException > { mapper.readValue<ArrayWrapper >(src) }
65
+ }
66
+
67
+ @Test
68
+ fun list () {
69
+ val src = mapper.writeValueAsString(AnyWrapper (arrayOf<Int ?>(null )))
70
+ assertThrows<InvalidNullException > { mapper.readValue<ListWrapper >(src) }
71
+ }
72
+
73
+ @Test
74
+ fun map () {
75
+ val src = mapper.writeValueAsString(AnyWrapper (mapOf (" foo" to null )))
76
+ assertThrows<InvalidNullException > { mapper.readValue<MapWrapper >(src) }
77
+ }
78
+ }
79
+
80
+ class ContentNullsSkipArrayWrapper (@JsonSetter(contentNulls = Nulls .SKIP ) val value : Array <Int >)
81
+ data class ContentNullsSkipListWrapper (@JsonSetter(contentNulls = Nulls .SKIP ) val value : List <Int >)
82
+ data class ContentNullsSkipMapWrapper (@JsonSetter(contentNulls = Nulls .SKIP ) val value : Map <String , Int >)
83
+
84
+ @Nested
85
+ inner class CustomByAnnotationTest {
86
+ @Test
87
+ fun array () {
88
+ val expected = ContentNullsSkipArrayWrapper (emptyArray())
89
+ val src = mapper.writeValueAsString(AnyWrapper (arrayOf<Int ?>(null )))
90
+ val result = mapper.readValue<ContentNullsSkipArrayWrapper >(src)
91
+
92
+ Assertions .assertArrayEquals(expected.value, result.value)
93
+ }
94
+
95
+ @Test
96
+ fun list () {
97
+ val expected = ContentNullsSkipListWrapper (emptyList())
98
+ val src = mapper.writeValueAsString(AnyWrapper (listOf<Int ?>(null )))
99
+ val result = mapper.readValue<ContentNullsSkipListWrapper >(src)
100
+
101
+ Assertions .assertEquals(expected, result)
102
+ }
103
+
104
+ @Test
105
+ fun map () {
106
+ val expected = ContentNullsSkipMapWrapper (emptyMap())
107
+ val src = mapper.writeValueAsString(AnyWrapper (mapOf (" foo" to null )))
108
+ val result = mapper.readValue<ContentNullsSkipMapWrapper >(src)
109
+
110
+ Assertions .assertEquals(expected, result)
111
+ }
112
+ }
113
+
114
+ class AnnotatedArrayWrapper (@JsonSetter(nulls = Nulls .SKIP ) val value : Array <Int > = emptyArray())
115
+ data class AnnotatedListWrapper (@JsonSetter(nulls = Nulls .SKIP ) val value : List <Int > = emptyList())
116
+ data class AnnotatedMapWrapper (@JsonSetter(nulls = Nulls .SKIP ) val value : Map <String , Int > = emptyMap())
117
+
118
+ // If Default is specified by annotation, it is not overridden.
119
+ @Nested
120
+ inner class AnnotatedNullInput {
121
+ @Test
122
+ fun array () {
123
+ val src = mapper.writeValueAsString(AnyWrapper (arrayOf<Int ?>(null )))
124
+ assertThrows<InvalidNullException > { mapper.readValue<AnnotatedArrayWrapper >(src) }
125
+ }
126
+
127
+ @Test
128
+ fun list () {
129
+ val src = mapper.writeValueAsString(AnyWrapper (arrayOf<Int ?>(null )))
130
+ assertThrows<InvalidNullException > { mapper.readValue<AnnotatedListWrapper >(src) }
131
+ }
132
+
133
+ @Test
134
+ fun map () {
135
+ val src = mapper.writeValueAsString(AnyWrapper (mapOf (" foo" to null )))
136
+ assertThrows<InvalidNullException > { mapper.readValue<AnnotatedMapWrapper >(src) }
137
+ }
138
+ }
139
+ }
0 commit comments