Skip to content

Commit e09def3

Browse files
authored
Merge pull request #45 from dixudx/fix_bug_on_nil_value
fix bug on matchesValue for nil type
2 parents dc4af23 + 99c5750 commit e09def3

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

merge.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,9 @@ func matchesValue(av, bv interface{}) bool {
224224
if bt == at {
225225
return true
226226
}
227+
case nil:
228+
// Both nil, fine.
229+
return true
227230
case map[string]interface{}:
228231
bt := bv.(map[string]interface{})
229232
for key := range at {

merge_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,23 @@ func TestMergeEmptyArray(t *testing.T) {
271271
}
272272
}
273273

274+
func TestCreateMergePatchNil(t *testing.T) {
275+
doc := `{ "title": "hello", "nested": {"one": 1, "two": [{"one":null}, {"two":null}, {"three":null}]} }`
276+
pat := doc
277+
278+
exp := `{}`
279+
280+
res, err := CreateMergePatch([]byte(doc), []byte(pat))
281+
282+
if err != nil {
283+
t.Errorf("Unexpected error: %s, %s", err, string(res))
284+
}
285+
286+
if !compareJSON(exp, string(res)) {
287+
t.Fatalf("Object array was not added")
288+
}
289+
}
290+
274291
func TestMergeObjArray(t *testing.T) {
275292
doc := `{ "array": [ {"a": {"b": 2}}, {"a": {"b": 3}} ]}`
276293
exp := `{}`

0 commit comments

Comments
 (0)