You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
swift-ci opened this issue
Mar 30, 2019
· 2 comments
Labels
bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.CodableArea → standard library: `Codable` and co.compilerThe Swift compiler in itself
Given a JSON object with a nullable field, such as
{
"count": null//may be string or null
}
If you try to decode it into this struct
structSample: Decodable {
varcount: String = "0"//let's default to 0 if null
}
You'll get the error
valueNotFound(Swift.String, Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: "count", intValue: nil)], debugDescription: "Expected String value but found null instead.", underlyingError: nil))
I expected no error, and count would be the value if non-null, otherwise "0".
I actually first tried defining count as a let, which worked without error, but this introduced a bug because it will always be that "default" value even when count is non-null.
The text was updated successfully, but these errors were encountered:
I'm afraid that's just not the behavior of the default init(coder:), and it would be a breaking change to make it so. cc bendjones (JIRA User), @itaiferber
We can't read into the intent of why default values are supplied. Sometimes, you'd really want the default value to be used in case decoding fails, but sometimes, you just define a default value to give your type a synthesized init without even considering what that might do for Codable synthesis. There's no way for us to guess correctly, so we do the conservative thing of always trying to decode and not squashing the error silently.
bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.CodableArea → standard library: `Codable` and co.compilerThe Swift compiler in itself
Environment
Swift 5
Additional Detail from JIRA
md5: e4633977df6db435fe2813d574985f12
Issue Description:
Given a JSON object with a nullable field, such as
If you try to decode it into this struct
You'll get the error
I expected no error, and count would be the value if non-null, otherwise "0".
I actually first tried defining count as a let, which worked without error, but this introduced a bug because it will always be that "default" value even when count is non-null.
The text was updated successfully, but these errors were encountered: