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-7210] Codable decoder issue when inheriting from a Class conforming to Codable #49758

Open
swift-ci opened this issue Mar 15, 2018 · 3 comments
Assignees
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. Codable Area → standard library: `Codable` and co. compiler The Swift compiler in itself

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-7210
Radar rdar://problem/34819823
Original Reporter barakrl (JIRA User)
Type Bug
Status Reopened
Resolution

Attachment: Download

Environment

Version 9.2 (9C40b)

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

md5: a81e1c99a230bb85f5defb9c13fedb34

relates to:

  • SR-7090 Crashing with EXC_BAD_ACCESS after decoding Codable subclass

Issue Description:

When a class inherits from a base class that conforms to Codable, the decoding is not executed (more on that below) and the returned object has the default properties values.

class BaseClass: Codable {}

class MyClass: BaseClass {

    enum CodingKeys: String, CodingKey {
        case objectId = "id"
        case name
    }
    
    var objectId: Int = -1
    var name: String = ""
}

let dataString = "{\"id\":123,\"name\":\"test object\"}"
let data = dataString.data(using: .utf8)!

do {
    let object = try JSONDecoder().decode(MyClass.self, from: data)
    print(object.objectId) //outputs -1 or BAD_ACCESS (and not 123 as expected)
}
catch {
    print(error)
}

But, if I use either:

protocol Base: Codable { //protocol "base"
}

class MyClass: BaseClass {
...
}

The decoding is preformed as expected.

@swift-ci
Copy link
Collaborator Author

Comment by Barak Harel (JIRA)

It's probably a duplicate of SR-7090, but not 100% sure.

@belkadan
Copy link
Contributor

Huh. @itaiferber, it looks like there's no JIRA for just the very base issue yet. I know you do have a Radar for it.

@itaiferber
Copy link
Contributor

This is a combination of two issues. The EXC_BAD_ACCESS is indeed SR-7090 & co.; the display of -1 would be caused by MyClass inheriting Codable from BaseClass, since we can't re-synthesize. That issue is being tracked by the now associated Radar; we need to make it clearer that that's what's happening — likely have a warning (with a mechanism to disable) that lets you know what's going on.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. Codable Area → standard library: `Codable` and co. compiler The Swift compiler in itself
Projects
None yet
Development

No branches or pull requests

3 participants