From c5dd33f72d04829b405ffdf01a96eb28012d4a1c Mon Sep 17 00:00:00 2001 From: Reuven Harrison Date: Mon, 8 Apr 2024 23:44:53 +0300 Subject: [PATCH] add Empty check to jsonPatch (#520) Co-authored-by: Reuven --- data/extensions/openapi-2.json | 95 ++++++++++++++++++++++++++++++++++ diff/diff_test.go | 11 ++++ diff/interface_map_diff.go | 4 +- diff/json_diff.go | 5 ++ 4 files changed, 114 insertions(+), 1 deletion(-) create mode 100644 data/extensions/openapi-2.json diff --git a/data/extensions/openapi-2.json b/data/extensions/openapi-2.json new file mode 100644 index 00000000..df0256f3 --- /dev/null +++ b/data/extensions/openapi-2.json @@ -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": {} + } +} \ No newline at end of file diff --git a/diff/diff_test.go b/diff/diff_test.go index 1b89f68b..9c19aff5 100644 --- a/diff/diff_test.go +++ b/diff/diff_test.go @@ -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) +} diff --git a/diff/interface_map_diff.go b/diff/interface_map_diff.go index 6ce23bcc..ea966997 100644 --- a/diff/interface_map_diff.go +++ b/diff/interface_map_diff.go @@ -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) } diff --git a/diff/json_diff.go b/diff/json_diff.go index 63d1f7d7..0cc9e9a7 100644 --- a/diff/json_diff.go +++ b/diff/json_diff.go @@ -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"`