Skip to content

Commit

Permalink
mergedPaths doesn't modify specs (#501)
Browse files Browse the repository at this point in the history
Co-authored-by: Reuven <[email protected]>
  • Loading branch information
reuvenharrison and Reuven authored Mar 5, 2024
1 parent 32d32b1 commit e1108f3
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 2 deletions.
11 changes: 11 additions & 0 deletions checker/composed_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
11 changes: 11 additions & 0 deletions data/composed/issue500/spec1.yaml
Original file line number Diff line number Diff line change
@@ -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
11 changes: 11 additions & 0 deletions data/composed/issue500/spec2.yaml
Original file line number Diff line number Diff line change
@@ -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
23 changes: 21 additions & 2 deletions diff/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit e1108f3

Please sign in to comment.