Skip to content

Commit 1500d2d

Browse files
Don't drop empty objects during merge
1 parent f30be09 commit 1500d2d

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

packages/firestore/src/api/user_data_converter.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,11 @@ export class UserDataConverter {
548548
result = result.insert(key, parsedValue);
549549
}
550550
});
551+
// If we encounter an empty object, we explicitly add it to the update mask
552+
// to ensure that the server creates a map entry.
553+
if (context.path && result.isEmpty()) {
554+
context.fieldMask.push(context.path);
555+
}
551556
return new ObjectValue(result);
552557
}
553558

packages/firestore/test/integration/api/database.test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,23 @@ apiDescribe('Database', persistence => {
188188
});
189189
});
190190

191+
it.only('can merge empty object', () => {
192+
return withTestDoc(persistence, doc => {
193+
const initialData = {};
194+
const firstMerge = { a: {} };
195+
const secondMerge = { b: {} };
196+
const finalData = { a: {}, b: {}};
197+
return doc
198+
.set(initialData)
199+
.then(() => doc.set(firstMerge, { mergeFields: ['a'] }))
200+
.then(() => doc.set(secondMerge, { merge: true }))
201+
.then(() => doc.get())
202+
.then(docSnapshot => {
203+
expect(docSnapshot.data()).to.be.deep.equal(finalData);
204+
});
205+
});
206+
});
207+
191208
it('can delete field using merge', () => {
192209
return withTestDoc(persistence, doc => {
193210
const initialData = {

0 commit comments

Comments
 (0)