Skip to content

Commit

Permalink
Port array null fix over from pre-v5
Browse files Browse the repository at this point in the history
  • Loading branch information
evanphx committed May 31, 2021
1 parent 1ab3b4f commit 0cc7ddb
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
2 changes: 1 addition & 1 deletion v5/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,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 v5/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 0cc7ddb

Please sign in to comment.