Bubble up circular reference errors

This commit is contained in:
Wouter Dullaert
2022-12-07 11:38:59 +01:00
committed by Dave Shanley
parent cbfb051a92
commit 4fcf45b813

View File

@@ -15,11 +15,13 @@ package libopenapi
import ( import (
"fmt" "fmt"
"github.com/pb33f/libopenapi/datamodel" "github.com/pb33f/libopenapi/datamodel"
v2high "github.com/pb33f/libopenapi/datamodel/high/v2" v2high "github.com/pb33f/libopenapi/datamodel/high/v2"
v3high "github.com/pb33f/libopenapi/datamodel/high/v3" v3high "github.com/pb33f/libopenapi/datamodel/high/v3"
v2low "github.com/pb33f/libopenapi/datamodel/low/v2" v2low "github.com/pb33f/libopenapi/datamodel/low/v2"
v3low "github.com/pb33f/libopenapi/datamodel/low/v3" v3low "github.com/pb33f/libopenapi/datamodel/low/v3"
"github.com/pb33f/libopenapi/resolver"
"github.com/pb33f/libopenapi/utils" "github.com/pb33f/libopenapi/utils"
what_changed "github.com/pb33f/libopenapi/what-changed" what_changed "github.com/pb33f/libopenapi/what-changed"
"github.com/pb33f/libopenapi/what-changed/model" "github.com/pb33f/libopenapi/what-changed/model"
@@ -116,14 +118,22 @@ func (d *document) BuildV2Model() (*DocumentModel[v2high.Swagger], []error) {
"supplied spec is a different version (%v). Try 'BuildV3Model()'", d.info.SpecFormat)) "supplied spec is a different version (%v). Try 'BuildV3Model()'", d.info.SpecFormat))
return nil, errors return nil, errors
} }
lowDoc, err := v2low.CreateDocument(d.info) lowDoc, errs := v2low.CreateDocument(d.info)
if err != nil { // Do not shortcircuit on circular reference errors, so the client
return nil, err // has the option of ignoring them.
for _, err := range errs {
if refErr, ok := err.(*resolver.ResolvingError); ok {
if refErr.CircularReference == nil {
return nil, errs
}
} else {
return nil, errs
}
} }
highDoc := v2high.NewSwaggerDocument(lowDoc) highDoc := v2high.NewSwaggerDocument(lowDoc)
return &DocumentModel[v2high.Swagger]{ return &DocumentModel[v2high.Swagger]{
Model: *highDoc, Model: *highDoc,
}, nil }, errs
} }
func (d *document) BuildV3Model() (*DocumentModel[v3high.Document], []error) { func (d *document) BuildV3Model() (*DocumentModel[v3high.Document], []error) {
@@ -137,14 +147,22 @@ func (d *document) BuildV3Model() (*DocumentModel[v3high.Document], []error) {
"supplied spec is a different version (%v). Try 'BuildV2Model()'", d.info.SpecFormat)) "supplied spec is a different version (%v). Try 'BuildV2Model()'", d.info.SpecFormat))
return nil, errors return nil, errors
} }
lowDoc, err := v3low.CreateDocument(d.info) lowDoc, errs := v3low.CreateDocument(d.info)
if err != nil { // Do not shortcircuit on circular reference errors, so the client
return nil, err // has the option of ignoring them.
for _, err := range errs {
if refErr, ok := err.(*resolver.ResolvingError); ok {
if refErr.CircularReference == nil {
return nil, errs
}
} else {
return nil, errs
}
} }
highDoc := v3high.NewDocument(lowDoc) highDoc := v3high.NewDocument(lowDoc)
return &DocumentModel[v3high.Document]{ return &DocumentModel[v3high.Document]{
Model: *highDoc, Model: *highDoc,
}, nil }, errs
} }
// CompareDocuments will accept a left and right Document implementing struct, build a model for the correct // CompareDocuments will accept a left and right Document implementing struct, build a model for the correct