Skip to content
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

Align DataSnapshot methods to @firebase/database DataSnapshot #926

Closed

Conversation

rhodgkins
Copy link
Contributor

@rhodgkins rhodgkins commented Jul 31, 2021

Description

I've noticed[1] a difference between DataSnapshots methods in this SDK compared to the one used when fetching data (when using the admin SDK).

When setting values that are (or contain) empty object/arrays and null values using the admin SDK, when fetching these values back, they're always null when calling .val().
However when creating a DataSnapshot with these values and calling .val() the results don't match.
I'm only raising this as an issue because when the functions are being called, I'd expect the DataSnapshot provided to the function handler to match the DataSnapshot when fetched from the same location - currently this isn't the case.

Code sample

Using the following data: { child: 'foo', nullChild: null, emptyChild: {}, emptyArray: [], child: { nullVal: null, key: 'bar' } }

Setting and then getting this value using the admin SDK [2] results in { child1: 'foo', child2: { key: 'bar } } when calling .val(), which is what you'd expect - all the empty/null nodes are dropped.

However using the firebase-functions database.DataSnapshot constructor the result includes these empty/null values { child: 'foo', nullChild: null, emptyChild: {}, emptyArray: {}, child: { nullVal: null, key: 'bar' } } when calling .val().

I think this is mostly due to the lack of this check the @firebase/database does when calling .val().

The discrepancies are mainly when using .val() but I've updated the other methods and their tests (.exists(), .numChildren(), .hasChildren(), etc.) for completeness...

[1] noticed when using firebase-functions-test makeDataSnapshot method (which calls this repos database.DataSnapshot constructor) for mocking parameters for testing a Firebase Function.

[2] By using:

const firebase = require('firebase-admin')
firebase.initializeApp({ databaseURL: 'https://<databaseName>.firebaseio.com' })

const setAndGet = async val => {
   const ref = firebase.database().ref('node')
   
   await ref.set(val)
   return await ref.get()
}

Comment on lines -544 to -547

// Null values are still reported as null.
populate({ myKey: null });
expect(subject.val()).to.deep.equal({ myKey: null });
Copy link
Contributor Author

@rhodgkins rhodgkins Jul 31, 2021

Choose a reason for hiding this comment

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

I've removed this test case as its incorrect compared to the Firebase SDK, the correct version is now in should deal with null-values appropriately.

Comment on lines +615 to +619
if (numKeys === 0) {
// Empty node
return null;
}

Copy link
Contributor Author

@rhodgkins rhodgkins Jul 31, 2021

Choose a reason for hiding this comment

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

This simulates the previously mentioned this.isEmpty() check the Firebase SDK does.

Comment on lines +748 to +769
describe('#hasChildren()', () => {
it('should true for objects', () => {
populate({
a: 'b',
c: 'd',
nullChild: null,
emptyObjectChild: {},
emptyArrayChild: [],
});
expect(subject.hasChildren()).to.be.true;
});

it('should be false for non-objects', () => {
populate(23);
expect(subject.hasChildren()).to.be.false;

populate({
nullChild: null,
emptyObjectChild: {},
emptyArrayChild: [],
});
expect(subject.hasChildren()).to.be.false;
Copy link
Contributor Author

@rhodgkins rhodgkins Jul 31, 2021

Choose a reason for hiding this comment

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

.hasChildren() had no tests so added some by mirroring the .numChildren() tests above.

@rhodgkins rhodgkins force-pushed the datasnapshot-null-children branch 2 times, most recently from 3dde778 to fae1bf0 Compare August 11, 2021 08:17
@rhodgkins
Copy link
Contributor Author

@inlined due to #943 this PR needs a bit of an update - shall I rebase off master and do it against the old non-v1 directory structure?

@inlined
Copy link
Member

inlined commented Aug 13, 2021

Let me get back to you. We got rid of the v1 directory temporarily to unbreak the emulator and people depending on undocumented behavior. We want to move everything back ASAP by releasing a breaking change. That breaking change might actually be the best time to accept your change since it's a good change but changes behavior notably.

rhodgkins added a commit to bookcreator/firebase-functions-test that referenced this pull request Aug 19, 2021
@rhodgkins
Copy link
Contributor Author

Let me get back to you. We got rid of the v1 directory temporarily to unbreak the emulator and people depending on undocumented behavior. We want to move everything back ASAP by releasing a breaking change. That breaking change might actually be the best time to accept your change since it's a good change but changes behavior notably.

Any update on this @inlined ?

@rhodgkins
Copy link
Contributor Author

Bump @inlined

@inlined
Copy link
Member

inlined commented Oct 25, 2021

Haven't forgotten about you. We just haven't made a breaking change yet. Your PR is actually listed on an internal checklist for changes to be made before our next breaking change.

@rhodgkins
Copy link
Contributor Author

Thanks (and sorry for the pestering then)! Shall I rebase (/keep rebasing) or wait till you guys are ready and give me a bump?

@inlined
Copy link
Member

inlined commented Oct 27, 2021

I don't want to waste your time chasing after a moving target. I'll reach out or rebase myself.

@inlined
Copy link
Member

inlined commented Jun 28, 2022

Closing this since the changes have been ported to the launch.next branch and will be included in the v4 SDK. Thank you very much for both your technical contribution and your patience.

@inlined inlined closed this Jun 28, 2022
rhodgkins added a commit to bookcreator/firebase-functions-test that referenced this pull request Aug 9, 2022
@taeold taeold added this to the v4 milestone Aug 17, 2022
@admin-01-web
Copy link

[gh pr checkout 926]

@admin-01-web
Copy link

@admin-01-web
Copy link

#1298

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants