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
Changes from 7 commits
003da31
628f4d2
7efff49
84a5c9a
b3118e1
15c66d0
9b4aa5f
d0a3cb4
895dd2e
44225d4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
andreaangiolillo marked this conversation as resolved.
Show resolved
Hide resolved
|
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: %s", e.Entry) | ||
andreaangiolillo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} |
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 | ||
} |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. don't define the interfaces where they are implemented, define them where they are used There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. addressing this in the next PR |
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) | ||
} |
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 | ||
} |
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