diff --git a/modules/kxs-ts-gen-core/src/commonMain/kotlin/dev/adamko/kxstsgen/core/SerializerDescriptorsExtractor.kt b/modules/kxs-ts-gen-core/src/commonMain/kotlin/dev/adamko/kxstsgen/core/SerializerDescriptorsExtractor.kt index d5b01c6b..32db4d08 100644 --- a/modules/kxs-ts-gen-core/src/commonMain/kotlin/dev/adamko/kxstsgen/core/SerializerDescriptorsExtractor.kt +++ b/modules/kxs-ts-gen-core/src/commonMain/kotlin/dev/adamko/kxstsgen/core/SerializerDescriptorsExtractor.kt @@ -2,12 +2,7 @@ package dev.adamko.kxstsgen.core import dev.adamko.kxstsgen.core.util.MutableMapWithDefaultPut import kotlinx.serialization.KSerializer -import kotlinx.serialization.descriptors.PolymorphicKind -import kotlinx.serialization.descriptors.PrimitiveKind -import kotlinx.serialization.descriptors.SerialDescriptor -import kotlinx.serialization.descriptors.SerialKind -import kotlinx.serialization.descriptors.StructureKind -import kotlinx.serialization.descriptors.elementDescriptors +import kotlinx.serialization.descriptors.* /** @@ -26,6 +21,8 @@ fun interface SerializerDescriptorsExtractor { serializer: KSerializer<*> ): Set { return extractDescriptors(serializer.descriptor) + .distinctBy { it.nullable } + .toSet() } diff --git a/modules/kxs-ts-gen-core/src/commonTest/kotlin/dev/adamko/kxstsgen/core/SerializerDescriptorsExtractorTest.kt b/modules/kxs-ts-gen-core/src/commonTest/kotlin/dev/adamko/kxstsgen/core/SerializerDescriptorsExtractorTest.kt index 4c1dd3b5..83e7ab77 100644 --- a/modules/kxs-ts-gen-core/src/commonTest/kotlin/dev/adamko/kxstsgen/core/SerializerDescriptorsExtractorTest.kt +++ b/modules/kxs-ts-gen-core/src/commonTest/kotlin/dev/adamko/kxstsgen/core/SerializerDescriptorsExtractorTest.kt @@ -5,6 +5,7 @@ import io.kotest.core.spec.style.FunSpec import io.kotest.matchers.collections.shouldContainExactlyInAnyOrder import kotlinx.serialization.Serializable import kotlinx.serialization.builtins.serializer +import kotlinx.serialization.descriptors.SerialDescriptor class SerializerDescriptorsExtractorTest : FunSpec({ @@ -18,14 +19,7 @@ class SerializerDescriptorsExtractorTest : FunSpec({ val actual = SerializerDescriptorsExtractor.Default(Example1.Parent.serializer()) - withClue( - """ - expected: ${expected.map { it.serialName }.sorted().joinToString()} - actual: ${actual.map { it.serialName }.sorted().joinToString()} - """.trimIndent() - ) { - actual shouldContainExactlyInAnyOrder expected - } + actual shouldContainDescriptors expected } test("Example2: given parent class, expect subclass property descriptor extracted") { @@ -38,17 +32,36 @@ class SerializerDescriptorsExtractorTest : FunSpec({ val actual = SerializerDescriptorsExtractor.Default(Example2.Parent.serializer()) - withClue( - """ - expected: ${expected.map { it.serialName }.sorted().joinToString()} - actual: ${actual.map { it.serialName }.sorted().joinToString()} - """.trimIndent() - ) { - actual shouldContainExactlyInAnyOrder expected - } + actual shouldContainDescriptors expected } -}) + test("Example3: expect nullable/non-nullable SerialDescriptors be de-duplicated") { + + val expected = listOf( + Example3.SomeType.serializer().descriptor, + Example3.TypeHolder.serializer().descriptor, + String.serializer().descriptor, + ) + + val actual = SerializerDescriptorsExtractor.Default(Example3.TypeHolder.serializer()) + + actual shouldContainDescriptors expected + } +}) { + companion object { + private infix fun Collection.shouldContainDescriptors(expected: Collection) { + val actual = this + withClue( + """ + expected: ${expected.map { it.serialName }.sorted().joinToString()} + actual: ${actual.map { it.serialName }.sorted().joinToString()} + """.trimIndent() + ) { + actual shouldContainExactlyInAnyOrder expected + } + } + } +} @Suppress("unused") @@ -78,3 +91,17 @@ private object Example2 { @Serializable class SubClass1(val n: Nested) : SealedSub() } + + +@Suppress("unused") +private object Example3 { + + @Serializable + class SomeType(val a: String) + + @Serializable + class TypeHolder( + val required: SomeType, + val optional: SomeType?, + ) +}