Skip to content

Commit

Permalink
Merge pull request #666 from k163377/fix-value-issue
Browse files Browse the repository at this point in the history
Fixed problem with `value class` where `JsonValue` flag was ignored.
  • Loading branch information
k163377 committed Apr 23, 2023
2 parents 4d5f70b + 16dcd07 commit 55370f1
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 7 deletions.
1 change: 1 addition & 0 deletions release-notes/CREDITS-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ WrongWrong (@k163377)
* #654: Change MKPE.parameter property to transient.
* #659: Improve serialization support for value class.
* #665: Modified to not load the entire Sequence into memory during serialization.
* #666: Fixed problem with value class where JsonValue flag was ignored.

Sylvain-maillard (@Sylvain-maillard)
* #554: Add extension function for addMixin.
Expand Down
2 changes: 2 additions & 0 deletions release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ Co-maintainers:
(contributed by wrongwrong)
#665: Modified to not load the entire Sequence into memory during serialization(fixes #368).
(contributed by wrongwrong)
#666: Fixed problem with value class where JsonValue flag was ignored.
(contributed by wrongwrong)

It is also confirmed that the issue submitted below is no longer reproduced,
although it is unclear when it was explicitly fixed.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@ object ULongSerializer : StdSerializer<ULong>(ULong::class.java) {
}

// Class must be UnboxableValueClass.
private fun Class<*>.getStaticJsonValueGetter(): Method? = this.declaredMethods
.find { method -> Modifier.isStatic(method.modifiers) && method.annotations.any { it is JsonValue } }
private fun Class<*>.getStaticJsonValueGetter(): Method? = this.declaredMethods.find { method ->
Modifier.isStatic(method.modifiers) && method.annotations.any { it is JsonValue && it.value }
}

object ValueClassUnboxSerializer : StdSerializer<Any>(Any::class.java) {
override fun serialize(value: Any, gen: JsonGenerator, provider: SerializerProvider) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ class GitHub530 {
value class ValueParamGetterAnnotated(@get:JsonValue val value: Int)

@JvmInline
value class ValueParamFieldAnnotated(@JvmField @field:JsonValue val value: Int)
value class ValueParamFieldAnnotated(
@JvmField @field:JsonValue
val value: Int
)

@JvmInline
value class PropertyWithOverriddenGetter(val value: Int) {
Expand All @@ -38,6 +41,13 @@ class GitHub530 {
@JvmInline
value class JsonValueGetterImplementation(val value: Int) : JsonValueGetter

@JvmInline
value class JsonValueGetterImplementationDisabled(val value: Int) : JsonValueGetter {
@get:JsonValue(false)
override val jsonValue: String
get() = super.jsonValue
}

private val writer = jacksonMapperBuilder().build().testPrettyWriter()

@Test
Expand Down Expand Up @@ -178,6 +188,29 @@ class GitHub530 {
)
}

@Test
fun jsonValueGetterImplementationDisabled() {
data class Data(
val nonNull: JsonValueGetterImplementationDisabled,
val nullable: JsonValueGetterImplementationDisabled?
)

assertEquals(
"""
{
"nonNull" : 0,
"nullable" : 1
}
""".trimIndent(),
writer.writeValueAsString(
Data(
JsonValueGetterImplementationDisabled(0),
JsonValueGetterImplementationDisabled(1)
)
)
)
}

@Test
fun jsonValueGetterImplementationAsGenericType() {
data class Data(
Expand All @@ -199,19 +232,34 @@ class GitHub530 {
)
)
)
assertEquals(
"""
{
"nonNull" : 0,
"nullable" : 1
}
""".trimIndent(),
writer.writeValueAsString(
Data(
JsonValueGetterImplementationDisabled(0),
JsonValueGetterImplementationDisabled(1)
)
)
)
}

@Test
fun inCollection() {
assertEquals(
"[ 0, 1, \"PropertyWithOverriddenGetter(value=2)\", \"DirectlyOverriddenGetter(value=3)\", \"JsonValueGetterImplementation(value=4)\" ]",
"[ 0, 1, \"PropertyWithOverriddenGetter(value=2)\", \"DirectlyOverriddenGetter(value=3)\", \"JsonValueGetterImplementation(value=4)\", 5 ]",
writer.writeValueAsString(
listOf(
ValueParamGetterAnnotated(0),
ValueParamFieldAnnotated(1),
PropertyWithOverriddenGetter(2),
DirectlyOverriddenGetter(3),
JsonValueGetterImplementation(4)
JsonValueGetterImplementation(4),
JsonValueGetterImplementationDisabled(5)
)
)
)
Expand All @@ -220,14 +268,15 @@ class GitHub530 {
@Test
fun inArray() {
assertEquals(
"[ 0, 1, \"PropertyWithOverriddenGetter(value=2)\", \"DirectlyOverriddenGetter(value=3)\", \"JsonValueGetterImplementation(value=4)\" ]",
"[ 0, 1, \"PropertyWithOverriddenGetter(value=2)\", \"DirectlyOverriddenGetter(value=3)\", \"JsonValueGetterImplementation(value=4)\", 5 ]",
writer.writeValueAsString(
arrayOf(
ValueParamGetterAnnotated(0),
ValueParamFieldAnnotated(1),
PropertyWithOverriddenGetter(2),
DirectlyOverriddenGetter(3),
JsonValueGetterImplementation(4)
JsonValueGetterImplementation(4),
JsonValueGetterImplementationDisabled(5)
)
)
)
Expand Down

0 comments on commit 55370f1

Please sign in to comment.