diff --git a/Firestore/CHANGELOG.md b/Firestore/CHANGELOG.md index 3c9e723abb3..a0273f87655 100644 --- a/Firestore/CHANGELOG.md +++ b/Firestore/CHANGELOG.md @@ -1,4 +1,5 @@ # Unreleased +- [fixed] Allow blob of data with zero length. (#11773, #12620) - [changed] Passing a non-nil value to the `@DocumentID` property wrapper's setter no longer logs a warning since it discouraged valid patterns, e.g., updating the document ID after the document is created in Firestore. (#12756) diff --git a/Firestore/Swift/Tests/Integration/CodableIntegrationTests.swift b/Firestore/Swift/Tests/Integration/CodableIntegrationTests.swift index 85a7e0ba014..c6d01a63e98 100644 --- a/Firestore/Swift/Tests/Integration/CodableIntegrationTests.swift +++ b/Firestore/Swift/Tests/Integration/CodableIntegrationTests.swift @@ -185,6 +185,40 @@ class CodableIntegrationTests: FSTIntegrationTestCase { } } + func testDataBlob() throws { + struct Model: Encodable { + var name: String + var data: Data + var emptyData: Data + } + let model = Model( + name: "name", + data: Data([1, 2, 3, 4]), + emptyData: Data() + ) + + let docToWrite = documentRef() + + for flavor in allFlavors { + try setData(from: model, forDocument: docToWrite, withFlavor: flavor) + + let data = readDocument(forRef: docToWrite) + + XCTAssertEqual(data["data"] as! Data, Data([1, 2, 3, 4]), "Failed with flavor \(flavor)") + XCTAssertEqual(data["emptyData"] as! Data, Data(), "Failed with flavor \(flavor)") + } + + disableNetwork() + defer { + enableNetwork() + } + + try docToWrite.setData(from: model) + let data = readDocument(forRef: docToWrite) + XCTAssertEqual(data["data"] as! Data, Data([1, 2, 3, 4]), "Failed with flavor offline docRef") + XCTAssertEqual(data["emptyData"] as! Data, Data(), "Failed with flavor offline docRef") + } + func testExplicitNull() throws { struct Model: Encodable { var name: String diff --git a/Firestore/core/src/nanopb/nanopb_util.h b/Firestore/core/src/nanopb/nanopb_util.h index 6c551735e3e..f689cea1634 100644 --- a/Firestore/core/src/nanopb/nanopb_util.h +++ b/Firestore/core/src/nanopb/nanopb_util.h @@ -179,6 +179,7 @@ inline NSData* _Nonnull MakeNSData(const ByteString& str) { } inline NSData* _Nonnull MakeNSData(const pb_bytes_array_t* _Nullable data) { + if (data == nil) return [[NSData alloc] init]; return [[NSData alloc] initWithBytes:data->bytes length:data->size]; }