Skip to content

Commit

Permalink
fix json patch serializtion
Browse files Browse the repository at this point in the history
  • Loading branch information
Reuven committed Apr 4, 2024
1 parent c2ca1b1 commit 5a6578c
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 35 deletions.
6 changes: 4 additions & 2 deletions diff/diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
3 changes: 1 addition & 2 deletions diff/interface_map_diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package diff

import (
"github.com/tufin/oasdiff/utils"
"github.com/wI2L/jsondiff"
)

// InterfaceMap is a map of string to interface
Expand Down Expand Up @@ -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
}
Expand Down
69 changes: 69 additions & 0 deletions diff/json_diff.go
Original file line number Diff line number Diff line change
@@ -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

Check warning on line 56 in diff/json_diff.go

View check run for this annotation

Codecov / codecov/patch

diff/json_diff.go#L48-L56

Added lines #L48 - L56 were not covered by tests
}

// 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

Check warning on line 68 in diff/json_diff.go

View check run for this annotation

Codecov / codecov/patch

diff/json_diff.go#L60-L68

Added lines #L60 - L68 were not covered by tests
}
32 changes: 1 addition & 31 deletions diff/modified_interfaces.go
Original file line number Diff line number Diff line change
@@ -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

Check warning on line 8 in diff/modified_interfaces.go

View check run for this annotation

Codecov / codecov/patch

diff/modified_interfaces.go#L7-L8

Added lines #L7 - L8 were not covered by tests
}

// 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
}

0 comments on commit 5a6578c

Please sign in to comment.