From 12ca397f2cde0c0da24bc4d63bdabfa3ac1e0f90 Mon Sep 17 00:00:00 2001 From: George Hawkins Date: Wed, 18 Dec 2019 13:38:29 +0100 Subject: [PATCH] Added test of interaction between Kotlin object singleton deserialization and Jackson @JsonTypeInfo and @JsonIdentityInfo. --- .../jackson/module/kotlin/test/GithubX.kt | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/GithubX.kt diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/GithubX.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/GithubX.kt new file mode 100644 index 00000000..40cdc8f6 --- /dev/null +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/GithubX.kt @@ -0,0 +1,60 @@ +package com.fasterxml.jackson.module.kotlin.test + +import com.fasterxml.jackson.annotation.JsonIdentityInfo +import com.fasterxml.jackson.annotation.JsonTypeInfo +import com.fasterxml.jackson.annotation.ObjectIdGenerators +import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper +import com.fasterxml.jackson.module.kotlin.jacksonTypeRef +import com.fasterxml.jackson.module.kotlin.readValue +import org.junit.Test +import kotlin.test.assertEquals +import kotlin.test.assertSame + +@JsonTypeInfo(use = JsonTypeInfo.Id.MINIMAL_CLASS, include = JsonTypeInfo.As.PROPERTY, property = "@type") +@JsonIdentityInfo(generator = ObjectIdGenerators.IntSequenceGenerator::class, property = "@id") +interface Entity + +object NumberEntity : Entity + +data class NumberValue(val value: Int) { + val entity = NumberEntity +} + +class TestGithubX { + private val json = """ + [ { + "value" : 10, + "entity" : { + "@type" : ".NumberEntity", + "@id" : 1 + } + }, { + "value" : 11, + "entity" : 1 + } ] + """.trimIndent() + + @Test + fun `test writing involving type, id and object`() { + val input = listOf(NumberValue(10), NumberValue(11)) + + val output = jacksonObjectMapper() + .writerFor(jacksonTypeRef>()) + .withDefaultPrettyPrinter() + .writeValueAsString(input) + + assertEquals(json, output) + } + + @Test + fun `test reading involving type, id and object`() { + val output = jacksonObjectMapper().readValue>(json) + + assertEquals(2, output.size) + val (a, b) = output + assertSame(NumberEntity::class.java, a.entity.javaClass) + assertSame(NumberEntity::class.java, b.entity.javaClass) + assertEquals(10, a.value) + assertEquals(11, b.value) + } +}