From 0cc7ddb3af9fac2addce39c6298fa324bd847abd Mon Sep 17 00:00:00 2001 From: Evan Phoenix Date: Mon, 31 May 2021 16:57:54 -0700 Subject: [PATCH] Port array null fix over from pre-v5 --- v5/merge.go | 2 +- v5/merge_test.go | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/v5/merge.go b/v5/merge.go index 7219316..cde37f1 100644 --- a/v5/merge.go +++ b/v5/merge.go @@ -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 diff --git a/v5/merge_test.go b/v5/merge_test.go index 8006045..a7e1bb9 100644 --- a/v5/merge_test.go +++ b/v5/merge_test.go @@ -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" } }`