Created
August 12, 2024 09:31
-
-
Save TobseF/5b50fe0236dc97f12dc98a6ea8e85d11 to your computer and use it in GitHub Desktop.
Test to demonstrate, that it's not possible to read a map which a generic value type as key.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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<Index, Int> = mutableMapOf()) | |
data class FooField(val index: Index) | |
data class FooMapValue(val map: MutableMap<Int, Index> = mutableMapOf()) | |
data class FooList(val list: MutableList<Index> = 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<FooMapKey>(fooJson) | |
assertEquals(foo, readValue) | |
} | |
@Test | |
fun parseFooMapEmptyTest() { | |
val mapper = jacksonObjectMapper().registerKotlinModule() | |
val foo = FooMapKey() | |
val fooJson = mapper.writeValueAsString(foo) | |
val readValue = mapper.readValue<FooMapKey>(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<FooField>(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<FooList>(fooJson) | |
assertEquals(foo, readValue) | |
} | |
@Test | |
fun parseFooListEmptyTest() { | |
val mapper = jacksonObjectMapper().registerKotlinModule() | |
val foo = FooList() | |
val fooJson = mapper.writeValueAsString(foo) | |
val readValue = mapper.readValue<FooList>(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<FooMapValue>(fooJson) | |
assertEquals(foo, readValue) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment