Skip to content

Commit

Permalink
Use replace operation if source key exists but has null value (#29)
Browse files Browse the repository at this point in the history
Signed-off-by: Tamal Saha <tamal@appscode.com>
  • Loading branch information
tamalsaha committed May 19, 2021
1 parent 9c9863c commit b21dfd7
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
2 changes: 0 additions & 2 deletions v3/jsonpatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,8 +237,6 @@ func handleValues(av, bv interface{}, p string, patch []Operation) ([]Operation,
if at == nil && bt == nil {
// do nothing
return patch, nil
} else if at == nil && bt != nil {
return append(patch, NewOperation("add", p, bv)), nil
} else if at != bt {
// If types have changed, replace completely (preserves null in destination)
return append(patch, NewOperation("replace", p, bv)), nil
Expand Down
36 changes: 36 additions & 0 deletions v3/jsonpatch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -801,6 +801,41 @@ var (
}`
)

var (
nullKeyA = `{
"apiVersion": "cert-manager.io/v1",
"kind": "CertificateRequest",
"metadata": {
"creationTimestamp": null,
"name": "test-cr",
"namespace": "default-unit-test-ns"
},
"spec": {
"issuerRef": {
"name": ""
},
"request": null
},
"status": {}
}`
nullKeyB = `{
"apiVersion": "cert-manager.io/v1",
"kind": "CertificateRequest",
"metadata": {
"creationTimestamp": null,
"name": "test-cr",
"namespace": "default-unit-test-ns"
},
"spec": {
"issuerRef": {
"name": ""
},
"request": "bXV0YXRpb24gY2FsbGVk"
},
"status": {}
}`
)

func TestCreatePatch(t *testing.T) {
cases := []struct {
name string
Expand Down Expand Up @@ -845,6 +880,7 @@ func TestCreatePatch(t *testing.T) {
{"Different Array", oldArray, newArray},
{"Array at root", `[{"asdf":"qwerty"}]`, `[{"asdf":"bla"},{"asdf":"zzz"}]`},
{"Empty array at root", `[]`, `[{"asdf":"bla"},{"asdf":"zzz"}]`},
{"Null Key uses replace operation", nullKeyA, nullKeyB},
}

for _, c := range cases {
Expand Down

0 comments on commit b21dfd7

Please sign in to comment.