-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CLOUDP-236398: Add validation logic for paths (#7)
- Loading branch information
1 parent
61a4368
commit 3ed479c
Showing
8 changed files
with
1,968 additions
and
3 deletions.
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.