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-7099] Default property values in Codable conforming class take precedence when decoding JSON with those properties set #49647

Closed
swift-ci opened this issue Mar 1, 2018 · 4 comments

Comments

@swift-ci
Copy link
Collaborator

@swift-ci swift-ci commented Mar 1, 2018

Previous ID SR-7099
Radar None
Original Reporter georgealegre (JIRA User)
Type Bug
Status Resolved
Resolution Invalid
Environment

macOS High Sierra 10.3.3.

Xcode Version 9.2 (9C40b)

Apple Swift version 4.0.3 (swiftlang-900.0.74.1 clang-900.0.39.2)
Target: x86_64-apple-macosx10.9

Working on an iOS application with the Swift compiler language set to 3.2.

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, Codable
Assignee None
Priority Medium

md5: 46c3a50f989d441361b08d992ddc0f99

Issue Description:

Decoding JSON with JSONDecoder into a Codable conforming class with properties with default values doesn't work as expected. If the JSON has those properties set with a value, this value gets ignored and the default value of the property gets set.

For example, lets say I have this class:

public class Example: Codable {

{{ public let name: String = ""}}

{{}}}

And this JSON inside a Data property called data:

{name: "Some Name"}

When doing the following, the 'name' property has a value of "" and not "Some Name".

let example = try? JSONDecoder().decode(Example.self, from: data)

If I remove the default value and move it into an initializer, decoding works as expected and the 'name' property ends up with a value of "Some Name":

public class Example: Codable {
public let name: String
init() { name = "" }
}

@belkadan
Copy link
Contributor

@belkadan belkadan commented Mar 1, 2018

I think this is correct behavior. This code is also rejected:

public class Example {
    public let name: String = ""
    init(n: String) {
      name = n // error: immutable value 'self.name' may only be initialized once
    }
}

Values assigned directly at the property declaration site are not default values; they're initial values.

@swift-ci
Copy link
Collaborator Author

@swift-ci swift-ci commented Mar 1, 2018

Comment by Jorge Alegre (JIRA)

I'm sorry, I got the names wrong. Anyways, I get what you mean, your example is clearly wrong. The point is that the idea of having an initial value in the property declaration site or in the initializer is the same, but decoding only works properly when using initial values in the initializer and not in the property declaration site.

@belkadan
Copy link
Contributor

@belkadan belkadan commented Mar 1, 2018

No, that's not what I meant. You declared a property with let and gave it a value; that's its value forever. So Codable will skip it.

@swift-ci
Copy link
Collaborator Author

@swift-ci swift-ci commented Mar 1, 2018

Comment by Jorge Alegre (JIRA)

Ups, my bad. You are totally right. I guess this isn't a bug after all. Thanks for clarifying.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants