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

RealmProperty cannot decode null values #7323

Closed
miklselsoe opened this issue Jul 5, 2021 · 3 comments · Fixed by #7328
Closed

RealmProperty cannot decode null values #7323

miklselsoe opened this issue Jul 5, 2021 · 3 comments · Fixed by #7328
Assignees

Comments

@miklselsoe
Copy link

miklselsoe commented Jul 5, 2021

After RealmOptional was deprecated, I changed all my RealmOptional<Type> properties to RealmProperty<Type?>

I have a few places where I relied on RealmOptional being Codable. With RealmProperty also being Codable, I thought it was safe to change the property definitions. Unfortunately, while RealmOptional was able to decode null values, RealmProperty cannot:

Swift.DecodingError.valueNotFound(Swift.Optional<Swift.Double>, Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: "fromKm", intValue: nil)], debugDescription: "Expected Optional<Double> but found null value instead.", underlyingError: nil))

Looking at the implementation for the two types, it is evident that RealmProperty ignores null values, while RealmOptional does not:

extension RealmProperty: Codable where Value: Codable {
    public convenience init(from decoder: Decoder) throws {
        self.init()
        // `try decoder.singleValueContainer().decode(Value?.self)` incorrectly
        // rejects null values: https://bugs.swift.org/browse/SR-7404
        let container = try decoder.singleValueContainer()
        self.value = try container.decode(Value.self)
    }

    public func encode(to encoder: Encoder) throws {
        try self.value.encode(to: encoder)
    }
}https://github.com/realm/realm-cocoa/blob/v10.9.0/RealmSwift/RealmProperty.swift#L70-L76

extension RealmOptional: Codable where Value: Codable {
    public convenience init(from decoder: Decoder) throws {
        self.init()
        // `try decoder.singleValueContainer().decode(Value?.self)` incorrectly
        // rejects null values: https://bugs.swift.org/browse/SR-7404
        let container = try decoder.singleValueContainer()
        self.value = container.decodeNil() ? nil : try container.decode(Value.self)
    }

    public func encode(to encoder: Encoder) throws {
        try self.value.encode(to: encoder)
    }
}https://github.com/realm/realm-cocoa/blob/v10.9.0/RealmSwift/Optional.swift#L78-L84

I think this is a bug. I wonder if there is a better workaround than reverting to RealmOptional?

Version of Realm and Tooling

Realm framework version: 10.8.0

Xcode version: 12.5.1

iOS/OSX version: 14.6

Dependency manager + version: Carthage 0.38.0

@leemaguire
Copy link
Contributor

Hi @miklselsoe

I will look into this for you.

@miklselsoe
Copy link
Author

Are these two issues related?
#7332

@leemaguire
Copy link
Contributor

Hi @miklselsoe The problems are not interdependant on one another.

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

Successfully merging a pull request may close this issue.

2 participants