Skip to content

Commit

Permalink
add Empty check to jsonPatch (#520)
Browse files Browse the repository at this point in the history
Co-authored-by: Reuven <[email protected]>
  • Loading branch information
reuvenharrison and Reuven authored Apr 8, 2024
1 parent 070b485 commit c5dd33f
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 1 deletion.
95 changes: 95 additions & 0 deletions data/extensions/openapi-2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
{
"openapi": "3.0.0",
"paths": {
"/v1alpha1/generic": {
"put": {
"operationId": "GenericController_request",
"requestBody": {
"required": true,
"content": {
"application/json": {
"schema": {
"type": "object",
"properties": {
"resources": {
"type": "array",
"minItems": 1,
"items": {
"type": "object",
"properties": {
"id": {
"type": "string"
},
"kind": {
"oneOf": [
{
"type": "string",
"enum": [
"foo",
"bar",
"fizz",
"buzz"
],
"x-enumNames": [
"FOO",
"BAR",
"FIZZ",
"BUZZ"
]
},
{
"type": "string",
"enum": [
"self"
],
"x-enumNames": [
"SELF"
]
}
]
}
},
"required": [
"id",
"kind"
]
}
}
},
"required": [
"resources"
]
}
}
}
},
"responses": {
"200": {
"description": "response",
"content": {
"application/json": {
"schema": {
"type": "object",
"properties": {
"id": {
"type": "number"
}
},
"required": [
"id"
]
}
}
}
}
},
"tags": []
}
}
},
"tags": [],
"servers": [],
"components": {
"schemas": {}
}
}
11 changes: 11 additions & 0 deletions diff/diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -915,3 +915,14 @@ func TestDiff_ExtensionsInvalidImplicit(t *testing.T) {
_, _, err = diff.GetWithOperationsSourcesMap(diff.NewConfig(), s1, s1)
require.EqualError(t, err, "json: unsupported type: chan int")
}

func TestDiff_ExtensionsIssue519(t *testing.T) {
loader := openapi3.NewLoader()

s1, err := load.NewSpecInfo(loader, load.NewSource("../data/extensions/openapi-2.json"))
require.NoError(t, err)

d, _, err := diff.GetWithOperationsSourcesMap(diff.NewConfig(), s1, s1)
require.NoError(t, err)
require.Empty(t, d)
}
4 changes: 3 additions & 1 deletion diff/interface_map_diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ func getInterfaceMapDiffInternal(map1, map2 InterfaceMap) (*InterfaceMapDiff, er
if err != nil {
return nil, err
}
result.Modified[name1] = patch
if !patch.Empty() {
result.Modified[name1] = patch
}
} else {
result.Deleted = append(result.Deleted, name1)
}
Expand Down
5 changes: 5 additions & 0 deletions diff/json_diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ import (
// jsonPatch is a wrapper to jsondiff.jsonPatch with proper serialization for json and yaml
type jsonPatch []*jsonOperation

// Empty indicates whether a change was found in this element
func (p jsonPatch) Empty() bool {
return len(p) == 0
}

// jsonOperation is a wrapper to jsondiff.jsonOperation with proper serialization for json and yaml
type jsonOperation struct {
OldValue interface{} `json:"oldValue" yaml:"oldValue"`
Expand Down

0 comments on commit c5dd33f

Please sign in to comment.