Uploaded image for project: 'Couchbase Lite'
  1. Couchbase Lite
  2. CBL-4582

MutableDocument contains(key: String) returns wrong result

    XMLWordPrintable

Details

    • Bug
    • Resolution: Fixed
    • Major
    • Beryllium
    • 3.0.2
    • iOS
    • Security Level: Public
    • None
    • C-iOS 108
    • 1

    Description

      From slack:

      Hi team, A quick follow up question on whether this is a bug. When customer tried out MutableDocument, it had some unexpected behavior. It has: func contains(key: String) -> Bool. For some reason, MutableDocument's "contains(key" will return false for a bool key when the bool's value is false. This is despite its "keys" property definitely containing that key. It does not work this way for MutableDictionaryObject. Customer is wondering why? Note: I am not sure if this contains function is supported given that it is not listed in the API reference: https://docs.couchbase.com/mobile/3.0.2/couchbase-lite-swift/Classes/MutableDocument.htmlHere's example XCTest code below that repros issue:

       

      struct ExampleObject: Codable, Equatable {
              let what: String
          }
       
          struct ExampleStructure: Codable, Equatable {
              let hello: String
              let `time`: Int
              let `float`: Double
              let `boolean`: Bool
              let otherbool: Bool
              let null: String?
              let obj: ExampleObject
              let arr: [Int]
          }
       
          func testExample() throws {
              let exampleObj = ExampleObject(what: "that")
              var exampleStructure = ExampleStructure(hello: "world", time: 12345, float: 1, boolean: true, otherbool: false, null: nil, obj: exampleObj, arr: [1, 2, 3])
              
              var jsonData = try JSONEncoder().encode(exampleStructure)
              var json = try XCTUnwrap(String(data: jsonData, encoding: .utf8))
              var exampleDoc = try MutableDocument(json: json)
              
              print(exampleDoc)
              XCTAssertTrue(exampleDoc.contains(key: "otherbool")) // Fails
              XCTAssertTrue(exampleDoc.keys.contains("otherbool"))
              XCTAssertTrue(exampleDoc.contains(key: "boolean"))
              XCTAssertTrue(exampleDoc.keys.contains("boolean"))
              
              exampleStructure = ExampleStructure(hello: "world", time: 12345, float: 1, boolean: false, otherbool: true, null: nil, obj: exampleObj, arr: [1, 2, 3])
              
              jsonData = try JSONEncoder().encode(exampleStructure)
              json = try XCTUnwrap(String(data: jsonData, encoding: .utf8))
              exampleDoc = try MutableDocument(json: json)
              
              XCTAssertTrue(exampleDoc.contains(key: "otherbool"))
              XCTAssertTrue(exampleDoc.keys.contains("otherbool"))
              XCTAssertTrue(exampleDoc.contains(key: "boolean")) // Fails
              XCTAssertTrue(exampleDoc.keys.contains("boolean"))
       
              let exampleDict = try MutableDictionaryObject(json: json)
              
              XCTAssertTrue(exampleDict.contains(key: "otherbool"))
              XCTAssertTrue(exampleDict.keys.contains("otherbool"))
              XCTAssertTrue(exampleDict.contains(key: "boolean"))
              XCTAssertTrue(exampleDict.keys.contains("boolean"))
          } 

      Attachments

        Issue Links

          Activity

            People

              pasin Pasin Suriyentrakorn
              pasin Pasin Suriyentrakorn
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                PagerDuty