Skip to content

Allow MERGE operations into empty array values#754

Merged
tgolen merged 10 commits intomainfrom
chuckdries/allow-merge-empty-array
Mar 20, 2026
Merged

Allow MERGE operations into empty array values#754
tgolen merged 10 commits intomainfrom
chuckdries/allow-merge-empty-array

Conversation

@chuckdries
Copy link
Contributor

@chuckdries chuckdries commented Mar 18, 2026

Details

An alternative solution to #753. Rather than treating empty arrays as null at set-time, we should allow merge updates to clobber empty array values. Also logs an alert with ENSURE_BUGBOT so we can know when it happens.

See #expert-contributors

Related Issues

https://github.com/Expensify/Expensify/issues/611769

Automated Tests

Add should merge an object into an empty array and should still reject merging an object into a non-empty array

Manual Tests

  1. Use Onyx.set('someKey', []) in the browser console to create or set a key to []
  2. Use Onyx.get('someKey').then(data => console.log('someKey', data)) to log the value and make sure it's []
  3. Use Onyx.merge('someKey', {asdf: {}}) to merge into the empty array
  4. Use Onyx.get('someKey').then(data => console.log('someKey', data)) to log the value and make sure it's {asdf: {}}
  5. Ensure that you saw a bugbot log in your console

