Skip to content
This repository has been archived by the owner on Oct 19, 2024. It is now read-only.

crash when encoding classes that have multiple CodingKeys because of inheritance #19

Open
meeloo opened this issue Sep 22, 2021 · 0 comments

Comments

@meeloo
Copy link

meeloo commented Sep 22, 2021

Hi!

I have a bit of inheritance in my codable objects and MessagePack crashes when encoding one of these. I could simplify the code to reproduce the crash to this:

func testMessagePack() {
        do {
            let testerB = TesterB()

            let encoder = MessagePackEncoder()
            let data = try encoder.encode(testerB)
        } catch {
            return
        }
    }
}

class TesterA: Codable {
    var fun = 1
}

class TesterB: TesterA {
    enum CodingKeys: CodingKey {
        case foo
    }
    var foo = 2

    override func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: CodingKeys.self)
        try container.encode(foo, forKey: .foo)
        try super.encode(to: encoder)
    }
}

I checked the code and saw there could be only one container. What would be the best way to add support for any number of containers? I was thinking of just storing an array of them and just set container to the array.last, what do you think?

Cheers,

S.

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

No branches or pull requests

1 participant