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

[SR-11996] Warn when using immutable property with initial value in Codable type #54430

Closed
theblixguy opened this issue Jan 7, 2020 · 3 comments
Assignees
Labels
Codable Area → standard library: `Codable` and co. compiler The Swift compiler in itself diagnostics QoI Bug: Diagnostics Quality of Implementation improvement

Comments

@theblixguy
Copy link
Collaborator

Previous ID SR-11996
Radar rdar://problem/58455438
Original Reporter @theblixguy
Type Improvement
Status Resolved
Resolution Done
Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Improvement, Codable, DiagnosticsQol
Assignee @theblixguy
Priority Medium

md5: b4a7e5afcaedc2fa993c06d396d468ee

Issue Description:

If we have an immutable property with an initial value in a Codable type, then decoding the type will not set the value of the property to the decoded value. For example:

struct Foo: Codable {
  let bar: Int = 0
}

let decoder = JSONDecoder()
let foo = try! decoder.decode(Foo.self, from: Data("""
{
  "bar": 1,
}
""".utf8))
print(foo.bar) // 0

This is expected, because bar's value cannot be overwritten, however combined with the "magical" behaviour of Codable, this can be very confusing to many people.

We should offer a warning, along the lines of property 'foo' will not be decoded because it's immutable with an initial value; explicitly define a CodingKeys enum {with|without} the property, along with a fix-it to make the property mutable. This will make the behaviour more explicit to the users.

The {with|without} part depends on whether the type is only Decodable, in which case the property should not be included in the enum. If it's Encodable (or both Encodable/Decodable) then the property should be included.

@beccadax
Copy link
Contributor

beccadax commented Jan 9, 2020

@swift-ci create

@theblixguy
Copy link
Collaborator Author

Fixed on master by #30218

@swift-ci
Copy link
Collaborator

Comment by Ondrej Hanslik (JIRA)

I believe this should never have been a warning. Creating warnings for code that is completely valid and widely used only because someone could find it confusing is just wrong.
This is what documentation is for. We won't stop using such code, we will only have to find a bad workaround.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@shahmishal shahmishal added diagnostics QoI Bug: Diagnostics Quality of Implementation and removed DiagnosticsQol labels Apr 26, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Codable Area → standard library: `Codable` and co. compiler The Swift compiler in itself diagnostics QoI Bug: Diagnostics Quality of Implementation improvement
Projects
None yet
Development

No branches or pull requests

4 participants