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

[Bug] superDecoder(forKey:) doesn’t work as documented #314

Open
soumyamahunt opened this issue Nov 13, 2023 · 4 comments
Open

[Bug] superDecoder(forKey:) doesn’t work as documented #314

soumyamahunt opened this issue Nov 13, 2023 · 4 comments
Assignees

Comments

@soumyamahunt
Copy link

When trying to decode a Codable model with following implementation:

struct SomeCodable {
    let value1: String?
}

extension SomeCodable: Decodable {
    init(from decoder: any Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)
        if let key1_containerDecoder = try? container.superDecoder(forKey: CodingKeys.key1) {
            let key1_container = try key1_containerDecoder.container(keyedBy: CodingKeys.self)
            self.value1 = try key1_container.decodeIfPresent(String.self, forKey: CodingKeys.value1)
        } else {
            self.value1 = nil
        }
    }
}

extension SomeCodable: Encodable {
    func encode(to encoder: any Encoder) throws {
        var container = encoder.container(keyedBy: CodingKeys.self)
        var key1_container = container.nestedContainer(keyedBy: CodingKeys.self, forKey: CodingKeys.key1)
        try key1_container.encodeIfPresent(self.value1, forKey: CodingKeys.value1)
    }
}

extension SomeCodable {
    enum CodingKeys: String, CodingKey {
        case value1 = "key2"
        case key1 = "key1"
    }
}

and empty JSON object({}) error is thrown at

let key1_container = try key1_containerDecoder.container(keyedBy: CodingKeys.self)

valueNotFound(Swift.KeyedDecodingContainer<MetaCodableTests.SomeCodable.CodingKeys>, Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: "key1", intValue: nil)], debugDescription: "Cannot get keyed decoding container -- found null value instead.", underlyingError: nil))"

instead of

if let key1_containerDecoder = try? container.superDecoder(forKey: CodingKeys.key1) 

which is diferrent than documented.

As per discussion in forum, both older and new Foundation implementation seems to have this bug.

@kperryua
Copy link
Member

Yep, this behaves counter to the documentation. And failing sooner does make sense to me as opposed to deferring it.

The forum thread discusses the low likelihood of regression from clients relying on the existing behavior. Normally I'd tend to agree, but during the swift-foundation reimplementation of the entire API surface, I was repeatedly struck by breakages from odd client behavior and expectation. There's a large number of clients of this API, so even a small percentage chance of this happening makes the expected number of real-world regressions probably non-zero.

@glessard glessard assigned glessard and unassigned kperryua Nov 13, 2023
@glessard
Copy link
Contributor

glessard commented Nov 13, 2023

Agreed! The important thing is to match the behaviour from macOS 13 and earlier. (rdar://118337978, for reference.)

@kperryua
Copy link
Member

And that is indeed what's happening here. Again, I'm not opposed to trying this out, but we may end up being forced to explore compatibility techniques to push the change through for the general population.

@soumyamahunt
Copy link
Author

@kperryua @glessard meanwhile is there any current workaround that can be used to have the functionality mentioned in the docs?

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

3 participants