Skip to content

handle ignoreUndefinedProperties in set(merge: true) #4347

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 4 commits into from
Jan 27, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
7 changes: 7 additions & 0 deletions .changeset/silent-cars-burn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@firebase/firestore': patch
---

handle `ignoreUndefinedProperties` in `set({ merge: true })`. Previously this
would behave as if the undefined value were `FieldValue.delete()`, which wasn't
intended.
6 changes: 5 additions & 1 deletion packages/firestore/src/api/user_data_reader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -741,7 +741,11 @@ export function parseData(
} else {
// If context.path is null we are inside an array and we don't support
// field mask paths more granular than the top-level array.
if (context.path) {
//
// If the input is undefined it can never participate in the fieldMask. With
// `ignoreUndefinedProperties` set to false, `parseScalarValue` will reject
// an undefined value.
if (context.path && input !== undefined) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be cleaner to pull the undefined check from parseScalarValue into this function. Something like:

if (lookslikeJson) { ... }
else if (input instanceof FieldValue) { ... }
else if (input == undefined && context.ignoreUndefinedProperties) {
    return null;
}  else { ... }

Optional.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea. Done.

context.fieldMask.push(context.path);
}

Expand Down
9 changes: 9 additions & 0 deletions packages/firestore/test/integration/api/fields.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,15 @@ apiDescribe('`undefined` properties', (persistence: boolean) => {
});
});

it('are ignored in set({ merge: true })', () => {
return withTestDocAndSettings(persistence, settings, async doc => {
await doc.set({ foo: 'foo', 'bar': 'unchanged' });
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am surprised the pre-commit hook didn't drop the quotes around bar.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

await doc.set({ foo: 'foo', 'bar': undefined }, { merge: true });
const docSnap = await doc.get();
expect(docSnap.data()).to.deep.equal({ foo: 'foo', bar: 'unchanged' });
});
});

it('are ignored in update()', () => {
return withTestDocAndSettings(persistence, settings, async doc => {
await doc.set({});
Expand Down