Skip to content

Commit

Permalink
unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Reuven committed Feb 1, 2024
1 parent 427103b commit d9e94d0
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 9 deletions.
6 changes: 6 additions & 0 deletions flatten/commonparams/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/*
Package commonparams moves common parameters to the operations under it
This is helpful to improve breaking changes accuracy
See here for Common Parameters definition: https://swagger.io/docs/specification/describing-parameters/
*/
package commonparams
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package pathparams
package commonparams

import "github.com/getkin/kin-openapi/openapi3"

// Move moves path-level params to the operations under the path
// Move moves common parameters to the operations under the path
func Move(spec *openapi3.T) {
moveParams(spec)
}
Expand Down
5 changes: 0 additions & 5 deletions flatten/pathparams/doc.go

This file was deleted.

8 changes: 6 additions & 2 deletions load/spec_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

"github.com/getkin/kin-openapi/openapi3"
"github.com/tufin/oasdiff/flatten/allof"
"github.com/tufin/oasdiff/flatten/pathparams"
"github.com/tufin/oasdiff/flatten/commonparams"
"github.com/yargevad/filepathx"
)

Expand Down Expand Up @@ -42,12 +42,14 @@ func getVersion(spec *openapi3.T) string {

type Option func(Loader, []*SpecInfo) ([]*SpecInfo, error)

// WithIdentity returns the original SpecInfos
func WithIdentity() Option {
return func(loader Loader, specInfos []*SpecInfo) ([]*SpecInfo, error) {
return specInfos, nil
}
}

// WithFlattenAllOf returns SpecInfos with flattened allOf
func WithFlattenAllOf() Option {
return func(loader Loader, specInfos []*SpecInfo) ([]*SpecInfo, error) {
var err error
Expand All @@ -60,10 +62,12 @@ func WithFlattenAllOf() Option {
}
}

// WithFlattenPathParams returns SpecInfos with Common Parameters combined into operation parameters
// See here for Common Parameters definition: https://swagger.io/docs/specification/describing-parameters/
func WithFlattenPathParams() Option {
return func(loader Loader, specInfos []*SpecInfo) ([]*SpecInfo, error) {
for _, specInfo := range specInfos {
pathparams.Move(specInfo.Spec)
commonparams.Move(specInfo.Spec)
}
return specInfos, nil
}
Expand Down
10 changes: 10 additions & 0 deletions load/spec_info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,13 @@ func TestSpecInfo_GlobNoFiles(t *testing.T) {
_, err := load.NewSpecInfoFromGlob(MockLoader{}, "../data/*.xxx")
require.EqualError(t, err, "no matching files")
}

func TestSpecInfo_Options(t *testing.T) {
_, err := load.NewSpecInfo(MockLoader{}, load.NewSource("../data/openapi-test1.yaml"), load.WithIdentity(), load.WithFlattenAllOf(), load.WithFlattenPathParams())
require.NoError(t, err)
}

func TestSpecInfo_GlobOptions(t *testing.T) {
_, err := load.NewSpecInfoFromGlob(MockLoader{}, "../data/*.yaml", load.WithIdentity(), load.WithFlattenAllOf(), load.WithFlattenPathParams())
require.NoError(t, err)
}

0 comments on commit d9e94d0

Please sign in to comment.