Skip to content

Commit 98df422

Browse files
authored
Merge pull request #909 from FasterXML/2.18
2.18
2 parents 452149e + 0f765ba commit 98df422

File tree

4 files changed

+29
-17
lines changed

4 files changed

+29
-17
lines changed

release-notes/CREDITS-2.x

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ WrongWrong (@k163377)
3131
# 2.18.3 (not yet released)
3232

3333
WrongWrong (@k163377)
34+
* #908: Additional fixes related to #904.
3435
* #904: Fixed an error when serializing a `value class` that wraps a `Map`
3536
* #900: Fixed an issue where some tests were not running
3637

release-notes/VERSION-2.x

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ Co-maintainers:
3636

3737
2.18.3 (not yet released)
3838

39-
#904: An error that occurred when serializing a `value class` that wraps a `Map`(#873) has been fixed.
39+
#904: Fixed a problem where context was not being propagated properly when serializing an unboxed value of `value class`
40+
or a value retrieved with `JsonValue`.
41+
This fixes a problem where an error would occur when serializing a `value class` that wraps a `Map`(#873).
4042

4143
2.18.2 (27-Nov-2024)
4244
2.18.1 (28-Oct-2024)

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

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,6 @@ object ValueClassUnboxSerializer : StdSerializer<Any>(Any::class.java) {
5656

5757
override fun serialize(value: Any, gen: JsonGenerator, provider: SerializerProvider) {
5858
val unboxed = value::class.java.getMethod("unbox-impl").invoke(value)
59-
60-
if (unboxed == null) {
61-
provider.findNullValueSerializer(null).serialize(null, gen, provider)
62-
return
63-
}
64-
6559
provider.defaultSerializeValue(unboxed, gen)
6660
}
6761
}
@@ -76,9 +70,7 @@ internal sealed class ValueClassSerializer<T : Any>(t: Class<T>) : StdSerializer
7670
val unboxed = unboxMethod.invoke(value)
7771
// As shown in the processing of the factory function, jsonValueGetter is always a static method.
7872
val jsonValue: Any? = staticJsonValueGetter.invoke(null, unboxed)
79-
jsonValue
80-
?.let { provider.findValueSerializer(it::class.java).serialize(it, gen, provider) }
81-
?: provider.findNullValueSerializer(null).serialize(null, gen, provider)
73+
provider.defaultSerializeValue(jsonValue, gen)
8274
}
8375
}
8476

src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/GitHub873.kt

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,22 @@
11
package com.fasterxml.jackson.module.kotlin.test.github
22

3+
import com.fasterxml.jackson.annotation.JsonValue
34
import com.fasterxml.jackson.module.kotlin.defaultMapper
45
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
56
import com.fasterxml.jackson.module.kotlin.readValue
67
import kotlin.test.Test
78

89
class GitHub873 {
10+
@JvmInline
11+
value class Person(
12+
val properties: Map<String, Any>,
13+
)
14+
15+
data class TimestampedPerson(
16+
val timestamp: Long,
17+
val person: Person,
18+
)
19+
920
@Test
1021
fun `should serialize value class`() {
1122

@@ -35,12 +46,18 @@ class GitHub873 {
3546
}
3647

3748
@JvmInline
38-
value class Person(
39-
val properties: Map<String, Any>,
40-
)
49+
value class MapAsJsonValue(val value: String) {
50+
@get:JsonValue
51+
val jsonValue get() = mapOf("key" to value)
52+
}
4153

42-
data class TimestampedPerson(
43-
val timestamp: Long,
44-
val person: Person,
45-
)
54+
data class JsonValueWrapper(val value: MapAsJsonValue)
55+
56+
@Test
57+
fun `JsonValue is serialized in the same way`() {
58+
val data = JsonValueWrapper(MapAsJsonValue("value"))
59+
val json = defaultMapper.writeValueAsString(data)
60+
61+
assert("""{"value":{"key":"value"}}""" == json)
62+
}
4663
}

0 commit comments

Comments
 (0)