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

Use replace operation if source key exists but has null value #29

Merged
merged 1 commit into from
May 18, 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: 0 additions & 2 deletions v2/jsonpatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,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 v2/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