-
Notifications
You must be signed in to change notification settings - Fork 13
Description
Description
When using generate() from the json-merge-patch package to create a patch where an array is replaced with undefined, the function fails to generate the expected patch with { a: null } to properly remove the field.
Steps to Reproduce
import { generate } from "json-merge-patch";
// I return undefined
console.log(generate({ a: ["hello"] }, { a: undefined }))
// I show that `a` changed to an array
console.log(generate({ a: undefined }, { a: [] }))Live reproduction: https://codesandbox.io/p/sandbox/fxdjg4
Expected Behavior
According to RFC 7396 (JSON Merge Patch), when a field is being removed (replaced with undefined), the generated patch should contain null for that field:
Expected output:
{ a: null } // First case
{ a: [] } // Second case
Actual Behavior
The function fails to generate the patch properly when an array is replaced with undefined:
Actual output:
undefined // First case - undefined instead of { a: null }
{ a: [] } // Second case - This works correctly
Environment
json-merge-patch version: 1.0.2
Additional Context
This appears to be a bug in the implementation of the JSON Merge Patch specification (RFC 7396), which clearly states that removing a field should result in a patch with that field set to null.
The issue seems to be related to the arrayEquals function in the implementation:
json-merge-patch/lib/generate.js
Line 29 in e915ee1
| if (!arrayEquals(before, after)) { |
When replacing an array with undefined, the function returns undefined instead of properly generating a patch with null for the field being removed.