From 20e06e878ebbe09865ff36d64ff113fd4cff8905 Mon Sep 17 00:00:00 2001 From: eunwoo-park Date: Thu, 13 Jul 2023 00:12:23 +0900 Subject: Deserialize DokkaConfiguration's Set as LinkedHashSet (#3006) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes #2999 Helps preserve order of Set elements. Co-authored-by: 박은우/게임플랫폼클라팀/NE --- core/src/main/kotlin/utilities/json.kt | 1 + core/src/test/kotlin/utilities/JsonKtTest.kt | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/core/src/main/kotlin/utilities/json.kt b/core/src/main/kotlin/utilities/json.kt index eb666d31..c082de2c 100644 --- a/core/src/main/kotlin/utilities/json.kt +++ b/core/src/main/kotlin/utilities/json.kt @@ -13,6 +13,7 @@ import com.fasterxml.jackson.core.type.TypeReference as JacksonTypeReference private val objectMapper = run { val module = SimpleModule().apply { addSerializer(FileSerializer) + addAbstractTypeMapping(Set::class.java, LinkedHashSet::class.java) } jacksonObjectMapper() .registerModule(module) diff --git a/core/src/test/kotlin/utilities/JsonKtTest.kt b/core/src/test/kotlin/utilities/JsonKtTest.kt index 301f4f5d..a1d432d9 100644 --- a/core/src/test/kotlin/utilities/JsonKtTest.kt +++ b/core/src/test/kotlin/utilities/JsonKtTest.kt @@ -1,5 +1,6 @@ package utilities +import org.jetbrains.dokka.utilities.parseJson import org.jetbrains.dokka.utilities.serializeAsCompactJson import org.jetbrains.dokka.utilities.serializeAsPrettyJson import kotlin.test.Test @@ -42,6 +43,20 @@ class JsonTest { assertEquals(expected, actual) } + @Test + fun `should keep order of Set after serialize and deserialize`() { + val testObject = SimpleTestSetDataClass( + someStringSet = setOf("Foo", "Bar", "ABC") + ) + val expected = testObject.someStringSet.toList() // ["Foo", "Bar", "ABC"] + + val jsonString = serializeAsCompactJson(testObject) + val parsedClass: SimpleTestSetDataClass = parseJson(jsonString) + val actual = parsedClass.someStringSet.toList() + + assertEquals(expected, actual) + } + /** * If the expected output was generated on Linux, but the tests are run under Windows, * the test might fail when comparing the strings due to different separators. @@ -55,3 +70,7 @@ data class SimpleTestDataClass( val someIntWithDefaultValue: Int = 42, val someDouble: Double ) + +data class SimpleTestSetDataClass( + val someStringSet: Set +) -- cgit