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

JsonValue set in value class does not work #530

Closed
k163377 opened this issue Jan 2, 2022 · 2 comments
Closed

JsonValue set in value class does not work #530

k163377 opened this issue Jan 2, 2022 · 2 comments
Labels

Comments

@k163377
Copy link
Contributor

k163377 commented Jan 2, 2022

Describe the bug
SSIA

This issue is related to #199 .

To Reproduce

import com.fasterxml.jackson.annotation.JsonValue
import com.fasterxml.jackson.module.kotlin.jacksonMapperBuilder
import org.junit.Test
import kotlin.test.assertEquals

class GitHubXXX {
    // At the moment, the output is the same with or without `JsonValue`,
    // but this pattern is included in the test case in case the option to default to a serialization method that
    // does not `unbox` is introduced in the future.
    @JvmInline value class Foo(@get:JsonValue val value: Int)

    @JvmInline value class Bar(val value: Int) {
        @get:JsonValue val jsonValue: String get() = this.toString()
    }

    interface JsonValueGetter { @get:JsonValue val jsonValue: String get() = this.toString() }
    @JvmInline value class Baz(val value: Int): JsonValueGetter

    @JvmInline value class Qux(val value: Int): JsonValueGetter

    @JvmInline value class Corge(val value: Int) {
        @get:JsonValue val jsonValue: String get() = this.toString()
    }

    data class Data<T : Any>(
        val foo1: Foo,
        val foo2: Foo?,
        val bar1: Bar,
        val bar2: Bar?,
        val baz1: Baz,
        val baz2: Baz?,
        val qux1: JsonValueGetter,
        val qux2: JsonValueGetter?,
        val corge1: T,
        val corge2: T?
    )

    @Test
    fun test() {
        val writer = jacksonMapperBuilder().build().writerWithDefaultPrettyPrinter()

        assertEquals(
            """
                {
                  "foo1" : 1,
                  "foo2" : 2,
                  "bar1" : "Bar(value=3)",
                  "bar2" : "Bar(value=4)",
                  "baz1" : "Baz(value=5)",
                  "baz2" : "Baz(value=6)",
                  "qux1" : "Qux(value=7)",
                  "qux2" : "Qux(value=8)",
                  "corge1" : "Corge(value=9)",
                  "corge2" : "Corge(value=10)"
                }
            """.trimIndent(),
            writer.writeValueAsString(
                Data(
                    Foo(1), Foo(2),
                    Bar(3), Bar(4),
                    Baz(5), Baz(6),
                    Qux(7), Qux(8),
                    Corge(9), Corge(10)
                )
            )
        )
    }
}

At the moment, actual as follows.

{
  "foo1" : 1,
  "foo2" : 2,
  "bar1" : 3,
  "bar2" : 4,
  "baz1" : 5,
  "baz2" : 6,
  "qux1" : 7,
  "qux2" : 8,
  "corge1" : 9,
  "corge2" : 10
}

Versions
This commit: 9cd05ee

Additional context
I am going to fix this problem myself.
I would appreciate it if you could point out any omissions in the test cases.

@k163377 k163377 added the bug label Jan 2, 2022
@k163377 k163377 mentioned this issue Jan 2, 2022
@dinomite
Copy link
Member

dinomite commented Jan 2, 2022

Probably makes sense to merge after #512

@dinomite
Copy link
Member

dinomite commented Jan 2, 2022

(meant that comment for #531)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants