Skip to content

Commit

Permalink
Move open api spec info to load (#289)
Browse files Browse the repository at this point in the history
  • Loading branch information
Reuven Harrison authored Jun 8, 2023
1 parent f939f02 commit a574235
Show file tree
Hide file tree
Showing 16 changed files with 82 additions and 78 deletions.
12 changes: 0 additions & 12 deletions checker/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/getkin/kin-openapi/openapi3"
"github.com/tufin/oasdiff/checker/localizations"
"github.com/tufin/oasdiff/diff"
"github.com/tufin/oasdiff/load"
)

type Level int
Expand Down Expand Up @@ -390,14 +389,3 @@ func (diffBC *BCDiff) AddRequestPropertiesDiff(path string, operation string, me
}
return mediaTypeBCDiff.SchemaDiff.PropertiesDiff
}

// LoadOpenAPISpecInfoFromFile loads a LoadOpenAPISpecInfoFromFile from a local file path
func LoadOpenAPISpecInfoFromFile(loader load.Loader, location string) (*load.OpenAPISpecInfo, error) {
s, err := loader.LoadFromFile(location)
return &load.OpenAPISpecInfo{Spec: s, Url: location}, err
}

func LoadOpenAPISpecInfo(loader load.Loader, location string) (*load.OpenAPISpecInfo, error) {
s, err := load.From(loader, location)
return &load.OpenAPISpecInfo{Spec: s, Url: location}, err
}
8 changes: 4 additions & 4 deletions checker/checker_breaking_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ const (
installCommandPath = "/api/{domain}/{project}/install-command"
)

func l(t *testing.T, v int) load.OpenAPISpecInfo {
func l(t *testing.T, v int) load.SpecInfo {
t.Helper()
loader := openapi3.NewLoader()
oas, err := loader.LoadFromFile(fmt.Sprintf("../data/openapi-test%d.yaml", v))
require.NoError(t, err)
return load.OpenAPISpecInfo{Spec: oas, Url: fmt.Sprintf("../data/openapi-test%d.yaml", v)}
return load.SpecInfo{Spec: oas, Url: fmt.Sprintf("../data/openapi-test%d.yaml", v)}
}

func d(t *testing.T, config *diff.Config, v1, v2 int) []checker.BackwardCompatibilityError {
Expand Down Expand Up @@ -143,8 +143,8 @@ func TestBreaking_PathParamRename(t *testing.T) {
require.NoError(t, err)

d, osm, err := diff.GetWithOperationsSourcesMap(getConfig(),
&load.OpenAPISpecInfo{Spec: s1},
&load.OpenAPISpecInfo{Spec: s2},
&load.SpecInfo{Spec: s1},
&load.SpecInfo{Spec: s2},
)
require.NoError(t, err)
errs := checker.CheckBackwardCompatibility(checker.GetDefaultChecks(), d, osm)
Expand Down
4 changes: 2 additions & 2 deletions checker/checker_deprecation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import (
"github.com/tufin/oasdiff/load"
)

func open(file string) (*load.OpenAPISpecInfo, error) {
return checker.LoadOpenAPISpecInfoFromFile(openapi3.NewLoader(), file)
func open(file string) (*load.SpecInfo, error) {
return load.LoadSpecInfoFromFile(openapi3.NewLoader(), file)
}

func getDeprecationFile(file string) string {
Expand Down
16 changes: 8 additions & 8 deletions checker/composed_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,20 @@ import (
"github.com/tufin/oasdiff/load"
)

func loadFrom(t *testing.T, prefix string, v int) load.OpenAPISpecInfo {
func loadFrom(t *testing.T, prefix string, v int) load.SpecInfo {
path := fmt.Sprintf(prefix+"spec%d.yaml", v)
loader := openapi3.NewLoader()
oas, err := loader.LoadFromFile(path)
require.NoError(t, err)
return load.OpenAPISpecInfo{Spec: oas, Url: path}
return load.SpecInfo{Spec: oas, Url: path}
}

func TestComposed_Empty(t *testing.T) {
s1 := []load.OpenAPISpecInfo{
s1 := []load.SpecInfo{
loadFrom(t, "../data/composed/base/", 1),
}

s2 := []load.OpenAPISpecInfo{
s2 := []load.SpecInfo{
loadFrom(t, "../data/composed/base/", 1),
}

Expand All @@ -34,25 +34,25 @@ func TestComposed_Empty(t *testing.T) {
}

func TestComposed_Duplicate(t *testing.T) {
s1 := []load.OpenAPISpecInfo{
s1 := []load.SpecInfo{
loadFrom(t, "../data/composed/base/", 1),
loadFrom(t, "../data/composed/base/", 1),
}

s2 := []load.OpenAPISpecInfo{}
s2 := []load.SpecInfo{}

config := diff.NewConfig()
_, _, err := diff.GetPathsDiff(config, s1, s2)
require.Error(t, err)
}

func TestComposed_CompareMostRecent(t *testing.T) {
s1 := []load.OpenAPISpecInfo{
s1 := []load.SpecInfo{
loadFrom(t, "../data/composed/base/", 1),
loadFrom(t, "../data/composed/base/", 2),
}

s2 := []load.OpenAPISpecInfo{
s2 := []load.SpecInfo{
loadFrom(t, "../data/composed/revision/", 1),
loadFrom(t, "../data/composed/revision/", 2),
}
Expand Down
10 changes: 5 additions & 5 deletions diff/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,17 +80,17 @@ Note that GetWithOperationsSourcesMap expects OpenAPI References (https://swagge
References are normally resolved automatically when you load the spec.
In other cases you can resolve refs using https://pkg.go.dev/github.com/getkin/kin-openapi/openapi3#Loader.ResolveRefsIn.
*/
func GetWithOperationsSourcesMap(config *Config, s1, s2 *load.OpenAPISpecInfo) (*Diff, *OperationsSourcesMap, error) {
func GetWithOperationsSourcesMap(config *Config, s1, s2 *load.SpecInfo) (*Diff, *OperationsSourcesMap, error) {
diff, err := getDiff(config, newState(), s1.Spec, s2.Spec)
if err != nil {
return nil, nil, err
}

_, operationsSources1, err := mergedPaths([]load.OpenAPISpecInfo{*s1}, config.MatchPathParams)
_, operationsSources1, err := mergedPaths([]load.SpecInfo{*s1}, config.MatchPathParams)
if err != nil {
return nil, nil, err
}
_, operationsSources2, err := mergedPaths([]load.OpenAPISpecInfo{*s2}, config.MatchPathParams)
_, operationsSources2, err := mergedPaths([]load.SpecInfo{*s2}, config.MatchPathParams)
if err != nil {
return nil, nil, err
}
Expand All @@ -115,7 +115,7 @@ Note that Get expects OpenAPI References (https://swagger.io/docs/specification/
References are normally resolved automatically when you load the spec.
In other cases you can resolve refs using https://pkg.go.dev/github.com/getkin/kin-openapi/openapi3#Loader.ResolveRefsIn.
*/
func GetPathsDiff(config *Config, s1, s2 []load.OpenAPISpecInfo) (*Diff, *OperationsSourcesMap, error) {
func GetPathsDiff(config *Config, s1, s2 []load.SpecInfo) (*Diff, *OperationsSourcesMap, error) {
state := newState()
result := newDiff()
var err error
Expand Down Expand Up @@ -155,7 +155,7 @@ func getPathItem(paths openapi3.Paths, path string, matchPathParams bool) *opena
return paths.Find(path)
}

func mergedPaths(s1 []load.OpenAPISpecInfo, matchPathParams bool) (*openapi3.Paths, *OperationsSourcesMap, error) {
func mergedPaths(s1 []load.SpecInfo, matchPathParams bool) (*openapi3.Paths, *OperationsSourcesMap, error) {
result := make(openapi3.Paths, 0)
operationsSources := make(OperationsSourcesMap)
for _, s := range s1 {
Expand Down
16 changes: 8 additions & 8 deletions diff/diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -736,10 +736,10 @@ func TestDiff_PathParamInMethodRenamed(t *testing.T) {
require.NoError(t, err)

d, _, err := diff.GetWithOperationsSourcesMap(diff.NewConfig(),
&load.OpenAPISpecInfo{
&load.SpecInfo{
Spec: s1,
},
&load.OpenAPISpecInfo{
&load.SpecInfo{
Spec: s2,
})
require.NoError(t, err)
Expand All @@ -759,10 +759,10 @@ func TestDiff_PathParamInOperationRenamed(t *testing.T) {
require.NoError(t, err)

d, _, err := diff.GetWithOperationsSourcesMap(diff.NewConfig(),
&load.OpenAPISpecInfo{
&load.SpecInfo{
Spec: s1,
},
&load.OpenAPISpecInfo{
&load.SpecInfo{
Spec: s2,
})
require.NoError(t, err)
Expand All @@ -782,10 +782,10 @@ func TestDiff_TwoPathParamsRenamed(t *testing.T) {
require.NoError(t, err)

d, _, err := diff.GetWithOperationsSourcesMap(diff.NewConfig(),
&load.OpenAPISpecInfo{
&load.SpecInfo{
Spec: s1,
},
&load.OpenAPISpecInfo{
&load.SpecInfo{
Spec: s2,
})
require.NoError(t, err)
Expand All @@ -809,10 +809,10 @@ func TestDiff_TwoPathParamsOneRenamed(t *testing.T) {
require.NoError(t, err)

d, _, err := diff.GetWithOperationsSourcesMap(diff.NewConfig(),
&load.OpenAPISpecInfo{
&load.SpecInfo{
Spec: s1,
},
&load.OpenAPISpecInfo{
&load.SpecInfo{
Spec: s2,
})
require.NoError(t, err)
Expand Down
8 changes: 4 additions & 4 deletions diff/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,13 @@ func ExampleGetPathsDiff() {
loader := openapi3.NewLoader()
loader.IsExternalRefsAllowed = true

s1, err := checker.LoadOpenAPISpecInfo(loader, "../data/openapi-test1.yaml")
s1, err := load.LoadSpecInfo(loader, "../data/openapi-test1.yaml")
if err != nil {
fmt.Fprintf(os.Stderr, "failed to load spec with %v", err)
return
}

s2, err := checker.LoadOpenAPISpecInfo(loader, "../data/openapi-test3.yaml")
s2, err := load.LoadSpecInfo(loader, "../data/openapi-test3.yaml")
if err != nil {
fmt.Fprintf(os.Stderr, "failed to load spec with %v", err)
return
Expand All @@ -80,8 +80,8 @@ func ExampleGetPathsDiff() {
diffConfig := diff.NewConfig().WithCheckBreaking()

diffRes, operationsSources, err := diff.GetPathsDiff(diffConfig,
[]load.OpenAPISpecInfo{*s1},
[]load.OpenAPISpecInfo{*s2},
[]load.SpecInfo{*s1},
[]load.SpecInfo{*s2},
)

if err != nil {
Expand Down
5 changes: 2 additions & 3 deletions internal/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (

"github.com/getkin/kin-openapi/openapi3"
"github.com/tufin/oasdiff/build"
"github.com/tufin/oasdiff/checker"
"github.com/tufin/oasdiff/diff"
"github.com/tufin/oasdiff/load"
)
Expand Down Expand Up @@ -84,11 +83,11 @@ func runInternal(args []string, stdout io.Writer, stderr io.Writer) (bool, *Retu
}

func normalDiff(loader load.Loader, base, revision string, config *diff.Config) (*diff.Diff, *diff.OperationsSourcesMap, *ReturnError) {
s1, err := checker.LoadOpenAPISpecInfo(loader, base)
s1, err := load.LoadSpecInfo(loader, base)
if err != nil {
return nil, nil, getErrFailedToLoadSpec("base", base, err)
}
s2, err := checker.LoadOpenAPISpecInfo(loader, revision)
s2, err := load.LoadSpecInfo(loader, revision)
if err != nil {
return nil, nil, getErrFailedToLoadSpec("revision", revision, err)
}
Expand Down
4 changes: 2 additions & 2 deletions lint/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const (
LEVEL_WARN = 1
)

type Check func(string, *load.OpenAPISpecInfo) []*Error
type Check func(string, *load.SpecInfo) []*Error

type Error struct {
Id string `json:"id,omitempty" yaml:"id,omitempty"`
Expand Down Expand Up @@ -48,7 +48,7 @@ func (e Errors) Swap(i, j int) {
e[i], e[j] = e[j], e[i]
}

func Run(config Config, source string, spec *load.OpenAPISpecInfo) Errors {
func Run(config Config, source string, spec *load.SpecInfo) Errors {
result := make(Errors, 0)

if spec == nil {
Expand Down
4 changes: 2 additions & 2 deletions lint/checker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ import (
"github.com/tufin/oasdiff/load"
)

func loadFrom(t *testing.T, path string) *load.OpenAPISpecInfo {
func loadFrom(t *testing.T, path string) *load.SpecInfo {
t.Helper()

loader := openapi3.NewLoader()
oas, err := loader.LoadFromFile(path)
require.NoError(t, err)
return &load.OpenAPISpecInfo{Spec: oas, Url: path}
return &load.SpecInfo{Spec: oas, Url: path}
}

func TestRun(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion lint/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

// InfoCheck based on REQUIRED fields (Version and Info) from swagger docs,
// see: https://swagger.io/docs/specification/api-general-info/
func InfoCheck(source string, spec *load.OpenAPISpecInfo) []*Error {
func InfoCheck(source string, spec *load.SpecInfo) []*Error {

result := make([]*Error, 0)

Expand Down
2 changes: 1 addition & 1 deletion lint/path_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/tufin/oasdiff/utils"
)

func PathParamsCheck(source string, s *load.OpenAPISpecInfo) []*Error {
func PathParamsCheck(source string, s *load.SpecInfo) []*Error {
result := make([]*Error, 0)

if s == nil || s.Spec == nil {
Expand Down
2 changes: 1 addition & 1 deletion lint/required_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/tufin/oasdiff/load"
)

func RequiredParamsCheck(source string, s *load.OpenAPISpecInfo) []*Error {
func RequiredParamsCheck(source string, s *load.SpecInfo) []*Error {
result := make([]*Error, 0)

if s == nil || s.Spec == nil {
Expand Down
2 changes: 1 addition & 1 deletion lint/shcema_checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func newState(source string) *state {
}
}

func SchemaCheck(source string, spec *load.OpenAPISpecInfo) []*Error {
func SchemaCheck(source string, spec *load.SpecInfo) []*Error {
result := make([]*Error, 0)

if spec == nil || spec.Spec == nil {
Expand Down
24 changes: 0 additions & 24 deletions load/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"net/url"

"github.com/getkin/kin-openapi/openapi3"
"github.com/yargevad/filepathx"
)

// Loader interface includes the OAS load functions
Expand All @@ -25,29 +24,6 @@ func From(loader Loader, path string) (*openapi3.T, error) {
return loader.LoadFromFile(path)
}

type OpenAPISpecInfo struct {
Url string
Spec *openapi3.T
}

// FromGlob is a convenience function that opens OpenAPI specs from local files matching the specified glob parameter
func FromGlob(loader Loader, glob string) ([]OpenAPISpecInfo, error) {
files, err := filepathx.Glob(glob)
if err != nil {
return nil, err
}
result := make([]OpenAPISpecInfo, 0)
for _, file := range files {
spec, err := loader.LoadFromFile(file)
if err != nil {
return nil, err
}
result = append(result, OpenAPISpecInfo{Url: file, Spec: spec})
}

return result, nil
}

func loadFromURI(loader Loader, uri *url.URL) (*openapi3.T, error) {
oas, err := loader.LoadFromURI(uri)
if err != nil {
Expand Down
Loading

0 comments on commit a574235

Please sign in to comment.