Skip to content

Commit

Permalink
Check for void nodes on JSON Patch render.
Browse files Browse the repository at this point in the history
  • Loading branch information
josephburnett committed Feb 9, 2022
1 parent 7064efc commit bd2fc26
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 6 deletions.
4 changes: 2 additions & 2 deletions lib/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,11 +242,11 @@ func fuzz(t *testing.T, aStr, bStr string) {
// Apply diff to A to get B.
patchedA, err := a.Patch(diffAB)
if err != nil {
t.Errorf("applying patch %v to %v should give %v. Got err: %v", diffAB, aStr, bStr, err)
t.Errorf("applying patch %v to %v should give %v. Got err: %v", diffAB.Render(), aStr, bStr, err)
return
}
if !patchedA.Equals(b) {
t.Errorf("applying patch %v to %v should give %v. Got err: %v", diffAB, aStr, bStr, patchedA)
t.Errorf("applying patch %v to %v should give %v. Got: %v", diffAB.Render(), aStr, bStr, patchedA)
return
}
}
Expand Down
4 changes: 2 additions & 2 deletions lib/diff_write.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (d Diff) RenderPatch() (string, error) {
if len(element.OldValues) == 0 && len(element.NewValues) == 0 {
return "", fmt.Errorf("Cannot render empty diff element as JSON Patch op.")
}
if len(element.OldValues) == 1 {
if len(element.OldValues) == 1 && !isVoid(element.OldValues[0]) {
patch = append(patch, patchElement{
Op: "test",
Path: path,
Expand All @@ -71,7 +71,7 @@ func (d Diff) RenderPatch() (string, error) {
Value: element.OldValues[0],
})
}
if len(element.NewValues) == 1 {
if len(element.NewValues) == 1 && !isVoid(element.NewValues[0]) {
patch = append(patch, patchElement{
Op: "add",
Path: path,
Expand Down
6 changes: 4 additions & 2 deletions lib/fuzz_backport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ import "testing"
// versions before 1.18. This is only necessary until go1.18beta2 is GA
// and we can depend on it.
//
// To run fuzzing use `go1.18beta2 test -tags=test_fuzz ./lib/-run=FuzzJd`
// To run fuzzing use `go1.18beta2 test -tags=test_fuzz ./lib/ -fuzz=FuzzJd`
func TestFuzzBackport(t *testing.T) {
for _, backport := range [][2]string{{
"[]", "0",
"[]", "0", // FuzzJd/e193f6c4bfd5b8d3c12e1ac42162b2ccd7a31f9aafd466066c1ec7a95da48e1e
}, {
"{}", " ", // FuzzJd/868060b2021521d32933f40415c6f95b38fda5f5c6bdb7fa6664d046c637c03c
}} {
fuzz(t, backport[0], backport[1])
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
go test fuzz v1
string("{}")
string(" ")

0 comments on commit bd2fc26

Please sign in to comment.