Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed handling of null and JsonValue as well as #904 #908

Merged
merged 3 commits into from
Feb 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions release-notes/CREDITS-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Contributors:
# 2.18.3 (not yet released)

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

Expand Down
4 changes: 3 additions & 1 deletion release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -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`(#873).

2.18.2 (27-Nov-2024)
2.18.1 (28-Oct-2024)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,6 @@ object ValueClassUnboxSerializer : StdSerializer<Any>(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)
}
}
Expand All @@ -76,9 +70,7 @@ internal sealed class ValueClassSerializer<T : Any>(t: Class<T>) : 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)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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<String, Any>,
)

data class TimestampedPerson(
val timestamp: Long,
val person: Person,
)

@Test
fun `should serialize value class`() {

Expand Down Expand Up @@ -35,12 +46,18 @@ class GitHub873 {
}

@JvmInline
value class Person(
val properties: Map<String, Any>,
)
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)
}
}