Skip to content

Commit

Permalink
Merge pull request #908 from k163377/add-case-for-873
Browse files Browse the repository at this point in the history
Fixed handling of null and JsonValue as well as #904
  • Loading branch information
k163377 authored Feb 2, 2025
2 parents e30c612 + 6027563 commit 0f765ba
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 17 deletions.
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)
}
}

0 comments on commit 0f765ba

Please sign in to comment.