diff --git a/diff/diff_test.go b/diff/diff_test.go index 2236bb24..e510c896 100644 --- a/diff/diff_test.go +++ b/diff/diff_test.go @@ -923,8 +923,10 @@ func TestDiff_Extensions(t *testing.T) { require.NoError(t, err) dd := d.PathsDiff.Modified["/example/callback"].OperationsDiff.Modified["POST"].ExtensionsDiff.Modified[extension] require.Len(t, dd, 2) - require.Equal(t, "{\"value\":\"200\",\"op\":\"replace\",\"path\":\"/responses/default/statusCode\"}", dd[0].String()) - require.Equal(t, "{\"value\":\"http://api.example.com/v1/example/calllllllllback\",\"op\":\"replace\",\"path\":\"/uri\"}", dd[1].String()) + require.Equal(t, "200", dd[0].Value) + require.Equal(t, "201", dd[0].OldValue) + require.Equal(t, "http://api.example.com/v1/example/calllllllllback", dd[1].Value) + require.Equal(t, "http://api.example.com/v1/example/callback", dd[1].OldValue) } func TestDiff_ExtensionsInvalid(t *testing.T) { diff --git a/diff/interface_map_diff.go b/diff/interface_map_diff.go index f592e2d0..4502076d 100644 --- a/diff/interface_map_diff.go +++ b/diff/interface_map_diff.go @@ -2,7 +2,6 @@ package diff import ( "github.com/tufin/oasdiff/utils" - "github.com/wI2L/jsondiff" ) // InterfaceMap is a map of string to interface @@ -54,7 +53,7 @@ func getInterfaceMapDiffInternal(map1, map2 InterfaceMap, filter utils.StringSet for name1, interface1 := range map1 { if _, ok := filter[name1]; ok { if interface2, ok := map2[name1]; ok { - patch, err := jsondiff.Compare(interface1, interface2) + patch, err := compareJson(interface1, interface2) if err != nil { return nil, err } diff --git a/diff/json_diff.go b/diff/json_diff.go new file mode 100644 index 00000000..2a795b1c --- /dev/null +++ b/diff/json_diff.go @@ -0,0 +1,69 @@ +package diff + +import ( + "encoding/json" + + "github.com/wI2L/jsondiff" +) + +// jsonPatch is a wrapper to jsondiff.jsonPatch with proper serialization for json and yaml +type jsonPatch []*jsonOperation + +// jsonOperation is a wrapper to jsondiff.jsonOperation with proper serialization for json and yaml +type jsonOperation struct { + OldValue interface{} `json:"old_value" yaml:"old_value"` + Value interface{} `json:"value" yaml:"value"` + Type string `json:"op" yaml:"op"` + From string `json:"from" yaml:"from"` + Path string `json:"path" yaml:"path"` +} + +func toJsonPatch(patch jsondiff.Patch) jsonPatch { + result := make(jsonPatch, len(patch)) + for i, op := range patch { + result[i] = newJsonOperation(op) + } + return result +} + +func newJsonOperation(op jsondiff.Operation) *jsonOperation { + return &jsonOperation{ + OldValue: op.OldValue, + Value: op.Value, + Type: op.Type, + From: op.From, + Path: op.Path, + } +} + +func compareJson(source, target interface{}, opts ...jsondiff.Option) (jsonPatch, error) { + patch, err := jsondiff.Compare(source, target, opts...) + if err != nil { + return nil, err + } + return toJsonPatch(patch), nil +} + +// GetJsonOrigValue returns the original value of the diff, only if there is exactly one diff +func GetJsonOrigValue(patch jsonPatch) (json.RawMessage, bool) { + if len(patch) != 1 { + return nil, false + } + result, ok := patch[0].OldValue.(json.RawMessage) + if !ok { + return nil, false + } + return result, true +} + +// GetJsonOrigValue returns the new value of the diff, only if there is exactly one diff +func GetJsonNewValue(patch jsonPatch) (json.RawMessage, bool) { + if len(patch) != 1 { + return nil, false + } + result, ok := patch[0].Value.(json.RawMessage) + if !ok { + return nil, false + } + return result, true +} diff --git a/diff/modified_interfaces.go b/diff/modified_interfaces.go index 0c6a61f6..2d8ae683 100644 --- a/diff/modified_interfaces.go +++ b/diff/modified_interfaces.go @@ -1,39 +1,9 @@ package diff -import ( - "encoding/json" - - "github.com/wI2L/jsondiff" -) - // ModifiedInterfaces is map of interface names to their respective diffs -type ModifiedInterfaces map[string]jsondiff.Patch +type ModifiedInterfaces map[string]jsonPatch // Empty indicates whether a change was found in this element func (modifiedInterfaces ModifiedInterfaces) Empty() bool { return len(modifiedInterfaces) == 0 } - -// GetJsonOrigValue returns the original value of the diff, only if there is exactly one diff -func GetJsonOrigValue(patch jsondiff.Patch) (json.RawMessage, bool) { - if len(patch) != 1 { - return nil, false - } - result, ok := patch[0].OldValue.(json.RawMessage) - if !ok { - return nil, false - } - return result, true -} - -// GetJsonOrigValue returns the new value of the diff, only if there is exactly one diff -func GetJsonNewValue(patch jsondiff.Patch) (json.RawMessage, bool) { - if len(patch) != 1 { - return nil, false - } - result, ok := patch[0].Value.(json.RawMessage) - if !ok { - return nil, false - } - return result, true -}