-
Notifications
You must be signed in to change notification settings - Fork 73
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
Onyx.merge does not remove a nested object key #301
Comments
following |
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 |
@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 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. |
Yes this is indeed a bit inconsistent, but until now this was actually the intended output. I'm open to changing this so all |
|
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 |
I just created a PR for this here. Should be done really soon 👍 |
Finished implementation of this feature/bug. 👍 |
Merged! |
PR was reverted |
After the revert, another PR fixing this issue was merged: #380 I think we can close this now? :) @thienlnam @tgolen |
Yup looks good, thanks! |
Hi folks, while working on my PR, I think I've run into a similar issue again. Let's take this test case:
Should the This is relevant for both Onyx.merge():Unit test: it('should merge a non-existing key with a nested null removed', () => {
let testKeyValue;
connectionID = Onyx.connect({
key: ONYX_KEYS.TEST_KEY,
initWithStoredValues: false,
callback: (value) => {
testKeyValue = value;
},
});
return Onyx.merge(ONYX_KEYS.TEST_KEY, {
waypoints: {
1: 'Home',
2: 'Work',
3: null,
},
}).then(() => {
expect(testKeyValue).toEqual({
waypoints: {
1: 'Home',
2: 'Work',
},
});
});
}); The test fails because the persisted value is: {
"waypoints": {
"1": "Home",
"2": "Work",
"3": null
}
} Onyx.mergeCollection():Unit test: describe('Onyx.mergeCollection', () => {
it('should omit nested null values', () => {
let result;
const routineRoute = `${ONYX_KEYS.COLLECTION.ROUTES}routine`;
const holidayRoute = `${ONYX_KEYS.COLLECTION.ROUTES}holiday`;
connectionID = Onyx.connect({
key: ONYX_KEYS.COLLECTION.ROUTES,
initWithStoredValues: false,
callback: (value) => result = value,
waitForCollectionCallback: true,
});
return Onyx.mergeCollection(ONYX_KEYS.COLLECTION.ROUTES, {
[routineRoute]: {
waypoints: {
1: 'Home',
2: 'Work',
3: 'Gym',
},
},
[holidayRoute]: {
waypoints: {
1: 'Home',
2: 'Beach',
3: null,
},
},
}).then(() => {
expect(result).toEqual({
[routineRoute]: {
waypoints: {
1: 'Home',
2: 'Work',
3: 'Gym',
},
},
[holidayRoute]: {
waypoints: {
1: 'Home',
2: 'Beach',
},
},
});
});
});
}); 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 |
I've narrowed the issue down a little. Here's a failing unit test of the Given: react-native-onyx/tests/unit/fastMergeTest.js Lines 15 to 34 in db7007f
This test fails: it('should merge an object with an empty object and remove deeply nested null values', () => {
const result = utils.fastMerge({}, testObjectWithNullishValues);
expect(result).toEqual(testObjectWithNullValuesRemoved);
}); The actual {
"b": {
"c": {
"h": "h"
},
"d": {
"e": null
}
}
} The top-level nullish values are removed (the |
If you set an object key to null, Onyx.merge will remove it
This works for first level keys
// Remove all first level keys that are explicitly set to null.
Corresponding object:
But if you try to remove a nested key
It will just set the value to null
The text was updated successfully, but these errors were encountered: