cph-cachet / carp.core-kotlin

Infrastructure-agnostic framework for distributed data collection.
https://carp.cachet.dk/core/
MIT License
21 stars 3 forks source link

Serializing "unknown types" on JS release throws exception #473

Closed jakdan99 closed 7 months ago

jakdan99 commented 7 months ago

Using the typescript declarations, encoding a StudyProtocolSnapshot which has an unknown type throws: IllegalStateException: Could not find a matching field for "polymorphicDiscriminator" on the JavaScript runtime.

Repro steps:

472

Whathecode commented 7 months ago

Thanks a lot for the repro! 🙏

Noteworthy: this works, and is tested, on the JS target platform.

I suspect the problem is related specifically to released sources, which get minified. Unknown polymorphism on the JS platform relies on accessing internals of kotlinx.serialization.

        // HACK: Modify kotlinx.serialization internals to ensure the encoder is not in polymorphic mode.
        //  Otherwise, `encoder.encodeJsonElement` encodes type information, but this is already represented in the wrapped unknown object.
        AccessInternals.setField( encoder, "polymorphicDiscriminator", null )

When the tests run with non-minified sources, however, this polymorphicDiscriminator field is renamed to something else.

This code was written quite a long time ago. We should check whether updates to kotlinx.serialization allow doing this without the HACK. But, if this is not possible, we will need to ensure the name is fixed by adding to the kotlinx.serialization facade.