Skip to content

Commit 5b6940e

Browse files
authored
Allow Blob of data with zero length. (#12694)
1 parent d58541e commit 5b6940e

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

Firestore/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# Unreleased
2+
- [fixed] Allow blob of data with zero length. (#11773, #12620)
23
- [changed] Passing a non-nil value to the `@DocumentID` property wrapper's
34
setter no longer logs a warning since it discouraged valid patterns,
45
e.g., updating the document ID after the document is created in Firestore. (#12756)

Firestore/Swift/Tests/Integration/CodableIntegrationTests.swift

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,40 @@ class CodableIntegrationTests: FSTIntegrationTestCase {
185185
}
186186
}
187187

188+
func testDataBlob() throws {
189+
struct Model: Encodable {
190+
var name: String
191+
var data: Data
192+
var emptyData: Data
193+
}
194+
let model = Model(
195+
name: "name",
196+
data: Data([1, 2, 3, 4]),
197+
emptyData: Data()
198+
)
199+
200+
let docToWrite = documentRef()
201+
202+
for flavor in allFlavors {
203+
try setData(from: model, forDocument: docToWrite, withFlavor: flavor)
204+
205+
let data = readDocument(forRef: docToWrite)
206+
207+
XCTAssertEqual(data["data"] as! Data, Data([1, 2, 3, 4]), "Failed with flavor \(flavor)")
208+
XCTAssertEqual(data["emptyData"] as! Data, Data(), "Failed with flavor \(flavor)")
209+
}
210+
211+
disableNetwork()
212+
defer {
213+
enableNetwork()
214+
}
215+
216+
try docToWrite.setData(from: model)
217+
let data = readDocument(forRef: docToWrite)
218+
XCTAssertEqual(data["data"] as! Data, Data([1, 2, 3, 4]), "Failed with flavor offline docRef")
219+
XCTAssertEqual(data["emptyData"] as! Data, Data(), "Failed with flavor offline docRef")
220+
}
221+
188222
func testExplicitNull() throws {
189223
struct Model: Encodable {
190224
var name: String

Firestore/core/src/nanopb/nanopb_util.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ inline NSData* _Nonnull MakeNSData(const ByteString& str) {
179179
}
180180

181181
inline NSData* _Nonnull MakeNSData(const pb_bytes_array_t* _Nullable data) {
182+
if (data == nil) return [[NSData alloc] init];
182183
return [[NSData alloc] initWithBytes:data->bytes length:data->size];
183184
}
184185

0 commit comments

Comments
 (0)