Uploaded image for project: 'Realm Cocoa SDK'
  1. Realm Cocoa SDK
  2. RCOCOA-1036

RealmOptional properties fail to be decoded if the key is not present

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Minor - P4 Minor - P4
    • Q2-22FY
    • Affects Version/s: None
    • Component/s: None

      Goals

      I'd like to use make a realm model that has a RealmOptional property conform to Codable, and properly encode/decode properties in all scenarios.

      Expected Results

      I expected my object to be encodable and decodable when the key:value pairs are present, existing, or null, in a JSON structure.

      Actual Results

      Scenario 1: It works when the RealmOptional value is present.
      Scenario 2: It works when the RealmOptional value is null.
      Scenario 3: It does not work when the RealmOptional is not present.

      Steps for others to Reproduce

      <details><summary>Scenario 1 (it works)</summary>
      <p>

      Given the class:

      class MyObject : Object, Decodable {
          dynamic var myOptional = RealmOptional<Double>()
      }
      

      And the following JSON:

      {
          "myOptional" : 42.42
      }
      

      Decoding the JSON into MyObject.self works as expected.

      </p>
      </details>

      <details><summary>Scenario 2 (it works)</summary>
      <p>

      Given the class:

      class MyObject : Object, Decodable {
          dynamic var myOptional = RealmOptional<Double>()
      }
      

      And the following JSON:

      {
          "myOptional" : null
      }
      

      Decoding the JSON into MyObject.self works as expected.

      </p>
      </details>

      <details><summary>Scenario 3 (it doesn't work)</summary>
      <p>

      Given the class:

      class MyObject : Object, Decodable {
          dynamic var myOptional = RealmOptional<Double>()
      }
      

      And the following JSON:

      {
      }
      

      Decoding the JSON into MyObject.self fails, because the key cannot be found.

      </p>
      </details>

      Code Sample

      Code samples are located in the section below.

      Version of Realm and Tooling

      Realm framework version: 10.3.0

      Realm Object Server version: idk

      Xcode version: 12.2

      iOS/OSX version: macOS 10.15.7

      Dependency manager + version: SPM

      Further Details

      This issue was already raised over 3 years ago here: https://github.com/realm/realm-cocoa/issues/5012#issuecomment-326252157
      I read that entire thread and the issue I'm referring to doesn't seem to have a closure. I checked this code posted here but that seemed to be an overkill to solve the problem. Maybe 3.5 years later there's a better solution.

            Assignee:
            thomas.goyne@mongodb.com Thomas Goyne
            Reporter:
            unitosyncbot Unito Sync Bot
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: