Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preserve "null" values in arrays #143

Merged
merged 1 commit into from
May 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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