Skip to content

Commit 414327f

Browse files
authored
Merge pull request #477 from TWiStErRob/deprecation_fixes
[2.13 RC1] Deprecation fixes for KotlinModule and docs for KotlinFeature
2 parents 0eeffb8 + 6d2df73 commit 414327f

File tree

3 files changed

+132
-40
lines changed

3 files changed

+132
-40
lines changed

release-notes/CREDITS-2.x

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ Authors:
1313

1414
Contributors:
1515

16+
Róbert Papp (TWiStErRob@github)
17+
* #477: KotlinFeature documentation & deprecation replacements
18+
1619
wrongwrong (k163377@github)
1720
* #468: Improved support for value classes
1821
(2.13)

src/main/kotlin/com/fasterxml/jackson/module/kotlin/KotlinFeature.kt

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,45 @@ package com.fasterxml.jackson.module.kotlin
33
import java.util.BitSet
44
import kotlin.math.pow
55

6+
/**
7+
* @see KotlinModule.Builder
8+
*/
69
enum class KotlinFeature(val enabledByDefault: Boolean) {
10+
/**
11+
* This feature represents whether to deserialize `null` values for collection properties as empty collections.
12+
*/
713
NullToEmptyCollection(enabledByDefault = false),
14+
15+
/**
16+
* This feature represents whether to deserialize `null` values for a map property to an empty map object.
17+
*/
818
NullToEmptyMap(enabledByDefault = false),
19+
20+
/**
21+
* This feature represents whether to treat `null` values as absent when deserializing,
22+
* thereby using the default value provided in Kotlin.
23+
*/
924
NullIsSameAsDefault(enabledByDefault = false),
25+
26+
/**
27+
* By default, there's no special handling of singletons (pre-2.10 behavior).
28+
* Each time a Singleton object is deserialized a new instance is created.
29+
*
30+
* When this feature is enabled, it will deserialize then canonicalize (was the default in 2.10).
31+
* Deserializing a singleton overwrites the value of the single instance.
32+
*
33+
* See [jackson-module-kotlin#225]: keep Kotlin singletons as singletons.
34+
* @see com.fasterxml.jackson.module.kotlin.SingletonSupport
35+
*/
1036
SingletonSupport(enabledByDefault = false),
37+
38+
/**
39+
* This feature represents whether to check deserialized collections.
40+
*
41+
* With this disabled, the default, collections which are typed to disallow null members (e.g. `List<String>`)
42+
* may contain null values after deserialization.
43+
* Enabling it protects against this but has significant performance impact.
44+
*/
1145
StrictNullChecks(enabledByDefault = false);
1246

1347
internal val bitSet: BitSet = 2.0.pow(ordinal).toInt().toBitSet()

src/main/kotlin/com/fasterxml/jackson/module/kotlin/KotlinModule.kt

Lines changed: 95 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,21 @@ fun Class<*>.isKotlinClass(): Boolean {
3232
* (e.g. List<String>) may contain null values after deserialization. Enabling it
3333
* protects against this but has significant performance impact.
3434
*/
35-
class KotlinModule @Deprecated(level = DeprecationLevel.WARNING, message = "Use KotlinModule.Builder") constructor(
35+
class KotlinModule @Deprecated(
36+
level = DeprecationLevel.WARNING,
37+
message = "Use KotlinModule.Builder instead of named constructor parameters.",
38+
replaceWith = ReplaceWith(
39+
"""KotlinModule.Builder()
40+
.withReflectionCacheSize(reflectionCacheSize)
41+
.configure(KotlinFeature.NullToEmptyCollection, nullToEmptyCollection)
42+
.configure(KotlinFeature.NullToEmptyMap, nullToEmptyMap)
43+
.configure(KotlinFeature.NullIsSameAsDefault, nullIsSameAsDefault)
44+
.configure(KotlinFeature.SingletonSupport, singletonSupport)
45+
.configure(KotlinFeature.StrictNullChecks, strictNullChecks)
46+
.build()""",
47+
"com.fasterxml.jackson.module.kotlin.KotlinFeature"
48+
)
49+
) constructor(
3650
val reflectionCacheSize: Int = 512,
3751
val nullToEmptyCollection: Boolean = false,
3852
val nullToEmptyMap: Boolean = false,
@@ -129,100 +143,141 @@ class KotlinModule @Deprecated(level = DeprecationLevel.WARNING, message = "Use
129143
KotlinFeature.values().filter { it.enabledByDefault }.forEach { or(it.bitSet) }
130144
}
131145

132-
fun withReflectionCacheSize(reflectionCacheSize: Int) = apply {
146+
fun withReflectionCacheSize(reflectionCacheSize: Int): Builder = apply {
133147
this.reflectionCacheSize = reflectionCacheSize
134148
}
135149

136-
fun enable(feature: KotlinFeature) = apply {
150+
fun enable(feature: KotlinFeature): Builder = apply {
137151
bitSet.or(feature.bitSet)
138152
}
139153

140-
fun disable(feature: KotlinFeature) = apply {
154+
fun disable(feature: KotlinFeature): Builder = apply {
141155
bitSet.andNot(feature.bitSet)
142156
}
143157

144-
fun configure(feature: KotlinFeature, enabled: Boolean) = when {
145-
enabled -> enable(feature)
146-
else -> disable(feature)
147-
}
158+
fun configure(feature: KotlinFeature, enabled: Boolean): Builder =
159+
when {
160+
enabled -> enable(feature)
161+
else -> disable(feature)
162+
}
148163

149-
fun isEnabled(feature: KotlinFeature): Boolean = bitSet.intersects(feature.bitSet)
164+
fun isEnabled(feature: KotlinFeature): Boolean =
165+
bitSet.intersects(feature.bitSet)
150166

151167
@Deprecated(
152168
message = "Deprecated, use withReflectionCacheSize(reflectionCacheSize) instead.",
153-
replaceWith = ReplaceWith("isEnabled(reflectionCacheSize)")
169+
replaceWith = ReplaceWith("withReflectionCacheSize(reflectionCacheSize)")
154170
)
155-
fun reflectionCacheSize(reflectionCacheSize: Int) = apply {
156-
this.reflectionCacheSize = reflectionCacheSize
157-
}
171+
fun reflectionCacheSize(reflectionCacheSize: Int): Builder =
172+
withReflectionCacheSize(reflectionCacheSize)
158173

159174
@Deprecated(
160175
message = "Deprecated, use isEnabled(NullToEmptyCollection) instead.",
161-
replaceWith = ReplaceWith("isEnabled(NullToEmptyCollection)")
176+
replaceWith = ReplaceWith(
177+
"isEnabled(KotlinFeature.NullToEmptyCollection)",
178+
"com.fasterxml.jackson.module.kotlin.KotlinFeature"
179+
)
162180
)
163-
fun getNullToEmptyCollection() = isEnabled(NullToEmptyCollection)
181+
fun getNullToEmptyCollection(): Boolean =
182+
isEnabled(NullToEmptyCollection)
164183

165184
@Deprecated(
166185
message = "Deprecated, use configure(NullToEmptyCollection, enabled) instead.",
167-
replaceWith = ReplaceWith("configure(NullToEmptyCollection, enabled)")
186+
replaceWith = ReplaceWith(
187+
"configure(KotlinFeature.NullToEmptyCollection, nullToEmptyCollection)",
188+
"com.fasterxml.jackson.module.kotlin.KotlinFeature"
189+
)
168190
)
169-
fun nullToEmptyCollection(nullToEmptyCollection: Boolean) =
191+
fun nullToEmptyCollection(nullToEmptyCollection: Boolean): Builder =
170192
configure(NullToEmptyCollection, nullToEmptyCollection)
171193

172194
@Deprecated(
173195
message = "Deprecated, use isEnabled(NullToEmptyMap) instead.",
174-
replaceWith = ReplaceWith("isEnabled(NullToEmptyMap)")
196+
replaceWith = ReplaceWith(
197+
"isEnabled(KotlinFeature.NullToEmptyMap)",
198+
"com.fasterxml.jackson.module.kotlin.KotlinFeature"
199+
)
175200
)
176-
fun getNullToEmptyMap() = isEnabled(NullToEmptyMap)
201+
fun getNullToEmptyMap(): Boolean =
202+
isEnabled(NullToEmptyMap)
177203

178204
@Deprecated(
179205
message = "Deprecated, use configure(NullToEmptyMap, enabled) instead.",
180-
replaceWith = ReplaceWith("configure(NullToEmptyMap, enabled)")
206+
replaceWith = ReplaceWith(
207+
"configure(KotlinFeature.NullToEmptyMap, nullToEmptyMap)",
208+
"com.fasterxml.jackson.module.kotlin.KotlinFeature"
209+
)
181210
)
182-
fun nullToEmptyMap(nullToEmptyMap: Boolean) = configure(NullToEmptyMap, nullToEmptyMap)
211+
fun nullToEmptyMap(nullToEmptyMap: Boolean): Builder =
212+
configure(NullToEmptyMap, nullToEmptyMap)
183213

184214
@Deprecated(
185215
message = "Deprecated, use isEnabled(NullIsSameAsDefault) instead.",
186-
replaceWith = ReplaceWith("isEnabled(NullIsSameAsDefault)")
216+
replaceWith = ReplaceWith(
217+
"isEnabled(KotlinFeature.NullIsSameAsDefault)",
218+
"com.fasterxml.jackson.module.kotlin.KotlinFeature"
219+
)
187220
)
188-
fun getNullIsSameAsDefault() = isEnabled(NullIsSameAsDefault)
221+
fun getNullIsSameAsDefault(): Boolean =
222+
isEnabled(NullIsSameAsDefault)
189223

190224
@Deprecated(
191225
message = "Deprecated, use configure(NullIsSameAsDefault, enabled) instead.",
192-
replaceWith = ReplaceWith("configure(NullIsSameAsDefault, enabled)")
226+
replaceWith = ReplaceWith(
227+
"configure(KotlinFeature.NullIsSameAsDefault, nullIsSameAsDefault)",
228+
"com.fasterxml.jackson.module.kotlin.KotlinFeature"
229+
)
193230
)
194-
fun nullIsSameAsDefault(nullIsSameAsDefault: Boolean) = configure(NullIsSameAsDefault, nullIsSameAsDefault)
231+
fun nullIsSameAsDefault(nullIsSameAsDefault: Boolean): Builder =
232+
configure(NullIsSameAsDefault, nullIsSameAsDefault)
195233

196234
@Deprecated(
197235
message = "Deprecated, use isEnabled(SingletonSupport) instead.",
198-
replaceWith = ReplaceWith("isEnabled(SingletonSupport)")
236+
replaceWith = ReplaceWith(
237+
"isEnabled(KotlinFeature.SingletonSupport)",
238+
"com.fasterxml.jackson.module.kotlin.KotlinFeature"
239+
)
199240
)
200-
fun getSingletonSupport() = when {
201-
isEnabled(KotlinFeature.SingletonSupport) -> CANONICALIZE
202-
else -> DISABLED
203-
}
241+
fun getSingletonSupport(): SingletonSupport =
242+
when {
243+
isEnabled(KotlinFeature.SingletonSupport) -> CANONICALIZE
244+
else -> DISABLED
245+
}
204246

205247
@Deprecated(
206248
message = "Deprecated, use configure(SingletonSupport, enabled) instead.",
207-
replaceWith = ReplaceWith("configure(SingletonSupport, enabled)")
249+
replaceWith = ReplaceWith(
250+
"configure(KotlinFeature.SingletonSupport, singletonSupport)",
251+
"com.fasterxml.jackson.module.kotlin.KotlinFeature"
252+
)
208253
)
209-
fun singletonSupport(singletonSupport: SingletonSupport) = when (singletonSupport) {
210-
CANONICALIZE -> enable(KotlinFeature.SingletonSupport)
211-
else -> disable(KotlinFeature.SingletonSupport)
212-
}
254+
fun singletonSupport(singletonSupport: SingletonSupport): Builder =
255+
when (singletonSupport) {
256+
CANONICALIZE -> enable(KotlinFeature.SingletonSupport)
257+
else -> disable(KotlinFeature.SingletonSupport)
258+
}
213259

214260
@Deprecated(
215261
message = "Deprecated, use isEnabled(StrictNullChecks) instead.",
216-
replaceWith = ReplaceWith("isEnabled(StrictNullChecks)")
262+
replaceWith = ReplaceWith(
263+
"isEnabled(KotlinFeature.StrictNullChecks)",
264+
"com.fasterxml.jackson.module.kotlin.KotlinFeature"
265+
)
217266
)
218-
fun getStrictNullChecks() = isEnabled(StrictNullChecks)
267+
fun getStrictNullChecks(): Boolean =
268+
isEnabled(StrictNullChecks)
219269

220270
@Deprecated(
221271
message = "Deprecated, use configure(StrictNullChecks, enabled) instead.",
222-
replaceWith = ReplaceWith("configure(StrictNullChecks, enabled)")
272+
replaceWith = ReplaceWith(
273+
"configure(KotlinFeature.StrictNullChecks, strictNullChecks)",
274+
"com.fasterxml.jackson.module.kotlin.KotlinFeature"
275+
)
223276
)
224-
fun strictNullChecks(strictNullChecks: Boolean) = configure(StrictNullChecks, strictNullChecks)
277+
fun strictNullChecks(strictNullChecks: Boolean): Builder =
278+
configure(StrictNullChecks, strictNullChecks)
225279

226-
fun build() = KotlinModule(this)
280+
fun build(): KotlinModule =
281+
KotlinModule(this)
227282
}
228283
}

0 commit comments

Comments
 (0)