https://kotlinlang.org logo
Title
a

andrew

01/31/2023, 11:03 PM
EDIT: Looks like there's an open issue here: https://github.com/Kotlin/kotlinx.serialization/issues/2132 Is there a clean way to approach this without a surrogate class?
@Serializable
class DormaKabaKey(private val file: NeonFile) : Key<NeonFile>, NeonFile by file {

    @SerialName("fileId")
    private val fileId: ByteArray? = file.fileId
    @SerialName("fileDefinitionName")
    private val fileDefinitionName: String? = file.fileDefinitionName

    override fun getFileId() = fileId
    override fun hasFileId() = fileId !== null

    override fun getFileDefinitionName() = fileDefinitionName
    override fun hasFileDefinitionName() = fileDefinitionName !== null
}
For context, I'm using a proprietary library with NO caching support which is why I'm serializing their door lock credential type, and I have omitted many fields The better way to put it, I don't care about NeonFile or the impl, only my class and its values where it'll be then be reconstructed when being deserialized.
Stack trace:
kotlinx.serialization.SerializationException: Class 'b' is not registered for polymorphic serialization in the scope of 'NeonFile'.
                                                                                                    Mark the base class as 'sealed' or register the serializer explicitly.
                                                                                                    	at kotlinx.serialization.internal.AbstractPolymorphicSerializerKt.throwSubtypeNotRegistered(AbstractPolymorphicSerializer.kt:102)
                                                                                                    	at kotlinx.serialization.internal.AbstractPolymorphicSerializerKt.throwSubtypeNotRegistered(AbstractPolymorphicSerializer.kt:113)
                                                                                                    	at kotlinx.serialization.PolymorphicSerializerKt.findPolymorphicSerializer(PolymorphicSerializer.kt:109)
                                                                                                    	at kotlinx.serialization.internal.AbstractPolymorphicSerializer.serialize(AbstractPolymorphicSerializer.kt:32)
                                                                                                    	at kotlinx.serialization.protobuf.internal.ProtobufEncoder.encodeSerializableValue(ProtobufEncoding.kt:138)
                                                                                                    	at kotlinx.serialization.protobuf.internal.ProtobufTaggedEncoder.encodeSerializableElement(ProtobufTaggedEncoder.kt:135)
                                                                                                    	at com.stratisiot.stratissdk.keyring.key.DormaKabaKey.write$Self(DormaKabaKey.kt:8)
                                                                                                    	at com.stratisiot.stratissdk.keyring.key.DormaKabaKey$$serializer.serialize(DormaKabaKey.kt:8)
                                                                                                    	at com.stratisiot.stratissdk.keyring.key.DormaKabaKey$$serializer.serialize(DormaKabaKey.kt:8)
                                                                                                    	at kotlinx.serialization.protobuf.internal.ProtobufEncoder.encodeSerializableValue(ProtobufEncoding.kt:138)
                                                                                                    	at kotlinx.serialization.protobuf.ProtoBuf.encodeToByteArray(ProtoBuf.kt:130)
The nice thing about using delegation like this is that I do not need to reference the file property at all, it's still a LEGIC Neon File 🙂
p

pdvrieze

02/01/2023, 2:14 PM
At the very least it requires
NeonFile
to be serializable/have a serializer (which it doesn't appear to be based upon your posted errors).
a

andrew

02/01/2023, 2:25 PM
NeonFile itself is an interface but the delegation provides a proprietary implementation, yes
p

pdvrieze

02/01/2023, 2:45 PM
You will have to explicitly mark the
file
property as
Serializable
with the type of the actual serializer to use (the proprietary one should work correctly due to erasure, but you could write your own little wrapper).
a

andrew

02/01/2023, 2:53 PM
Or transient, could work? fails compile check, I'll try this suggestion
looks like I'll need to make a wrapper