diff --git a/release-notes/CREDITS-2.x b/release-notes/CREDITS-2.x index 2da904ac5..ef905d29f 100644 --- a/release-notes/CREDITS-2.x +++ b/release-notes/CREDITS-2.x @@ -18,6 +18,7 @@ Contributors: # 2.18.3 (not yet released) WrongWrong (@k163377) +* #905: Additional fixes related to #907. * #904: Fixed an error when serializing a `value class` that wraps a `Map` * #900: Fixed an issue where some tests were not running diff --git a/release-notes/VERSION-2.x b/release-notes/VERSION-2.x index 4f3a68d98..8f23c431b 100644 --- a/release-notes/VERSION-2.x +++ b/release-notes/VERSION-2.x @@ -18,7 +18,9 @@ Co-maintainers: 2.18.3 (not yet released) -#904: An error that occurred when serializing a `value class` that wraps a `Map`(#873) has been fixed. +#904: Fixed a problem where context was not being propagated properly when serializing an unboxed value of `value class` + or a value retrieved with `JsonValue`. + This fixes a problem where an error would occur when serializing a `value class` that wraps a `Map`. 2.18.2 (27-Nov-2024) 2.18.1 (28-Oct-2024) diff --git a/src/main/kotlin/com/fasterxml/jackson/module/kotlin/KotlinSerializers.kt b/src/main/kotlin/com/fasterxml/jackson/module/kotlin/KotlinSerializers.kt index efffdd6a2..82bcb164d 100644 --- a/src/main/kotlin/com/fasterxml/jackson/module/kotlin/KotlinSerializers.kt +++ b/src/main/kotlin/com/fasterxml/jackson/module/kotlin/KotlinSerializers.kt @@ -56,12 +56,6 @@ object ValueClassUnboxSerializer : StdSerializer(Any::class.java) { override fun serialize(value: Any, gen: JsonGenerator, provider: SerializerProvider) { val unboxed = value::class.java.getMethod("unbox-impl").invoke(value) - - if (unboxed == null) { - provider.findNullValueSerializer(null).serialize(null, gen, provider) - return - } - provider.defaultSerializeValue(unboxed, gen) } } @@ -76,9 +70,7 @@ internal sealed class ValueClassSerializer(t: Class) : StdSerializer val unboxed = unboxMethod.invoke(value) // As shown in the processing of the factory function, jsonValueGetter is always a static method. val jsonValue: Any? = staticJsonValueGetter.invoke(null, unboxed) - jsonValue - ?.let { provider.findValueSerializer(it::class.java).serialize(it, gen, provider) } - ?: provider.findNullValueSerializer(null).serialize(null, gen, provider) + provider.defaultSerializeValue(jsonValue, gen) } } diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/GitHub873.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/GitHub873.kt index dfe236440..c45def865 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/GitHub873.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/GitHub873.kt @@ -1,11 +1,22 @@ package com.fasterxml.jackson.module.kotlin.test.github +import com.fasterxml.jackson.annotation.JsonValue import com.fasterxml.jackson.module.kotlin.defaultMapper import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper import com.fasterxml.jackson.module.kotlin.readValue import kotlin.test.Test class GitHub873 { + @JvmInline + value class Person( + val properties: Map, + ) + + data class TimestampedPerson( + val timestamp: Long, + val person: Person, + ) + @Test fun `should serialize value class`() { @@ -35,12 +46,18 @@ class GitHub873 { } @JvmInline - value class Person( - val properties: Map, - ) + value class MapAsJsonValue(val value: String) { + @get:JsonValue + val jsonValue get() = mapOf("key" to value) + } - data class TimestampedPerson( - val timestamp: Long, - val person: Person, - ) + data class JsonValueWrapper(val value: MapAsJsonValue) + + @Test + fun `JsonValue is serialized in the same way`() { + val data = JsonValueWrapper(MapAsJsonValue("value")) + val json = defaultMapper.writeValueAsString(data) + + assert("""{"value":{"key":"value"}}""" == json) + } }