Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JS serialization fails for empty lists with non-primitive types #613

Open
Daeda88 opened this issue Sep 2, 2024 · 0 comments
Open

JS serialization fails for empty lists with non-primitive types #613

Daeda88 opened this issue Sep 2, 2024 · 0 comments

Comments

@Daeda88
Copy link
Contributor

Daeda88 commented Sep 2, 2024

@Test
    fun encodeDecodeNestedClassWithEmptyCollections() {
        val module = SerializersModule {
            polymorphic(AbstractClass::class, AbstractClass.serializer()) {
                subclass(ImplementedClass::class, ImplementedClass.serializer())
            }
        }

        val testData = TestData(mapOf(), mapOf(), true, null, ValueClass(42))
        val sealedClass: SealedClass = SealedClass.Test("value")
        val abstractClass: AbstractClass = ImplementedClass("value", true)
        val nestedClass = NestedClass(testData, sealedClass, abstractClass, listOf(), listOf(), listOf(), mapOf(), mapOf(), mapOf())
        val encoded = encode(NestedClass.serializer(), nestedClass) {
            encodeDefaults = true
            serializersModule = module
        }

        val testDataEncoded = nativeMapOf("map" to nativeMapOf(), "otherMap" to nativeMapOf(), "bool" to true, "nullableBool" to null, "valueClass" to 42)
        val sealedEncoded = nativeMapOf("type" to "test", "value" to "value")
        val abstractEncoded = nativeMapOf("type" to "implemented", "abstractValue" to "value", "otherValue" to true)
        nativeAssertEquals(
            nativeMapOf(
                "testData" to testDataEncoded,
                "sealed" to sealedEncoded,
                "abstract" to abstractEncoded,
                "testDataList" to nativeListOf(),
                "sealedList" to nativeListOf(),
                "abstractList" to nativeListOf(),
                "testDataMap" to nativeMapOf(),
                "sealedMap" to nativeMapOf(),
                "abstractMap" to nativeMapOf(),
            ),
            encoded,
        )

        val decoded = decode(NestedClass.serializer(), encoded) {
            serializersModule = module
        }
        assertEquals(nestedClass, decoded)
    }

This code fails on JS due to a wrong implementation of the List Serializer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant