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

Set Immutable Dictionary or Array to Document causes toDictionary() to crash in Swift #2104

Closed
pasin opened this Issue Mar 15, 2018 · 0 comments

Comments

Projects
None yet
1 participant
@pasin
Contributor

pasin commented Mar 15, 2018

This is reported in CBSE-4995. Here is the unit test that can cause the crash:

    func testSetImmutableDictionary() throws {
        let dict1 = MutableDictionaryObject()
        dict1.setValue("n1", forKey: "name")
        
        let doc1a = createDocument("doc1")
        doc1a.setDictionary(dict1, forKey: "dict1")
        try saveDocument(doc1a)
        
        let doc1 = db.document(withID: doc1a.id)!
        let doc1b = doc1.toMutable()
        doc1b.setDictionary(doc1.dictionary(forKey: "dict1"), forKey: "dict1b")
        
        let dict2 = MutableDictionaryObject()
        dict2.setValue("n2", forKey: "name")
        doc1b.setDictionary(dict2, forKey: "dict2")
        
        let dict: [String: Any] = ["dict1": ["name": "n1"],
                                   "dict1b": ["name": "n1"],
                                   "dict2": ["name": "n2"]]
        try saveDocument(doc1b, eval: { (d) in
            XCTAssert(d.toDictionary() == dict)
        })
    }

    func testSetImmutableArray() throws {
        let array1 = MutableArrayObject()
        array1.addString("a1")
        array1.addDictionary(MutableDictionaryObject(data: ["name": "n1"]))
        
        let doc1a = createDocument("doc1")
        doc1a.setArray(array1, forKey: "array1")
        try saveDocument(doc1a)
        
        let doc1 = db.document(withID: doc1a.id)!
        let doc1b = doc1.toMutable()
        doc1b.setArray(doc1.array(forKey: "array1"), forKey: "array1b")
        
        let array2 = MutableArrayObject()
        array2.addString("a2")
        array2.addDictionary(MutableDictionaryObject(data: ["name": "n2"]))
        doc1b.setArray(array2, forKey: "array2")
        
        let dict: [String: Any] = ["array1":  ["a1", ["name": "n1"]],
                                   "array1b": ["a1", ["name": "n1"]],
                                   "array2":  ["a2", ["name": "n2"]]]
        try saveDocument(doc1b, eval: { (d) in
            XCTAssert(d.toDictionary() == dict)
        })
    }

The issue is that the convertSETValue(_ value) function checks only MutableDictionary and MutableArray type. Instead the function should check the base type.

@pasin pasin added this to the 2.0.0 milestone Mar 15, 2018

@pasin pasin self-assigned this Mar 15, 2018

pasin added a commit that referenced this issue Mar 15, 2018

Fix immutable Swift Dict / Array not converted to objc object
* Fixed DataConverter. convertSETValue(value) to check the base Dictionary and Array type.
* Added unit tests for both Swift and Objective-C.

#2104

@pasin pasin closed this Mar 15, 2018

pasin added a commit that referenced this issue Mar 15, 2018

Fix immutable Swift Dict / Array not converted to objc object
* Fixed DataConverter. convertSETValue(value) to check the base Dictionary and Array type.
* Added unit tests for both Swift and Objective-C.

#2104
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment