Skip to content

Commit d220c21

Browse files
authored
Merge pull request mattbaird#3 from tmichaud314/add-replace-null-value
Fixes add/replace null-value marshalling
2 parents cda1f16 + e3bc067 commit d220c21

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed

jsonpatch.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package jsonpatch
22

33
import (
4+
"bytes"
45
"encoding/json"
56
"fmt"
67
"reflect"
@@ -20,6 +21,24 @@ func (j *JsonPatchOperation) Json() string {
2021
return string(b)
2122
}
2223

24+
func (j *JsonPatchOperation) MarshalJSON() ([]byte, error) {
25+
var b bytes.Buffer
26+
b.WriteString("{")
27+
b.WriteString(fmt.Sprintf(`"op":"%s"`, j.Operation))
28+
b.WriteString(fmt.Sprintf(`,"path":"%s"`, j.Path))
29+
// Consider omitting Value for non-nullable operations.
30+
if j.Value != nil || j.Operation == "replace" || j.Operation == "add" {
31+
v, err := json.Marshal(j.Value)
32+
if err != nil {
33+
return nil, err
34+
}
35+
b.WriteString(`,"value":`)
36+
b.Write(v)
37+
}
38+
b.WriteString("}")
39+
return b.Bytes(), nil
40+
}
41+
2342
type ByPath []JsonPatchOperation
2443

2544
func (a ByPath) Len() int { return len(a) }

jsonpatch_json_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package jsonpatch
2+
3+
import (
4+
"github.com/stretchr/testify/assert"
5+
"testing"
6+
)
7+
8+
func TestMarshalNullableValue(t *testing.T) {
9+
p1 := JsonPatchOperation{
10+
Operation: "replace",
11+
Path: "/a1",
12+
Value: nil,
13+
}
14+
assert.JSONEq(t, `{"op":"replace", "path":"/a1","value":null}`, p1.Json())
15+
16+
p2 := JsonPatchOperation{
17+
Operation: "replace",
18+
Path: "/a2",
19+
Value: "v2",
20+
}
21+
assert.JSONEq(t, `{"op":"replace", "path":"/a2", "value":"v2"}`, p2.Json())
22+
}
23+
24+
func TestMarshalNonNullableValue(t *testing.T) {
25+
p1 := JsonPatchOperation{
26+
Operation: "remove",
27+
Path: "/a1",
28+
}
29+
assert.JSONEq(t, `{"op":"remove", "path":"/a1"}`, p1.Json())
30+
31+
}

jsonpatch_simple_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,19 @@ var simpleC = `{"a":100, "b":100, "c":"hello"}`
1212
var simpleD = `{"a":100, "b":200, "c":"hello", "d":"foo"}`
1313
var simpleE = `{"a":100, "b":200}`
1414
var simplef = `{"a":100, "b":100, "d":"foo"}`
15+
var simpleG = `{"a":100, "b":null, "d":"foo"}`
1516
var empty = `{}`
1617

18+
func TestOneNullReplace(t *testing.T) {
19+
patch, e := CreatePatch([]byte(simplef), []byte(simpleG))
20+
assert.NoError(t, e)
21+
assert.Equal(t, len(patch), 1, "they should be equal")
22+
change := patch[0]
23+
assert.Equal(t, change.Operation, "replace", "they should be equal")
24+
assert.Equal(t, change.Path, "/b", "they should be equal")
25+
assert.Equal(t, change.Value, nil, "they should be equal")
26+
}
27+
1728
func TestSame(t *testing.T) {
1829
patch, e := CreatePatch([]byte(simpleA), []byte(simpleA))
1930
assert.NoError(t, e)

0 commit comments

Comments
 (0)