-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
CLOUDP-236398: Add validation logic for paths #7
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
003da31
CLOUDP-236398: include the merging logic for paths
andreaangiolillo 628f4d2
refactoring
andreaangiolillo 7efff49
CLOUDP-236398: include the merging logic for paths
andreaangiolillo 84a5c9a
merged origin
andreaangiolillo b3118e1
improvements
andreaangiolillo 15c66d0
Update openapi3.go
andreaangiolillo 9b4aa5f
Added test data
andreaangiolillo d0a3cb4
Update Makefile
andreaangiolillo 895dd2e
Delete FOAS.json
andreaangiolillo 44225d4
Update path_conflict_error.go
andreaangiolillo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package errors | ||
|
||
import "fmt" | ||
|
||
type PathConflictError struct { | ||
Entry string | ||
} | ||
|
||
func (e PathConflictError) Error() string { | ||
return fmt.Sprintf("there was a conflict with the path: %q", e.Entry) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package openapi | ||
|
||
import ( | ||
"log" | ||
"mongodb/openapi/tools/cli/internal/openapi/errors" | ||
|
||
"github.com/tufin/oasdiff/diff" | ||
"github.com/tufin/oasdiff/load" | ||
) | ||
|
||
type OasDiff struct { | ||
base *load.SpecInfo | ||
external *load.SpecInfo | ||
config *diff.Config | ||
specDiff *diff.Diff | ||
parser Parser | ||
} | ||
|
||
func NewOasDiff(base string) (*OasDiff, error) { | ||
parser := NewOpenAPI3() | ||
baseSpec, err := parser.CreateOpenAPISpecFromPath(base) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &OasDiff{ | ||
base: baseSpec, | ||
parser: parser, | ||
config: &diff.Config{ | ||
IncludePathParams: true, | ||
}, | ||
}, nil | ||
} | ||
|
||
func (o *OasDiff) MergeOpenAPISpecs(paths []string) (*load.SpecInfo, error) { | ||
for _, p := range paths { | ||
spec, err := o.parser.CreateOpenAPISpecFromPath(p) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
specDiff, err := diff.Get(o.config, o.base.Spec, spec.Spec) | ||
if err != nil { | ||
log.Fatalf("error in calculating the diff of the specs: %s", err) | ||
return nil, err | ||
} | ||
|
||
o.specDiff = specDiff | ||
o.external = spec | ||
err = o.mergeSpecIntoBase() | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
|
||
return o.base, nil | ||
} | ||
|
||
func (o OasDiff) mergeSpecIntoBase() error { | ||
return o.mergePaths() | ||
} | ||
|
||
func (o OasDiff) mergePaths() error { | ||
pathsToMerge := o.external.Spec.Paths | ||
basePaths := o.base.Spec.Paths | ||
for k, v := range pathsToMerge { | ||
if _, ok := basePaths[k]; !ok { | ||
basePaths[k] = v | ||
} else { | ||
return errors.PathConflictError{ | ||
Entry: k, | ||
} | ||
} | ||
} | ||
|
||
o.base.Spec.Paths = basePaths | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package openapi | ||
|
||
import ( | ||
"github.com/tufin/oasdiff/load" | ||
) | ||
|
||
type Merger interface { | ||
MergeOpenAPISpecs([]string) (*load.SpecInfo, error) | ||
} | ||
|
||
type Parser interface { | ||
CreateOpenAPISpecFromPath(string) (*load.SpecInfo, error) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package openapi | ||
|
||
import ( | ||
"github.com/getkin/kin-openapi/openapi3" | ||
"github.com/tufin/oasdiff/load" | ||
) | ||
|
||
type OpenAPI3 struct { | ||
IsExternalRefsAllowed bool | ||
CircularReferenceCounter int | ||
} | ||
|
||
func NewOpenAPI3() *OpenAPI3 { | ||
return &OpenAPI3{ | ||
IsExternalRefsAllowed: true, | ||
CircularReferenceCounter: 10, | ||
} | ||
} | ||
|
||
func (o *OpenAPI3) CreateOpenAPISpecFromPath(path string) (*load.SpecInfo, error) { | ||
openapi3.CircularReferenceCounter = o.CircularReferenceCounter | ||
loader := openapi3.NewLoader() | ||
loader.IsExternalRefsAllowed = o.IsExternalRefsAllowed | ||
|
||
spec, err := load.LoadSpecInfo(loader, load.NewSource(path)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return spec, nil | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we add unit tests or do we want to capture in another ticket?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
planning to add them in a follow-up PR where I add mocking