Skip to content

Commit e7fdcf8

Browse files
committed
Merge branch '2.19'
2 parents d5024e6 + 98df422 commit e7fdcf8

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/tools/jackson/module/kotlin/KotlinSerializers.kt

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,6 @@ private fun Class<*>.getStaticJsonValueGetter(): Method? = this.declaredMethods.
5050
object ValueClassUnboxSerializer : StdSerializer<Any>(Any::class.java) {
5151
override fun serialize(value: Any, gen: JsonGenerator, ctxt: SerializationContext) {
5252
val unboxed = value::class.java.getMethod("unbox-impl").invoke(value)
53-
54-
if (unboxed == null) {
55-
ctxt.findNullValueSerializer(null).serialize(null, gen, ctxt)
56-
return
57-
}
58-
5953
ctxt.writeValue(gen, unboxed)
6054
}
6155
}
@@ -70,9 +64,7 @@ internal sealed class ValueClassSerializer<T : Any>(t: Class<T>) : StdSerializer
7064
val unboxed = unboxMethod.invoke(value)
7165
// As shown in the processing of the factory function, jsonValueGetter is always a static method.
7266
val jsonValue: Any? = staticJsonValueGetter.invoke(null, unboxed)
73-
jsonValue
74-
?.let { ctxt.findValueSerializer(it::class.java).serialize(it, gen, ctxt) }
75-
?: ctxt.findNullValueSerializer(null).serialize(null, gen, ctxt)
67+
ctxt.writeValue(gen, jsonValue)
7668
}
7769
}
7870

src/test/kotlin/tools/jackson/module/kotlin/test/github/GitHub873.kt

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,20 @@ package tools.jackson.module.kotlin.test.github
22

33
import tools.jackson.module.kotlin.defaultMapper
44
import tools.jackson.module.kotlin.readValue
5+
import com.fasterxml.jackson.annotation.JsonValue
56
import kotlin.test.Test
67

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

@@ -34,12 +45,18 @@ class GitHub873 {
3445
}
3546

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

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

0 commit comments

Comments
 (0)