@@ -2,102 +2,233 @@ package com.fasterxml.jackson.module.kotlin.test.github
2
2
3
3
import com.fasterxml.jackson.annotation.JsonValue
4
4
import com.fasterxml.jackson.module.kotlin.jacksonMapperBuilder
5
- import org.junit.Test
6
5
import kotlin.test.assertEquals
6
+ import org.junit.Test
7
7
8
8
class GitHub530 {
9
9
// At the moment, the output is the same with or without `JsonValue`,
10
10
// but this pattern is included in the test case in case the option to default to a serialization method that
11
11
// does not `unbox` is introduced in the future.
12
- @JvmInline value class Foo (@get:JsonValue val value : Int )
13
- @JvmInline value class Bar (@JvmField @field :JsonValue val value : Int )
12
+ @JvmInline
13
+ value class ValueParamGetterAnnotated (@get :JsonValue val value : Int )
14
14
15
- @JvmInline value class Baz (val value : Int ) {
16
- @get:JsonValue val jsonValue: String get() = this .toString()
15
+ @JvmInline
16
+ value class ValueParamFieldAnnotated (@JvmField @field:JsonValue val value : Int )
17
+
18
+ @JvmInline
19
+ value class PropertyWithOverriddenGetter (val value : Int ) {
20
+ @get:JsonValue
21
+ val jsonValue: String
22
+ get() = this .toString()
17
23
}
18
- @JvmInline value class Qux (val value : Int ) {
19
- @JsonValue fun getJsonValue (): String = this .toString()
24
+
25
+ @JvmInline
26
+ value class DirectlyOverriddenGetter (val value : Int ) {
27
+ @JsonValue
28
+ fun getJsonValue (): String = this .toString()
20
29
}
21
30
22
- interface JsonValueGetter { @get:JsonValue val jsonValue: String get() = this .toString() }
23
- @JvmInline value class Quux (val value : Int ): JsonValueGetter
24
- @JvmInline value class Corge (val value : Int ): JsonValueGetter
31
+ interface JsonValueGetter {
32
+ @get:JsonValue
33
+ val jsonValue: String
34
+ get() = this .toString()
35
+ }
36
+
37
+ @JvmInline
38
+ value class JsonValueGetterImplementation (val value : Int ) : JsonValueGetter
39
+
40
+ private val writer = jacksonMapperBuilder().build().writerWithDefaultPrettyPrinter()
25
41
26
- @JvmInline value class Grault (val value : Int ) {
27
- @get:JsonValue val jsonValue: String get() = this .toString()
42
+ @Test
43
+ fun valueParamGetterAnnotated () {
44
+ data class Data (
45
+ val nonNull : ValueParamGetterAnnotated ,
46
+ val nullable : ValueParamGetterAnnotated ?
47
+ )
48
+
49
+ assertEquals(
50
+ """
51
+ {
52
+ "nonNull" : 0,
53
+ "nullable" : 1
54
+ }
55
+ """ .trimIndent(),
56
+ writer.writeValueAsString(
57
+ Data (
58
+ ValueParamGetterAnnotated (0 ),
59
+ ValueParamGetterAnnotated (1 )
60
+ )
61
+ )
62
+ )
28
63
}
29
64
30
- data class Data <T : Any >(
31
- val foo1 : Foo ,
32
- val foo2 : Foo ? ,
33
- val bar1 : Bar ,
34
- val bar2 : Bar ? ,
35
- val baz1 : Baz ,
36
- val baz2 : Baz ? ,
37
- val qux1 : Qux ,
38
- val qux2 : Qux ? ,
39
- val quux1 : Quux ,
40
- val quux2 : Quux ? ,
41
- val corge1 : JsonValueGetter ,
42
- val corge2 : JsonValueGetter ? ,
43
- val grault1 : T ,
44
- val grault2 : T ?
45
- )
65
+ @Test
66
+ fun valueParamFieldAnnoated () {
67
+ data class Data (
68
+ val nonNull : ValueParamFieldAnnotated ,
69
+ val nullable : ValueParamFieldAnnotated ?
70
+ )
71
+
72
+ assertEquals(
73
+ """
74
+ {
75
+ "nonNull" : 0,
76
+ "nullable" : 1
77
+ }
78
+ """ .trimIndent(),
79
+ writer.writeValueAsString(
80
+ Data (
81
+ ValueParamFieldAnnotated (0 ),
82
+ ValueParamFieldAnnotated (1 )
83
+ )
84
+ )
85
+ )
86
+ }
46
87
47
88
@Test
48
- fun inProperty () {
49
- val writer = jacksonMapperBuilder().build().writerWithDefaultPrettyPrinter()
89
+ fun propertyWithOverriddenGetter () {
90
+ data class Data (
91
+ val nonNull : PropertyWithOverriddenGetter ,
92
+ val nullable : PropertyWithOverriddenGetter ?
93
+ )
50
94
51
95
assertEquals(
52
96
"""
53
97
{
54
- "foo1" : 0,
55
- "foo2" : 1,
56
- "bar1" : 2,
57
- "bar2" : 3,
58
- "baz1" : "Baz(value=4)",
59
- "baz2" : "Baz(value=5)",
60
- "qux1" : "Qux(value=6)",
61
- "qux2" : "Qux(value=7)",
62
- "quux1" : "Quux(value=8)",
63
- "quux2" : "Quux(value=9)",
64
- "corge1" : "Corge(value=10)",
65
- "corge2" : "Corge(value=11)",
66
- "grault1" : "Grault(value=12)",
67
- "grault2" : "Grault(value=13)"
98
+ "nonNull" : "PropertyWithOverriddenGetter(value=0)",
99
+ "nullable" : "PropertyWithOverriddenGetter(value=1)"
68
100
}
69
101
""" .trimIndent(),
70
102
writer.writeValueAsString(
71
103
Data (
72
- Foo (0 ), Foo (1 ),
73
- Bar (2 ), Bar (3 ),
74
- Baz (4 ), Baz (5 ),
75
- Qux (6 ), Qux (7 ),
76
- Quux (8 ), Quux (9 ),
77
- Corge (10 ), Corge (11 ),
78
- Grault (12 ), Grault (13 )
104
+ PropertyWithOverriddenGetter (0 ),
105
+ PropertyWithOverriddenGetter (1 )
79
106
)
80
107
)
81
108
)
82
109
}
83
110
84
111
@Test
85
- fun inCollection () {
86
- val writer = jacksonMapperBuilder().build().writerWithDefaultPrettyPrinter()
112
+ fun directlyOverriddenGetter () {
113
+ data class Data (
114
+ val nonNull : DirectlyOverriddenGetter ,
115
+ val nullable : DirectlyOverriddenGetter ?
116
+ )
87
117
88
118
assertEquals(
89
- " [ 0, 1, \" Baz(value=2)\" , \" Qux(value=3)\" , \" Quux(value=4)\" ]" ,
90
- writer.writeValueAsString(listOf (Foo (0 ), Bar (1 ), Baz (2 ), Qux (3 ), Quux (4 )))
119
+ """
120
+ {
121
+ "nonNull" : "DirectlyOverriddenGetter(value=0)",
122
+ "nullable" : "DirectlyOverriddenGetter(value=1)"
123
+ }
124
+ """ .trimIndent(),
125
+ writer.writeValueAsString(
126
+ Data (
127
+ DirectlyOverriddenGetter (0 ),
128
+ DirectlyOverriddenGetter (1 )
129
+ )
130
+ )
91
131
)
92
132
}
93
133
94
134
@Test
95
- fun inArray () {
96
- val writer = jacksonMapperBuilder().build().writerWithDefaultPrettyPrinter()
135
+ fun propertyWithOverriddenGetterAsParameterizedType () {
136
+ data class Data <T : Any >(
137
+ val nonNull : T ,
138
+ val nullable : T ?
139
+ )
97
140
98
141
assertEquals(
99
- " [ 0, 1, \" Baz(value=2)\" , \" Qux(value=3)\" , \" Quux(value=4)\" ]" ,
100
- writer.writeValueAsString(arrayOf(Foo (0 ), Bar (1 ), Baz (2 ), Qux (3 ), Quux (4 )))
142
+ """
143
+ {
144
+ "nonNull" : "PropertyWithOverriddenGetter(value=0)",
145
+ "nullable" : "PropertyWithOverriddenGetter(value=1)"
146
+ }
147
+ """ .trimIndent(),
148
+ writer.writeValueAsString(
149
+ Data (
150
+ PropertyWithOverriddenGetter (0 ),
151
+ PropertyWithOverriddenGetter (1 )
152
+ )
153
+ )
154
+ )
155
+ }
156
+
157
+ @Test
158
+ fun jsonValueGetterImplementationAsConcreteType () {
159
+ data class Data (
160
+ val nonNull : JsonValueGetterImplementation ,
161
+ val nullable : JsonValueGetterImplementation ?
162
+ )
163
+
164
+ assertEquals(
165
+ """
166
+ {
167
+ "nonNull" : "JsonValueGetterImplementation(value=0)",
168
+ "nullable" : "JsonValueGetterImplementation(value=1)"
169
+ }
170
+ """ .trimIndent(),
171
+ writer.writeValueAsString(
172
+ Data (
173
+ JsonValueGetterImplementation (0 ),
174
+ JsonValueGetterImplementation (1 )
175
+ )
176
+ )
177
+ )
178
+ }
179
+
180
+ @Test
181
+ fun jsonValueGetterImplementationAsGenericType () {
182
+ data class Data (
183
+ val nonNull : JsonValueGetter ,
184
+ val nullable : JsonValueGetter ?
185
+ )
186
+
187
+ assertEquals(
188
+ """
189
+ {
190
+ "nonNull" : "JsonValueGetterImplementation(value=0)",
191
+ "nullable" : "JsonValueGetterImplementation(value=1)"
192
+ }
193
+ """ .trimIndent(),
194
+ writer.writeValueAsString(
195
+ Data (
196
+ JsonValueGetterImplementation (0 ),
197
+ JsonValueGetterImplementation (1 )
198
+ )
199
+ )
200
+ )
201
+ }
202
+
203
+ @Test
204
+ fun inCollection () {
205
+ assertEquals(
206
+ " [ 0, 1, \" PropertyWithOverriddenGetter(value=2)\" , \" DirectlyOverriddenGetter(value=3)\" , \" JsonValueGetterImplementation(value=4)\" ]" ,
207
+ writer.writeValueAsString(
208
+ listOf (
209
+ ValueParamGetterAnnotated (0 ),
210
+ ValueParamFieldAnnotated (1 ),
211
+ PropertyWithOverriddenGetter (2 ),
212
+ DirectlyOverriddenGetter (3 ),
213
+ JsonValueGetterImplementation (4 )
214
+ )
215
+ )
216
+ )
217
+ }
218
+
219
+ @Test
220
+ fun inArray () {
221
+ assertEquals(
222
+ " [ 0, 1, \" PropertyWithOverriddenGetter(value=2)\" , \" DirectlyOverriddenGetter(value=3)\" , \" JsonValueGetterImplementation(value=4)\" ]" ,
223
+ writer.writeValueAsString(
224
+ arrayOf(
225
+ ValueParamGetterAnnotated (0 ),
226
+ ValueParamFieldAnnotated (1 ),
227
+ PropertyWithOverriddenGetter (2 ),
228
+ DirectlyOverriddenGetter (3 ),
229
+ JsonValueGetterImplementation (4 )
230
+ )
231
+ )
101
232
)
102
233
}
103
234
}
0 commit comments