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

SIGABRT when accessing dot separated keypath on non-object fileds #133

Open
Sega-Zero opened this issue Oct 26, 2018 · 0 comments
Open

SIGABRT when accessing dot separated keypath on non-object fileds #133

Sega-Zero opened this issue Oct 26, 2018 · 0 comments

Comments

@Sega-Zero
Copy link

This construction

extension NSDictionary: MarshaledObject {
    public func any(for key: KeyType) throws -> Any {
        guard let value: Any = self.value(forKeyPath: key.stringValue) else {
            throw MarshalError.keyNotFound(key: key)
        }

is not really safe to use in case when we try to access a field that is not an object.
Say, we have this json:

{
	"contact_id": "123456",
	"type": 0,
	"avatar": "",
	"org": {"name": "name"},
}

And this model:

struct Test: Unmarshaling {
    let identifier: String
    let orgName: String?
    init(object: MarshaledObject) throws {
        self.identifier = try object.value(for: "contact_id")
        self.name = try object.value(for: "org.org_name")
    }
}

Sometimes, by accident, server may return this json:

{
	"contact_id": "123456",
	"type": 0,
	"avatar": "",
	"org": "",
}

In that case I get SIGABRT:
marshaldictionary swift 2018-10-26 20-45-39

Right now I have to workaround it by implementing a private struct for org field and map to it first, but it would be better if the code above would just return nil without crashing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant