Skip to content

Don't drop empty objects during merge #1202

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

Merged
merged 2 commits into from
Sep 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion packages/firestore/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
# 0.7.3 (Unreleased)
# 0.7.4 (Unreleased)
- [fixed] Fixed an issue where the first `get()` call made after being offline
could incorrectly return cached data without attempting to reach the backend.
- [changed] Changed `get()` to only make 1 attempt to reach the backend before
returning cached data, potentially reducing delays while offline. Previously
it would make 2 attempts, to work around a backend bug.
- [fixed] Fixed an issue that caused us to drop empty objects from calls to
`set(..., { merge: true })`.

# 0.7.2
- [fixed] Fixed a regression that prevented use of Firestore on ReactNative's
Expand Down
26 changes: 18 additions & 8 deletions packages/firestore/src/api/user_data_converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -539,15 +539,25 @@ export class UserDataConverter {

private parseObject(obj: Dict<AnyJs>, context: ParseContext): FieldValue {
let result = new SortedMap<string, FieldValue>(primitiveComparator);
objUtils.forEach(obj, (key: string, val: AnyJs) => {
const parsedValue = this.parseData(
val,
context.childContextForField(key)
);
if (parsedValue != null) {
result = result.insert(key, parsedValue);

if (objUtils.isEmpty(obj)) {
// If we encounter an empty object, we explicitly add it to the update
// mask to ensure that the server creates a map entry.
if (context.path && context.path.length > 0) {
context.fieldMask.push(context.path);
}
});
} else {
objUtils.forEach(obj, (key: string, val: AnyJs) => {
const parsedValue = this.parseData(
val,
context.childContextForField(key)
);
if (parsedValue != null) {
result = result.insert(key, parsedValue);
}
});
}

return new ObjectValue(result);
}

Expand Down
12 changes: 7 additions & 5 deletions packages/firestore/src/model/mutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -449,11 +449,13 @@ export class PatchMutation extends Mutation {

private patchObject(data: ObjectValue): ObjectValue {
for (const fieldPath of this.fieldMask.fields) {
const newValue = this.data.field(fieldPath);
if (newValue !== undefined) {
data = data.set(fieldPath, newValue);
} else {
data = data.delete(fieldPath);
if (!fieldPath.isEmpty()) {
const newValue = this.data.field(fieldPath);
if (newValue !== undefined) {
data = data.set(fieldPath, newValue);
} else {
data = data.delete(fieldPath);
}
}
}
return data;
Expand Down
28 changes: 28 additions & 0 deletions packages/firestore/test/integration/api/database.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,34 @@ apiDescribe('Database', persistence => {
});
});

it('can merge empty object', async () => {
await withTestDoc(persistence, async doc => {
const accumulator = new EventsAccumulator<firestore.DocumentSnapshot>();
const unsubscribe = doc.onSnapshot(accumulator.storeEvent);
await accumulator
.awaitEvent()
.then(() => doc.set({}))
.then(() => accumulator.awaitEvent())
.then(docSnapshot => expect(docSnapshot.data()).to.be.deep.equal({}))
.then(() => doc.set({ a: {} }, { mergeFields: ['a'] }))
.then(() => accumulator.awaitEvent())
.then(docSnapshot =>
expect(docSnapshot.data()).to.be.deep.equal({ a: {} })
)
.then(() => doc.set({ b: {} }, { merge: true }))
.then(() => accumulator.awaitEvent())
.then(docSnapshot =>
expect(docSnapshot.data()).to.be.deep.equal({ a: {}, b: {} })
)
.then(() => doc.get({ source: 'server' }))
.then(docSnapshot => {
expect(docSnapshot.data()).to.be.deep.equal({ a: {}, b: {} });
});

unsubscribe();
});
});

it('can delete field using merge', () => {
return withTestDoc(persistence, doc => {
const initialData = {
Expand Down