Skip to content

Commit

Permalink
make zero value config struct the same as the default config
Browse files Browse the repository at this point in the history
  • Loading branch information
jxsl13 committed Aug 20, 2023
1 parent 4381bee commit 9da55d5
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 39 deletions.
33 changes: 19 additions & 14 deletions document.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ type document struct {

// skip optional errors like circular reference errors.
// if configured
errorFilter func(error) bool
errorFilter []func(error) bool
}

// DocumentModel represents either a Swagger document (version 2) or an OpenAPI document (version 3) that is
Expand All @@ -118,12 +118,12 @@ type DocumentModel[T v2high.Swagger | v3high.Document] struct {
func NewDocument(specByteArray []byte, options ...ConfigurationOption) (Document, error) {
// sane defaults directly visible to the user.
config := &Configuration{
RemoteURLHandler: http.Get,
AllowFileReferences: false,
AllowRemoteReferences: false,
AvoidIndexBuild: false,
BypassDocumentCheck: false,
AllowCircularReferenceResolving: true,
RemoteURLHandler: http.Get,
AllowFileReferences: false,
AllowRemoteReferences: false,
AvoidIndexBuild: false,
BypassDocumentCheck: false,
ForbidCircularReferenceResolving: false,
}

var err error
Expand All @@ -150,7 +150,7 @@ func NewDocumentWithConfiguration(specByteArray []byte, config *Configuration) (
info: info,
highOpenAPI3Model: nil,
highSwaggerModel: nil,
errorFilter: defaultErrorFilter,
errorFilter: []func(error) bool{defaultErrorFilter},
}

d.SetConfiguration(config)
Expand All @@ -162,14 +162,19 @@ func (d *document) SetConfiguration(config *Configuration) {
d.config = config

if config == nil {
d.errorFilter = defaultErrorFilter
d.errorFilter = []func(error) bool{defaultErrorFilter}
return
}

d.errorFilter = errorutils.AndFilter(
if config.RemoteURLHandler == nil {
// set default handler if
config.RemoteURLHandler = http.Get
}

d.errorFilter = []func(error) bool{
// more filters can be added here if needed
circularReferenceErrorFilter(config.AllowCircularReferenceResolving),
)
circularReferenceErrorFilter(config.ForbidCircularReferenceResolving),
}
}

func NewDocumentWithTypeCheck(specByteArray []byte, bypassCheck bool) (Document, error) {
Expand Down Expand Up @@ -250,7 +255,7 @@ func (d *document) BuildV2Model() (*DocumentModel[v2high.Swagger], error) {
}

lowDoc, err := v2low.CreateDocumentFromConfig(d.info, d.config.toModelConfig())
err = errorutils.Filtered(err, d.errorFilter)
err = errorutils.Filtered(err, d.errorFilter...)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -282,7 +287,7 @@ func (d *document) BuildV3Model() (*DocumentModel[v3high.Document], error) {
}

lowDoc, err = v3low.CreateDocumentFromConfig(d.info, d.config.toModelConfig())
err = errorutils.Filtered(err, d.errorFilter)
err = errorutils.Filtered(err, d.errorFilter...)
if err != nil {
return nil, err
}
Expand Down
14 changes: 7 additions & 7 deletions document_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ type Configuration struct {
// passed in and used. Only enable this when parsing non openapi documents.
BypassDocumentCheck bool

// AllowCircularReferences will allow circular references to be resolved. This is disabled by default.
// Will return an error in case of circular references.
AllowCircularReferenceResolving bool
// ForbidCircularReferenceResolving will forbid circular references to be resolved. This is disabled by default.
// Will return an error in case of circular references if set to true.
ForbidCircularReferenceResolving bool
}

func (c *Configuration) toModelConfig() *datamodel.DocumentConfiguration {
Expand Down Expand Up @@ -109,11 +109,11 @@ func WithBypassDocumentCheck(bypass bool) ConfigurationOption {
}
}

// WithAllowCircularReferenceResolving returns an error for every detected circular reference if set to false.
// If set to true, circular references will be resolved (default behavior).
func WithAllowCircularReferenceResolving(allow bool) ConfigurationOption {
// WithForbidCircularReferenceResolving returns an error for every detected circular reference if set to true.
// If set to false, circular references will be resolved (default behavior).
func WithForbidCircularReferenceResolving(forbidden bool) ConfigurationOption {
return func(o *Configuration) error {
o.AllowCircularReferenceResolving = allow
o.ForbidCircularReferenceResolving = forbidden
return nil
}
}
2 changes: 1 addition & 1 deletion document_examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ components:
- testThing
`
// create a new document from specification bytes
doc, err := NewDocument([]byte(spec), WithAllowCircularReferenceResolving(false))
doc, err := NewDocument([]byte(spec), WithForbidCircularReferenceResolving(true))

// if anything went wrong, an error is thrown
if err != nil {
Expand Down
5 changes: 3 additions & 2 deletions document_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ func TestDocument_RenderAndReload_ChangeCheck_Burgershop(t *testing.T) {
func TestDocument_RenderAndReload_ChangeCheck_Stripe(t *testing.T) {

bs, _ := os.ReadFile("test_specs/stripe.yaml")
doc, err := NewDocument(bs, WithAllowCircularReferenceResolving(true))
doc, err := NewDocument(bs)
require.NoError(t, err)
_, err = doc.BuildV3Model()
require.NoError(t, err)
Expand Down Expand Up @@ -321,9 +321,10 @@ func TestDocument_AnyDocWithConfig(t *testing.T) {

func TestDocument_BuildModelCircular(t *testing.T) {
petstore, _ := os.ReadFile("test_specs/circular-tests.yaml")
doc, err := NewDocument(petstore, WithAllowCircularReferenceResolving(false))
doc, err := NewDocument(petstore, WithForbidCircularReferenceResolving(true))
require.NoError(t, err)
m, err := doc.BuildV3Model()
require.Error(t, err)

// top level library does not return broken objects
// with an error, only one or the other
Expand Down
9 changes: 3 additions & 6 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,15 @@ func isCircularErr(err error) bool {
// returns a filter function that checks if a given error is a circular reference error
// and in case that circular references are allowed or not, it returns false
// in order to skip the error or true in order to keep the error in the wrapped error list.
func circularReferenceErrorFilter(refAllowed bool) func(error) (keep bool) {
func circularReferenceErrorFilter(forbidden bool) func(error) (keep bool) {
return func(err error) bool {
if err == nil {
return false
}

if isCircularErr(err) {
if refAllowed {
return false
} else {
return true
}
// if forbidded -> keep the error and pass it to the user
return forbidden
}

// keep unknown error
Expand Down
4 changes: 3 additions & 1 deletion index/find_component_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,10 @@ paths:

// extract crs param from index
crsParam := index.GetMappedReferences()["https://schemas.opengis.net/ogcapi/features/part2/1.0/openapi/ogcapi-features-2.yaml#/components/parameters/crs"]
assert.NotNil(t, crsParam)
require.NotNil(t, crsParam)
assert.True(t, crsParam.IsRemote)
require.NotNil(t, crsParam.Node)
require.GreaterOrEqual(t, len(crsParam.Node.Content), 10)
assert.Equal(t, "crs", crsParam.Node.Content[1].Value)
assert.Equal(t, "query", crsParam.Node.Content[3].Value)
assert.Equal(t, "form", crsParam.Node.Content[9].Value)
Expand Down
13 changes: 9 additions & 4 deletions internal/errorutils/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ func Filtered(err error, filters ...func(error) (keep bool)) error {
return nil
}
errs := ShallowUnwrap(err)
filtered := Filter(errs, AndFilter(filters...))
filtered := Filter(errs, and(filters...))
if len(filtered) == 0 {
return nil
}
Expand All @@ -14,21 +14,26 @@ func Filtered(err error, filters ...func(error) (keep bool)) error {

func Filter(errs []error, filter func(error) (keep bool)) []error {
var result []error
var keep bool
for _, err := range errs {
if filter(err) {
keep = filter(err)
if keep {
result = append(result, err)
}
}
return result
}

func AndFilter(filters ...func(error) (keep bool)) func(error) (keep bool) {
func and(filters ...func(error) (keep bool)) func(error) (keep bool) {
return func(err error) bool {
var keep bool
for _, filter := range filters {
if !filter(err) {
keep = filter(err)
if !keep {
return false
}
}
// all true -> true
return true
}
}
8 changes: 4 additions & 4 deletions resolver/resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,12 @@ func TestResolver_CheckForCircularReferences_DigitalOcean(t *testing.T) {
require.NotNil(t, resolver)

circ := resolver.CheckForCircularReferences()
assert.Len(t, circ, 0)
assert.Len(t, resolver.GetResolvingErrors(), 0)
assert.Len(t, resolver.GetCircularErrors(), 0)
require.Len(t, circ, 0)
require.Len(t, resolver.GetResolvingErrors(), 0)
require.Len(t, resolver.GetCircularErrors(), 0)

_, err := yaml.Marshal(resolver.resolvedRoot)
assert.NoError(t, err)
require.NoError(t, err)
}

func TestResolver_CircularReferencesRequiredValid(t *testing.T) {
Expand Down

0 comments on commit 9da55d5

Please sign in to comment.