Skip to content

Commit

Permalink
Merge pull request #143 from DraggonFantasy/master
Browse files Browse the repository at this point in the history
Preserve "null" values in arrays
  • Loading branch information
evanphx committed May 31, 2021
2 parents 60af5a5 + 3bfa62b commit 1ab3b4f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
2 changes: 1 addition & 1 deletion merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ func pruneAryNulls(ary *partialArray) *partialArray {
for _, v := range *ary {
if v != nil {
pruneNulls(v)
newAry = append(newAry, v)
}
newAry = append(newAry, v)
}

*ary = newAry
Expand Down
25 changes: 25 additions & 0 deletions merge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,31 @@ func TestMergePatchNilDoc(t *testing.T) {
}
}

type arrayCases struct {
original, patch, res string
}

func TestMergePatchNilArray(t *testing.T) {

cases := []arrayCases {
{`{"a": [ {"b":"c"} ] }`, `{"a": [1]}`, `{"a": [1]}`},
{`{"a": [ {"b":"c"} ] }`, `{"a": [null, 1]}`, `{"a": [null, 1]}`},
{`["a",null]`, `[null]`, `[null]`},
{`["a"]`, `[null]`, `[null]`},
{`["a", "b"]`, `["a", null]`, `["a", null]`},
{`{"a":["b"]}`, `{"a": ["b", null]}`, `{"a":["b", null]}`},
{`{"a":[]}`, `{"a": ["b", null, null, "a"]}`, `{"a":["b", null, null, "a"]}`},
}

for _, c := range cases {
act := mergePatch(c.original, c.patch)

if !compareJSON(c.res, act) {
t.Errorf("null values not preserved in array")
}
}
}

func TestMergePatchRecursesIntoObjects(t *testing.T) {
doc := `{ "person": { "title": "hello", "age": 18 } }`
pat := `{ "person": { "title": "goodbye" } }`
Expand Down

0 comments on commit 1ab3b4f

Please sign in to comment.