Author Checklist

  • I linked the correct issue in the ### Related Issues section above
  • I wrote clear testing steps that cover the changes made in this PR
    • I added steps for local testing in the Tests section
    • I tested this PR with a High Traffic account against the staging or production API to ensure there are no regressions (e.g. long loading states that impact usability).
  • I included screenshots or videos for tests on all platforms
  • I ran the tests on all platforms & verified they passed on:
    • Android / native
    • Android / Chrome
    • iOS / native
    • iOS / Safari
    • MacOS / Chrome / Safari
  • I verified there are no console errors (if there's a console error not related to the PR, report it or open an issue for it to be fixed)
  • I followed proper code patterns (see Reviewing the code)
    • I verified that any callback methods that were added or modified are named for what the method does and never what callback they handle (i.e. toggleReport and not onIconClick)
    • I verified that the left part of a conditional rendering a React component is a boolean and NOT a string, e.g. myBool && <MyComponent />.
    • I verified that comments were added to code that is not self explanatory
    • I verified that any new or modified comments were clear, correct English, and explained "why" the code was doing something instead of only explaining "what" the code was doing.
    • I verified proper file naming conventions were followed for any new files or renamed files. All non-platform specific files are named after what they export and are not named "index.js". All platform-specific files are named for the platform the code supports as outlined in the README.
    • I verified the JSDocs style guidelines (in STYLE.md) were followed
  • If a new code pattern is added I verified it was agreed to be used by multiple Expensify engineers
  • I followed the guidelines as stated in the Review Guidelines
  • I tested other components that can be impacted by my changes (i.e. if the PR modifies a shared library or component like Avatar, I verified the components using Avatar are working as expected)
  • I verified all code is DRY (the PR doesn't include any logic written more than once, with the exception of tests)
  • I verified any variables that can be defined as constants (ie. in CONST.js or at the top of the file that uses the constant) are defined as such
  • I verified that if a function's arguments changed that all usages have also been updated correctly
  • If a new component is created I verified that:
    • A similar component doesn't exist in the codebase
    • All props are defined accurately and each prop has a /** comment above it */
    • The file is named correctly
    • The component has a clear name that is non-ambiguous and the purpose of the component can be inferred from the name alone
    • The only data being stored in the state is data necessary for rendering and nothing else
    • If we are not using the full Onyx data that we loaded, I've added the proper selector in order to ensure the component only re-renders when the data it is using changes
    • For Class Components, any internal methods passed to components event handlers are bound to this properly so there are no scoping issues (i.e. for onClick={this.submit} the method this.submit should be bound to this in the constructor)
    • Any internal methods bound to this are necessary to be bound (i.e. avoid this.submit = this.submit.bind(this); if this.submit is never passed to a component event handler like onClick)
    • All JSX used for rendering exists in the render method
    • The component has the minimum amount of code necessary for its purpose, and it is broken down into smaller components in order to separate concerns and functions
  • If any new file was added I verified that:
    • The file has a description of what it does and/or why is needed at the top of the file if the code is not self explanatory
  • If the PR modifies a generic component, I tested and verified that those changes do not break usages of that component in the rest of the App (i.e. if a shared library or component like Avatar is modified, I verified that Avatar is working as expected in all cases)
  • If the main branch was merged into this PR after a review, I tested again and verified the outcome was still expected according to the Test steps.
  • I have checked off every checkbox in the PR author checklist, including those that don't apply to this PR.

Screenshots/Videos

Android: Native
onyx.patch.working.android.mp4
iOS: Native
Onyx.patch.working.ios.mp4
MacOS: Chrome / Safari
onyx.patch.working.mp4

@chuckdries chuckdries marked this pull request as ready for review March 18, 2026 21:03
@chuckdries chuckdries requested a review from a team as a code owner March 18, 2026 21:03
@melvin-bot melvin-bot bot requested review from cristipaval and removed request for a team March 18, 2026 21:04
Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 479bc113d0

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

lib/utils.ts Outdated
Comment on lines +218 to +219
if (Array.isArray(existingValue) && existingValue.length === 0 && !Array.isArray(value)) {
return {isCompatible: true};

Choose a reason for hiding this comment

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

P2 Badge Restrict empty-array compatibility to merge-only updates

checkCompatibilityWithExistingValue() is shared by setWithRetry() and mergeCollectionWithPatches(), not only Onyx.merge(), so this new early return makes all non-array writes compatible whenever the current value is []. That means a key that is legitimately array-typed (and currently empty) can now be silently overwritten by an object/primitive via set/mergeCollection, whereas the previous array-vs-non-array guard would log and skip it. Please scope this empty-array exception to the merge-object path instead of globally in the compatibility helper.

Useful? React with 👍 / 👎.

Copy link
Member

Choose a reason for hiding this comment

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

I think this is right, and we should be checking explicitly for object here, rather than non-array

@chuckdries chuckdries requested a review from rafecolton March 18, 2026 21:56
Copy link
Member

@rafecolton rafecolton left a comment

Choose a reason for hiding this comment

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

I tested this locally and it worked well.

I think the AI reviewer's comments are valid, and this will allow overwriting arrays with scalar values in addition to objects. Is that intended?

lib/utils.ts Outdated
Comment on lines +218 to +219
if (Array.isArray(existingValue) && existingValue.length === 0 && !Array.isArray(value)) {
return {isCompatible: true};
Copy link
Member

Choose a reason for hiding this comment

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

I think this is right, and we should be checking explicitly for object here, rather than non-array

lib/Onyx.ts Outdated
Comment on lines +239 to +241
if (Array.isArray(existingValue) && existingValue.length === 0) {
Logger.logAlert(`[ENSURE_BUGBOT] Onyx merge called on key "${key}" whose existing value is an empty array. Will coerce to object.`);
}
Copy link
Member

Choose a reason for hiding this comment

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

Will this fire for any merge attempt into an existing array or only if attempting to merge an object?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm pretty sure merging into an array is unsupported anyway. Checking

rafecolton
rafecolton previously approved these changes Mar 19, 2026
Copy link
Member

@rafecolton rafecolton left a comment

Choose a reason for hiding this comment

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

Tested on dev via red/green, worked great!

const {isCompatible, existingValueType, newValueType} = utils.checkCompatibilityWithExistingValue(change, existingValue);
const {isCompatible, existingValueType, newValueType, isEmptyArrayCoercion} = utils.checkCompatibilityWithExistingValue(change, existingValue);
if (isEmptyArrayCoercion) {
Logger.logAlert(`[ENSURE_BUGBOT] Onyx merge called on key "${key}" whose existing value is an empty array. Will coerce to object.`);
Copy link
Collaborator

Choose a reason for hiding this comment

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

NAB: Is this enough data for someone to tell what went wrong and fix it? Having the key is nice, but I feel like having the value would almost be necessary. Though, I don't love the idea of logging the value since there is not a good way to know if it's sensitive or not. Is there any other data that we can include that would be helpful? I think probably the most useful thing would be the requestID (if one exists), but that sounds difficult to grab.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, I don't see a great way to get the requestID that delivered the update. We could maybe log in SaveResponseInOnyx for any SET [] operation, but that would be potentially noisier than we want, and that wouldn't cover pusher updates. I also don't want to log the value for the reason you stated

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The log statements that make it to the backend will have a user email and a datetime. It'll take a bit of legwork but it's probably not impossible to work backwards from that and the onyx key and figure out what the user was doing

lib/OnyxUtils.ts Outdated
},
{
result: (existingValue ?? {}) as TChange,
result: ((Array.isArray(existingValue) && existingValue.length === 0 ? {} : existingValue) ?? {}) as TChange,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Would you mind adding a code comment explaining why this is being done?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Well, I thought it was necessary because I was misinterpreting this comment in fastMerge, but I was mixing up source and target. Turns out it works fine without this line, so I reverted it.

lib/utils.ts Outdated
isCompatible: true,
};
}
// An empty array existing value is compatible with an object update.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Hm, this comment is kind of odd. It is making a statement, but it's not obvious to me why this statement would be true or not. I think the explanation about PHP below is helpful, but this comment could probably be more verbose to help understand why we chose to do this.

Copy link
Member

@rafecolton rafecolton left a comment

Choose a reason for hiding this comment

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

Re-tested, confirmed it still works

@rafecolton rafecolton requested review from tgolen and removed request for cristipaval March 20, 2026 00:14
Copy link
Collaborator

@tgolen tgolen left a comment

Choose a reason for hiding this comment

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

Cool, that looks great. Thanks for the improved comments!

@tgolen tgolen merged commit 109b22e into main Mar 20, 2026
10 checks passed
@os-botify
Copy link
Contributor

os-botify bot commented Mar 20, 2026

🚀 Published to npm in 3.0.49 🎉

rafecolton added a commit to Expensify/App that referenced this pull request Mar 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants