Skip to content

[Bug] enum cases are not encoded unless type specified #157

@Amzd

Description

@Amzd

Describe the bug

Encoding of enums is missing

@Codable
enum Foo {
    case test
}
// generates:
extension Foo: Decodable {
    init(from decoder: any Decoder) throws {
        let container = try decoder.container(keyedBy: DecodingKeys.self)
        guard container.allKeys.count == 1 else {
            let context = DecodingError.Context(
                codingPath: container.codingPath,
                debugDescription: "Invalid number of keys found, expected one."
            )
            throw DecodingError.typeMismatch(Self.self, context)
        }
        let contentDecoder = try container.superDecoder(forKey: container.allKeys.first.unsafelyUnwrapped)
        switch container.allKeys.first.unsafelyUnwrapped {
        case DecodingKeys.test:
            self = .test
        }
    }
}

extension Foo: Encodable {
    func encode(to encoder: any Encoder) throws {
        var container = encoder.container(keyedBy: CodingKeys.self)
        switch self {
        case .test:
            break
        }
    }
}

extension Foo {
    enum CodingKeys: String, CodingKey {
        case test = "test"
    }
    enum DecodingKeys: String, CodingKey {
        case test = "test"
    }
}

which means Foo.test just encodes to "{}" which is not useful and asymmetric to how it decodes

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions