Skip to content

Commit bd2fc26

Browse files
committed
Check for void nodes on JSON Patch render.
1 parent 7064efc commit bd2fc26

File tree

4 files changed

+11
-6
lines changed

4 files changed

+11
-6
lines changed

lib/common_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,11 +242,11 @@ func fuzz(t *testing.T, aStr, bStr string) {
242242
// Apply diff to A to get B.
243243
patchedA, err := a.Patch(diffAB)
244244
if err != nil {
245-
t.Errorf("applying patch %v to %v should give %v. Got err: %v", diffAB, aStr, bStr, err)
245+
t.Errorf("applying patch %v to %v should give %v. Got err: %v", diffAB.Render(), aStr, bStr, err)
246246
return
247247
}
248248
if !patchedA.Equals(b) {
249-
t.Errorf("applying patch %v to %v should give %v. Got err: %v", diffAB, aStr, bStr, patchedA)
249+
t.Errorf("applying patch %v to %v should give %v. Got: %v", diffAB.Render(), aStr, bStr, patchedA)
250250
return
251251
}
252252
}

lib/diff_write.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func (d Diff) RenderPatch() (string, error) {
5959
if len(element.OldValues) == 0 && len(element.NewValues) == 0 {
6060
return "", fmt.Errorf("Cannot render empty diff element as JSON Patch op.")
6161
}
62-
if len(element.OldValues) == 1 {
62+
if len(element.OldValues) == 1 && !isVoid(element.OldValues[0]) {
6363
patch = append(patch, patchElement{
6464
Op: "test",
6565
Path: path,
@@ -71,7 +71,7 @@ func (d Diff) RenderPatch() (string, error) {
7171
Value: element.OldValues[0],
7272
})
7373
}
74-
if len(element.NewValues) == 1 {
74+
if len(element.NewValues) == 1 && !isVoid(element.NewValues[0]) {
7575
patch = append(patch, patchElement{
7676
Op: "add",
7777
Path: path,

lib/fuzz_backport_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@ import "testing"
66
// versions before 1.18. This is only necessary until go1.18beta2 is GA
77
// and we can depend on it.
88
//
9-
// To run fuzzing use `go1.18beta2 test -tags=test_fuzz ./lib/-run=FuzzJd`
9+
// To run fuzzing use `go1.18beta2 test -tags=test_fuzz ./lib/ -fuzz=FuzzJd`
1010
func TestFuzzBackport(t *testing.T) {
1111
for _, backport := range [][2]string{{
12-
"[]", "0",
12+
"[]", "0", // FuzzJd/e193f6c4bfd5b8d3c12e1ac42162b2ccd7a31f9aafd466066c1ec7a95da48e1e
13+
}, {
14+
"{}", " ", // FuzzJd/868060b2021521d32933f40415c6f95b38fda5f5c6bdb7fa6664d046c637c03c
1315
}} {
1416
fuzz(t, backport[0], backport[1])
1517
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
go test fuzz v1
2+
string("{}")
3+
string(" ")

0 commit comments

Comments
 (0)