Skip to content

Commit 5dbbf85

Browse files
committed
chore: Added test for the fix
1 parent 87a5d6c commit 5dbbf85

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

formats/hocon/src/main/kotlin/kotlinx/serialization/hocon/Hocon.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ public sealed class Hocon(
145145

146146
}
147147

148-
private inner class ConfigReader(val conf: Config, private val poly: Boolean = false) : ConfigConverter<String>() {
148+
private inner class ConfigReader(val conf: Config, private val isPolymorph: Boolean = false) : ConfigConverter<String>() {
149149
private var ind = -1
150150

151151
override fun decodeElementIndex(descriptor: SerialDescriptor): Int {
@@ -162,7 +162,7 @@ public sealed class Hocon(
162162
if (parentName.isEmpty()) childName else "$parentName.$childName"
163163

164164
override fun SerialDescriptor.getTag(index: Int): String {
165-
return if (!poly) composeName(
165+
return if (!isPolymorph) composeName(
166166
currentTagOrNull.orEmpty(),
167167
getConventionElementName(index, useConfigNamingConvention)
168168
) else getElementName(index)
@@ -216,7 +216,7 @@ public sealed class Hocon(
216216
override fun beginStructure(descriptor: SerialDescriptor): CompositeDecoder =
217217
when {
218218
// Polymorph should always be object-like I believe?
219-
descriptor.kind.objLike -> ConfigReader(conf, true)
219+
descriptor.kind.objLike -> ConfigReader(conf, isPolymorph = true)
220220
else -> this
221221
}
222222

formats/hocon/src/test/kotlin/kotlinx/serialization/hocon/HoconPolymorphismTest.kt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ class HoconPolymorphismTest {
2323
data class AnnotatedTypeChild(@SerialName("my_type") val type: String) : Sealed(3)
2424
}
2525

26+
@Serializable
27+
data class SealedContainer(val sealed: Collection<Sealed>)
28+
2629
@Serializable
2730
data class CompositeClass(var sealed: Sealed)
2831

@@ -102,4 +105,25 @@ class HoconPolymorphismTest {
102105
serializer = Sealed.serializer(),
103106
)
104107
}
108+
109+
@Test
110+
fun testCollectionContainer() {
111+
objectHocon.assertStringFormAndRestored(
112+
expected = """
113+
sealed = [
114+
{ type = annotated_type_child, my_type = override, intField = 3 }
115+
{ type = object }
116+
{ type = data_class, name = testDataClass, intField = 1 }
117+
]
118+
""".trimIndent(),
119+
original = SealedContainer(
120+
listOf(
121+
Sealed.AnnotatedTypeChild(type = "override"),
122+
Sealed.ObjectChild,
123+
Sealed.DataClassChild(name = "testDataClass"),
124+
)
125+
),
126+
serializer = SealedContainer.serializer(),
127+
)
128+
}
105129
}

0 commit comments

Comments
 (0)