package de.itscope.catalog.pricelist import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper import com.fasterxml.jackson.module.kotlin.readValue import com.fasterxml.jackson.module.kotlin.registerKotlinModule import org.junit.jupiter.api.Assertions.assertEquals import org.junit.jupiter.api.Test /** * Test to demonstrate, that it's not possible to read a map which a generic value type as key. */ class ParseValueClassTest { @JvmInline value class Index(val value: Int = 0) /** * Uses a value class ([Index]) as key. */ data class FooMapKey(val map: MutableMap = mutableMapOf()) data class FooField(val index: Index) data class FooMapValue(val map: MutableMap = mutableMapOf()) data class FooList(val list: MutableList = mutableListOf()) // This 2 tests will fail on Jackson-module-kotlin: `2.17.2` @Test fun parseFooMapTest() { val mapper = jacksonObjectMapper().registerKotlinModule() val foo = FooMapKey().apply { map[Index(1)] = 42 } val fooJson = mapper.writeValueAsString(foo) val readValue = mapper.readValue(fooJson) assertEquals(foo, readValue) } @Test fun parseFooMapEmptyTest() { val mapper = jacksonObjectMapper().registerKotlinModule() val foo = FooMapKey() val fooJson = mapper.writeValueAsString(foo) val readValue = mapper.readValue(fooJson) assertEquals(foo, readValue) } // This 4 other tests will pass: @Test fun parseIndexTest() { val mapper = jacksonObjectMapper().registerKotlinModule() val foo = FooField(Index(0)) val fooJson = mapper.writeValueAsString(foo) val readValue = mapper.readValue(fooJson) assertEquals(foo, readValue) } @Test fun parseFooListTest() { val mapper = jacksonObjectMapper().registerKotlinModule() val foo = FooList().apply { list.add(Index(42)) } val fooJson = mapper.writeValueAsString(foo) val readValue = mapper.readValue(fooJson) assertEquals(foo, readValue) } @Test fun parseFooListEmptyTest() { val mapper = jacksonObjectMapper().registerKotlinModule() val foo = FooList() val fooJson = mapper.writeValueAsString(foo) val readValue = mapper.readValue(fooJson) assertEquals(foo, readValue) } @Test fun parseFooMapValueTest() { val mapper = jacksonObjectMapper().registerKotlinModule() val foo = FooMapValue().apply { map[1] = Index(42) } val fooJson = mapper.writeValueAsString(foo) val readValue = mapper.readValue(fooJson) assertEquals(foo, readValue) } }