You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Onyx.merge does not remove a nested object key #301
This is very interesting! I didn't believe it at first, but I did a quick test and confirmed exactly what you report. @chrispader you might be interested in checking this one out (our resident expert on Onyx.merge() 😁 ).
@thienlnam Could you give a little more context into the underlying thing you're trying to do and why it's preferrable to remove the nested key as opposed to having it remain as null?
I could see "consistency" as one argument (and a good one at that), but I kind of feel there needs to be a better reason to change it since it's changing some very low-lying parts of the lib.
A PR was raised to fix this #350. (edit: oh I think that's just for the type declarations). If top level keys are removed when set to null then we should do the same for nested keys IMO.
@thienlnam Could you give a little more context into the underlying thing you're trying to do and why it's preferrable to remove the nested key as opposed to having it remain as null?
Yeah, mostly just an expectation on how merging a null value would work and specifically for waypoints we had the issue because we keyed them like this, and used that to determine the order of the stops / when to add new ones
...
{
waypoint1: {}
waypoint2: {}
waypoint3: {}
}
It just becomes difficult to determine when to add a new waypoint. So for example, if I remove a waypoint it should be
In that specific scenario I ran into, it would be nice if we could remove unused nested object keys and I'm sure there are a few other cases that cleaning up the data object would be nice
Hi folks, while working on my PR, I think I've run into a similar issue again.
Let's take this test case:
There is no existing value of a given key in Onyx;
We call merge with a nested null;
Should the null be ignored here (since if there was an existing value, the null will remove it)?
This is relevant for both merge and mergeCollection (but I believe, both stem from the incorrect fastMerge implementation when the existing value is undefined).
Onyx.merge():
Unit test:
it('should merge a non-existing key with a nested null removed',()=>{lettestKeyValue;connectionID=Onyx.connect({key: ONYX_KEYS.TEST_KEY,initWithStoredValues: false,callback: (value)=>{testKeyValue=value;},});returnOnyx.merge(ONYX_KEYS.TEST_KEY,{waypoints: {1: 'Home',2: 'Work',3: null,},}).then(()=>{expect(testKeyValue).toEqual({waypoints: {1: 'Home',2: 'Work',},});});});
it('should merge an object with an empty object and remove deeply nested null values',()=>{constresult=utils.fastMerge({},testObjectWithNullishValues);expect(result).toEqual(testObjectWithNullValuesRemoved);});
The top-level nullish values are removed (the a: undefined was removed, the same happens if I change undefined to null), but the nested ones are retained.
Activity
neil-marcellini commentedon Aug 16, 2023
following
tgolen commentedon Sep 13, 2023
This is very interesting! I didn't believe it at first, but I did a quick test and confirmed exactly what you report. @chrispader you might be interested in checking this one out (our resident expert on
Onyx.merge()😁 ).tgolen commentedon Sep 13, 2023
@thienlnam Could you give a little more context into the underlying thing you're trying to do and why it's preferrable to remove the nested key as opposed to having it remain as
null?I could see "consistency" as one argument (and a good one at that), but I kind of feel there needs to be a better reason to change it since it's changing some very low-lying parts of the lib.
chrispader commentedon Sep 13, 2023
Yes this is indeed a bit inconsistent, but until now this was actually the intended output.
I'm open to changing this so all
nullvalues will be removed when mergingneil-marcellini commentedon Sep 18, 2023
A PR was raised to fix this #350.(edit: oh I think that's just for the type declarations). If top level keys are removed when set to null then we should do the same for nested keys IMO.thienlnam commentedon Sep 19, 2023
Yeah, mostly just an expectation on how merging a null value would work and specifically for waypoints we had the issue because we keyed them like this, and used that to determine the order of the stops / when to add new ones
It just becomes difficult to determine when to add a new waypoint. So for example, if I remove a waypoint it should be
But since it doesn't null out, it would be
And let's say I wanted to add a new waypoint - I'd have to find the first null waypoint and merge an empty object into it.
And if I deleted multiple stops, it becomes difficult to keep track of - we'd reorder the waypoints and leave all the empty waypoints as waypointX?
In that specific scenario I ran into, it would be nice if we could remove unused nested object keys and I'm sure there are a few other cases that cleaning up the data object would be nice
chrispader commentedon Sep 19, 2023
I just created a PR for this here. Should be done really soon 👍
chrispader commentedon Sep 21, 2023
Finished implementation of this feature/bug. 👍
thienlnam commentedon Sep 29, 2023
Merged!
thienlnam commentedon Sep 30, 2023
PR was reverted
chrispader commentedon Nov 15, 2023
After the revert, another PR fixing this issue was merged: #380
I think we can close this now? :) @thienlnam @tgolen
thienlnam commentedon Nov 19, 2023
Yup looks good, thanks!
paultsimura commentedon Mar 11, 2024
Hi folks, while working on my PR, I think I've run into a similar issue again.
Let's take this test case:
mergewith a nestednull;Should the
nullbe ignored here (since if there was an existing value, thenullwill remove it)?This is relevant for both
mergeandmergeCollection(but I believe, both stem from the incorrectfastMergeimplementation when the existing value isundefined).Onyx.merge():
Unit test:
The test fails because the persisted value is:
{ "waypoints": { "1": "Home", "2": "Work", "3": null } }Onyx.mergeCollection():
Unit test:
The test fails because the persisted value is:
{ "routes_holiday": { "waypoints": { "1": "Home", "2": "Beach", "3": null } }, "routes_routine": { "waypoints": { "1": "Home", "2": "Work", "3": "Gym" } } }cc: @tgolen @thienlnam
paultsimura commentedon Mar 11, 2024
I've narrowed the issue down a little.
Here's a failing unit test of the
fastMergefunction that I believe shouldn't be failing. Please correct me if I'm wrong:Given:
react-native-onyx/tests/unit/fastMergeTest.js
Lines 15 to 34 in db7007f
This test fails:
The actual
resultis:{ "b": { "c": { "h": "h" }, "d": { "e": null } } }The top-level nullish values are removed (the
a: undefinedwas removed, the same happens if I changeundefinedtonull), but the nested ones are retained.