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-5431] JSON does not Encode/Decode subclass properties #4278

Closed
swift-ci opened this issue Jul 11, 2017 · 10 comments
Closed

[SR-5431] JSON does not Encode/Decode subclass properties #4278

swift-ci opened this issue Jul 11, 2017 · 10 comments

Comments

@swift-ci
Copy link
Contributor

Previous ID SR-5431
Radar None
Original Reporter JE Miller (JIRA User)
Type Bug
Status Resolved
Resolution Duplicate

Attachment: Download

Environment

Xcode 9.0 beta. Swift 4. Mac OSX 10.12.5

Additional Detail from JIRA
Votes 0
Component/s Foundation
Labels Bug
Assignee None
Priority Medium

md5: 1e8b12f9f95b483d1f9167dfda98f90c

duplicates:

  • SR-4772 Classes conforming to Codable need to synthesize overrides instead of inheriting conformance

Issue Description:

Using the simplest possible class structure and defaulting everything, subclass String properties are not encoded nor decoded. Base class properties no problem. Here is the class structure, the test code which demonstrates the problem is given as an attachment. This is a much simpler case than SR-4772 and SR-5125.

class Product: Codable {
var family: String = ""
}
class SubProduct: Product {
var name: String = ""
}

@belkadan
Copy link

I think SR-4772 tracks this fine. You can still override the encoding and decoding members yourself, so it's not a blocking issue.

@swift-ci
Copy link
Contributor Author

Comment by John E. Miller (JIRA)

It remains a problem unless I am overriding incorrectly. Indeed, now I get an EXC_BAD_INSTRUCTION on encode. Decode just fails to decode the subclass property as before.

@belkadan
Copy link

cc @itaiferber

@itaiferber
Copy link
Contributor

SR-4772 does track this correctly. There are issues with the class case that I'm fixing now — overriding also has issues.

JE Miller (JIRA User) Can you please provide the failing code?

@swift-ci
Copy link
Contributor Author

Comment by John E. Miller (JIRA)

I added new class and subclass definition as Products.swift and new version of test code SerializationTestTests.swift.

@itaiferber
Copy link
Contributor

JE Miller (JIRA User) The crash you're seeing is because you're passing your encoder directly to your superclass via try super.encode(to: encoder) instead of, say, try super.encode(to: container.superEncoder()). While not necessarily recommended, this is supported, and was fixed with PR-10538 — which build of Swift are you running?

@swift-ci
Copy link
Contributor Author

Comment by John E. Miller (JIRA)

Thanks, the call to encoder via container does not crash; I put the call last. And the result is better:
Subproduct: {"name":"marshmallows","super":{"family":"product family name"}}

Although I hadn't expected the use of "super" as another level, it makes sense. Though I'm not sure how this will play out in the decode side. Or over multiple levels of subclassing. I suppose I will need to do something similar for my init with decode as well?

I'm embarrassed to admit that I don't know which build of Swift I am running.

@itaiferber
Copy link
Contributor

JE Miller (JIRA User) Yes, you'll need to do something similar in decode: try super.init(from: container.superDecoder()) — this will look for the info stored for the "super" key and hand the data off to your superclass.

For multiple levels of subclassing, you can expect to see the following structure:

{
    sub-subclass properties...,
    "super":
    {
        subclass properties...,
        "super": { base class properties }
    }
}

And no worries — just wanted to make sure this isn't still reproducing on the latest builds of Swift. If you get Xcode 9 beta 3 or one of the latest toolchains, you shouldn't see that particular crash anymore.

@swift-ci
Copy link
Contributor Author

Comment by John E. Miller (JIRA)

Cool. Xcode 9 beta 3 works fine with my subclass example - both encoding and decoding.

@itaiferber
Copy link
Contributor

Great! The core issue here still needs to be resolved, but glad this case works for you now.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@shahmishal shahmishal transferred this issue from apple/swift May 5, 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

3 participants