From e1108f354bff7342d01440c9cc7aed58a8d8354f Mon Sep 17 00:00:00 2001 From: Reuven Harrison Date: Tue, 5 Mar 2024 13:16:18 +0200 Subject: [PATCH] mergedPaths doesn't modify specs (#501) Co-authored-by: Reuven --- checker/composed_test.go | 11 +++++++++++ data/composed/issue500/spec1.yaml | 11 +++++++++++ data/composed/issue500/spec2.yaml | 11 +++++++++++ diff/diff.go | 23 +++++++++++++++++++++-- 4 files changed, 54 insertions(+), 2 deletions(-) create mode 100644 data/composed/issue500/spec1.yaml create mode 100644 data/composed/issue500/spec2.yaml diff --git a/checker/composed_test.go b/checker/composed_test.go index 2eb2fd8b..dfedcd65 100644 --- a/checker/composed_test.go +++ b/checker/composed_test.go @@ -48,6 +48,17 @@ func TestComposed_Duplicate(t *testing.T) { require.Error(t, err) } +func TestComposed_Issue500(t *testing.T) { + s1 := []*load.SpecInfo{ + loadFrom(t, "../data/composed/issue500/", 1), + loadFrom(t, "../data/composed/issue500/", 2), + } + + config := diff.NewConfig() + _, _, err := diff.GetPathsDiff(config, s1, s1) + require.NoError(t, err) +} + func TestComposed_CompareMostRecent(t *testing.T) { s1 := []*load.SpecInfo{ loadFrom(t, "../data/composed/base/", 1), diff --git a/data/composed/issue500/spec1.yaml b/data/composed/issue500/spec1.yaml new file mode 100644 index 00000000..c911194c --- /dev/null +++ b/data/composed/issue500/spec1.yaml @@ -0,0 +1,11 @@ +openapi: 3.0.3 +info: + title: Spec #1 + version: "0.1" +paths: + '/test': + get: + summary: GET endpoint + responses: + '200': + description: Success diff --git a/data/composed/issue500/spec2.yaml b/data/composed/issue500/spec2.yaml new file mode 100644 index 00000000..13f6f499 --- /dev/null +++ b/data/composed/issue500/spec2.yaml @@ -0,0 +1,11 @@ +openapi: 3.0.3 +info: + title: Spec #2 + version: "0.1" +paths: + '/test': + post: + summary: POST endpoint + responses: + '200': + description: Success diff --git a/diff/diff.go b/diff/diff.go index 58e5caf0..5c1fdb71 100644 --- a/diff/diff.go +++ b/diff/diff.go @@ -147,7 +147,7 @@ func mergedPaths(s1 []*load.SpecInfo, includePathParams bool) (*openapi3.Paths, p := getPathItem(result, path, includePathParams) if p == nil { - result.Set(path, pathItem) + result.Set(path, copyPathItem(pathItem)) for _, opItem := range pathItem.Operations() { operationsSources[opItem] = s.Url } @@ -179,12 +179,31 @@ func mergedPaths(s1 []*load.SpecInfo, includePathParams bool) (*openapi3.Paths, return nil, nil, fmt.Errorf("duplicate endpoint (%s %s) found in %s and %s. You may add the %s extension to specify order", op, path, operationsSources[oldOperation], s.Url, SinceDateExtension) } } - } } return result, &operationsSources, nil } +// copyPathItem returns a shallow copy of the path item +func copyPathItem(pathItem *openapi3.PathItem) *openapi3.PathItem { + return &openapi3.PathItem{ + Extensions: pathItem.Extensions, + Ref: pathItem.Ref, + Summary: pathItem.Summary, + Description: pathItem.Description, + Get: pathItem.Get, + Put: pathItem.Put, + Post: pathItem.Post, + Delete: pathItem.Delete, + Options: pathItem.Options, + Head: pathItem.Head, + Patch: pathItem.Patch, + Trace: pathItem.Trace, + Servers: pathItem.Servers, + Parameters: pathItem.Parameters, + } +} + func sinceDateFrom(pathItem openapi3.PathItem, operation openapi3.Operation) (civil.Date, error) { since, _, err := getSinceDate(pathItem.Extensions) if err != nil {