Fix revertChangeset for index-based arrays - #412
Open
aarondpn wants to merge 1 commit into
Open
Conversation
revertArrayChange processed $index subchanges in their original ascending order. Reverting an ADD splices the element out, so an earlier splice shifts the remaining elements and later indices land on the wrong slot: revertChangeset(['a','b','c'], diff(['a'], ['a','b','c'])) returned ['a','c']. ADDs are now undone from the highest index down, mirroring the ordering applyArrayChange already uses for REMOVE operations. Two further defects in the same path surfaced while verifying the fix: - revertLeafChange called addKeyValue without embeddedKey, so reverting a REMOVE appended the value instead of splicing it back at its index. This affected every type change, since treatTypeChangeAsReplace defaults to true and encodes those as a REMOVE/ADD pair at the same index. - The leaf/branch guard tested subchange.value != null, routing null-valued subchanges down the branch path where they were silently dropped. It now keys off the presence of nested changes, matching applyArrayChange. Verified by exhaustive round-trip over all arrays of length 0-4 built from four distinct types (116281 pairs, both directions): no apply or revert failures. Existing snapshots are unchanged.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Problem
revertChangesetcorrupts index-based arrays when a changeset contains more than oneADD:revertArrayChangeiterated$indexsubchanges in their original ascending order. Reverting anADDsplices the element out, so undoing index 1 first shifts'c'down into that slot and the subsequentsplice(2, 1)becomes a no-op.applyChangesetalready sorts$indexREMOVEs descending; the revert path never got the mirrored treatment. This is the revert-side counterpart to #407, which fixed the apply side onmaster.