Skip to content

Commit

Permalink
Deserialize DokkaConfiguration's Set as LinkedHashSet (#3006)
Browse files Browse the repository at this point in the history
Fixes #2999

Helps preserve order of Set elements.

Co-authored-by: 박은우/게임플랫폼클라팀/NE <eunwoo.park@nhn.com>
  • Loading branch information
eunwoop and eunwoo-park-nhn committed Jul 12, 2023
1 parent 54decbe commit 20e06e8
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
1 change: 1 addition & 0 deletions core/src/main/kotlin/utilities/json.kt
Expand Up @@ -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)
Expand Down
19 changes: 19 additions & 0 deletions 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
Expand Down Expand Up @@ -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.
Expand All @@ -55,3 +70,7 @@ data class SimpleTestDataClass(
val someIntWithDefaultValue: Int = 42,
val someDouble: Double
)

data class SimpleTestSetDataClass(
val someStringSet: Set<String>
)

0 comments on commit 20e06e8

Please sign in to comment.