Changed document signatures to use error instead of []error

Also removed old swagger `CreateDocument` method that has been deprecated.

Signed-off-by: quobix <dave@quobix.com>
This commit is contained in:
quobix
2023-10-24 12:31:47 -04:00
parent d30ac24db9
commit 5d717bdefe
14 changed files with 305 additions and 267 deletions

View File

@@ -4,6 +4,7 @@
package datamodel package datamodel
import ( import (
"io/fs"
"log/slog" "log/slog"
"net/http" "net/http"
"net/url" "net/url"
@@ -29,10 +30,6 @@ type DocumentConfiguration struct {
// Resolves [#132]: https://github.com/pb33f/libopenapi/issues/132 // Resolves [#132]: https://github.com/pb33f/libopenapi/issues/132
RemoteURLHandler func(url string) (*http.Response, error) RemoteURLHandler func(url string) (*http.Response, error)
// FileFilter is a list of specific files to be included by the rolodex when looking up references. If this value
// is set, then only these specific files will be included. If this value is not set, then all files will be included.
FileFilter []string
// If resolving locally, the BasePath will be the root from which relative references will be resolved from. // If resolving locally, the BasePath will be the root from which relative references will be resolved from.
// It's usually the location of the root specification. // It's usually the location of the root specification.
// //
@@ -42,6 +39,19 @@ type DocumentConfiguration struct {
// To avoid sucking in all the files, set the FileFilter to a list of specific files to be included. // To avoid sucking in all the files, set the FileFilter to a list of specific files to be included.
BasePath string // set the Base Path for resolving relative references if the spec is exploded. BasePath string // set the Base Path for resolving relative references if the spec is exploded.
// FileFilter is a list of specific files to be included by the rolodex when looking up references. If this value
// is set, then only these specific files will be included. If this value is not set, then all files will be included.
FileFilter []string
// RemoteFS is a filesystem that will be used to retrieve remote documents. If not set, then the rolodex will
// use its own internal remote filesystem implementation. The RemoteURLHandler will be used to retrieve remote
// documents if it has been set. The default is to use the internal remote filesystem loader.
RemoteFS fs.FS
// LocalFS is a filesystem that will be used to retrieve local documents. If not set, then the rolodex will
// use its own internal local filesystem implementation. The default is to use the internal local filesystem loader.
LocalFS fs.FS
// AllowFileReferences will allow the index to locate relative file references. This is disabled by default. // AllowFileReferences will allow the index to locate relative file references. This is disabled by default.
// //
// Deprecated: This behavior is now driven by the inclusion of a BasePath. If a BasePath is set, then the // Deprecated: This behavior is now driven by the inclusion of a BasePath. If a BasePath is set, then the

View File

@@ -302,7 +302,6 @@ func NewSchema(schema *base.Schema) *Schema {
s.Anchor = schema.Anchor.Value s.Anchor = schema.Anchor.Value
} }
// TODO: check this behavior.
for i := range schema.Enum.Value { for i := range schema.Enum.Value {
enum = append(enum, schema.Enum.Value[i].Value) enum = append(enum, schema.Enum.Value[i].Value)
} }

View File

@@ -18,8 +18,8 @@ var doc *v2.Swagger
func initTest() { func initTest() {
data, _ := os.ReadFile("../../../test_specs/petstorev2-complete.yaml") data, _ := os.ReadFile("../../../test_specs/petstorev2-complete.yaml")
info, _ := datamodel.ExtractSpecInfo(data) info, _ := datamodel.ExtractSpecInfo(data)
var err []error var err error
doc, err = v2.CreateDocument(info) doc, err = v2.CreateDocumentFromConfig(info, datamodel.NewDocumentConfiguration())
if err != nil { if err != nil {
panic("broken something") panic("broken something")
} }

View File

@@ -398,10 +398,10 @@ func TestStripeAsDoc(t *testing.T) {
func TestK8sAsDoc(t *testing.T) { func TestK8sAsDoc(t *testing.T) {
data, _ := os.ReadFile("../../../test_specs/k8s.json") data, _ := os.ReadFile("../../../test_specs/k8s.json")
info, _ := datamodel.ExtractSpecInfo(data) info, _ := datamodel.ExtractSpecInfo(data)
var err []error var err error
lowSwag, err := lowv2.CreateDocumentFromConfig(info, datamodel.NewDocumentConfiguration()) lowSwag, err := lowv2.CreateDocumentFromConfig(info, datamodel.NewDocumentConfiguration())
d := v2.NewSwaggerDocument(lowSwag) d := v2.NewSwaggerDocument(lowSwag)
assert.Len(t, err, 0) assert.Len(t, utils.UnwrapErrors(err), 0)
assert.NotNil(t, d) assert.NotNil(t, d)
} }

View File

@@ -74,25 +74,3 @@ func (t *Tag) Hash() [32]byte {
f = append(f, keys...) f = append(f, keys...)
return sha256.Sum256([]byte(strings.Join(f, "|"))) return sha256.Sum256([]byte(strings.Join(f, "|")))
} }
// TODO: future mutation API experiment code is here. this snippet is to re-marshal the object.
//func (t *Tag) MarshalYAML() (interface{}, error) {
// m := make(map[string]interface{})
// for i := range t.Extensions {
// m[i.Value] = t.Extensions[i].Value
// }
// if t.Name.Value != "" {
// m[NameLabel] = t.Name.Value
// }
// if t.Description.Value != "" {
// m[DescriptionLabel] = t.Description.Value
// }
// if t.ExternalDocs.Value != nil {
// m[ExternalDocsLabel] = t.ExternalDocs.Value
// }
// return m, nil
//}
//
//func NewTag() *Tag {
// return new(Tag)
//}

View File

@@ -5,6 +5,7 @@ package v2
import ( import (
"fmt" "fmt"
"github.com/pb33f/libopenapi/utils"
"os" "os"
"github.com/pb33f/libopenapi/datamodel" "github.com/pb33f/libopenapi/datamodel"
@@ -22,12 +23,13 @@ func Example_createLowLevelSwaggerDocument() {
info, _ := datamodel.ExtractSpecInfo(petstoreBytes) info, _ := datamodel.ExtractSpecInfo(petstoreBytes)
// build low-level document model // build low-level document model
document, errors := CreateDocument(info) document, err := CreateDocumentFromConfig(info, datamodel.NewDocumentConfiguration())
// if something went wrong, a slice of errors is returned // if something went wrong, a slice of errors is returned
if len(errors) > 0 { errs := utils.UnwrapErrors(err)
for i := range errors { if len(errs) > 0 {
fmt.Printf("error: %s\n", errors[i].Error()) for i := range errs {
fmt.Printf("error: %s\n", errs[i].Error())
} }
panic("cannot build document") panic("cannot build document")
} }
@@ -50,12 +52,13 @@ func ExampleCreateDocument() {
info, _ := datamodel.ExtractSpecInfo(petstoreBytes) info, _ := datamodel.ExtractSpecInfo(petstoreBytes)
// build low-level document model // build low-level document model
document, errors := CreateDocument(info) document, err := CreateDocumentFromConfig(info, datamodel.NewDocumentConfiguration())
// if something went wrong, a slice of errors is returned // if something went wrong, a slice of errors is returned
if len(errors) > 0 { errs := utils.UnwrapErrors(err)
for i := range errors { if len(errs) > 0 {
fmt.Printf("error: %s\n", errors[i].Error()) for i := range errs {
fmt.Printf("error: %s\n", errs[i].Error())
} }
panic("cannot build document") panic("cannot build document")
} }

View File

@@ -13,11 +13,14 @@ package v2
import ( import (
"context" "context"
"errors"
"github.com/pb33f/libopenapi/datamodel" "github.com/pb33f/libopenapi/datamodel"
"github.com/pb33f/libopenapi/datamodel/low" "github.com/pb33f/libopenapi/datamodel/low"
"github.com/pb33f/libopenapi/datamodel/low/base" "github.com/pb33f/libopenapi/datamodel/low/base"
"github.com/pb33f/libopenapi/index" "github.com/pb33f/libopenapi/index"
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
"os"
"path/filepath"
) )
// processes a property of a Swagger document asynchronously using bool and error channels for signals. // processes a property of a Swagger document asynchronously using bool and error channels for signals.
@@ -109,6 +112,10 @@ type Swagger struct {
// //
// This property is not a part of the OpenAPI schema, this is custom to libopenapi. // This property is not a part of the OpenAPI schema, this is custom to libopenapi.
SpecInfo *datamodel.SpecInfo SpecInfo *datamodel.SpecInfo
// Rolodex is a reference to the index.Rolodex instance created when the specification was read.
// The rolodex is used to look up references from file systems (local or remote)
Rolodex *index.Rolodex
} }
// FindExtension locates an extension from the root of the Swagger document. // FindExtension locates an extension from the root of the Swagger document.
@@ -123,38 +130,102 @@ func (s *Swagger) GetExtensions() map[low.KeyReference[string]]low.ValueReferenc
// CreateDocumentFromConfig will create a new Swagger document from the provided SpecInfo and DocumentConfiguration. // CreateDocumentFromConfig will create a new Swagger document from the provided SpecInfo and DocumentConfiguration.
func CreateDocumentFromConfig(info *datamodel.SpecInfo, func CreateDocumentFromConfig(info *datamodel.SpecInfo,
configuration *datamodel.DocumentConfiguration) (*Swagger, []error) { configuration *datamodel.DocumentConfiguration) (*Swagger, error) {
return createDocument(info, configuration) return createDocument(info, configuration)
} }
// CreateDocument will create a new Swagger document from the provided SpecInfo. func createDocument(info *datamodel.SpecInfo, config *datamodel.DocumentConfiguration) (*Swagger, error) {
//
// Deprecated: Use CreateDocumentFromConfig instead.
// TODO; DELETE ME
func CreateDocument(info *datamodel.SpecInfo) (*Swagger, []error) {
return createDocument(info, &datamodel.DocumentConfiguration{
AllowRemoteReferences: true,
AllowFileReferences: true,
})
}
func createDocument(info *datamodel.SpecInfo, config *datamodel.DocumentConfiguration) (*Swagger, []error) {
doc := Swagger{Swagger: low.ValueReference[string]{Value: info.Version, ValueNode: info.RootNode}} doc := Swagger{Swagger: low.ValueReference[string]{Value: info.Version, ValueNode: info.RootNode}}
doc.Extensions = low.ExtractExtensions(info.RootNode.Content[0]) doc.Extensions = low.ExtractExtensions(info.RootNode.Content[0])
// build an index // create an index config and shadow the document configuration.
idx := index.NewSpecIndexWithConfig(info.RootNode, &index.SpecIndexConfig{ idxConfig := index.CreateClosedAPIIndexConfig()
BaseURL: config.BaseURL, idxConfig.SpecInfo = info
RemoteURLHandler: config.RemoteURLHandler, idxConfig.IgnoreArrayCircularReferences = config.IgnoreArrayCircularReferences
//AllowRemoteLookup: config.AllowRemoteReferences, idxConfig.IgnorePolymorphicCircularReferences = config.IgnorePolymorphicCircularReferences
//AllowFileLookup: config.AllowFileReferences, idxConfig.AvoidCircularReferenceCheck = true
}) idxConfig.BaseURL = config.BaseURL
doc.Index = idx idxConfig.BasePath = config.BasePath
doc.SpecInfo = info idxConfig.Logger = config.Logger
rolodex := index.NewRolodex(idxConfig)
rolodex.SetRootNode(info.RootNode)
doc.Rolodex = rolodex
var errors []error // If basePath is provided, add a local filesystem to the rolodex.
if idxConfig.BasePath != "" {
var absError error
var cwd string
cwd, absError = filepath.Abs(config.BasePath)
if absError != nil {
return nil, absError
}
// if a supplied local filesystem is provided, add it to the rolodex.
if config.LocalFS != nil {
rolodex.AddLocalFS(cwd, config.LocalFS)
} else {
// create a local filesystem
localFSConf := index.LocalFSConfig{
BaseDirectory: cwd,
DirFS: os.DirFS(cwd),
FileFilters: config.FileFilter,
}
fileFS, err := index.NewLocalFSWithConfig(&localFSConf)
if err != nil {
return nil, err
}
idxConfig.AllowFileLookup = true
// add the filesystem to the rolodex
rolodex.AddLocalFS(cwd, fileFS)
}
}
// if base url is provided, add a remote filesystem to the rolodex.
if idxConfig.BaseURL != nil {
// if a supplied remote filesystem is provided, add it to the rolodex.
if config.RemoteFS != nil {
if config.BaseURL == nil {
return nil, errors.New("cannot use remote filesystem without a BaseURL")
}
rolodex.AddRemoteFS(config.BaseURL.String(), config.RemoteFS)
} else {
// create a remote filesystem
remoteFS, fsErr := index.NewRemoteFSWithConfig(idxConfig)
if fsErr != nil {
return nil, fsErr
}
if config.RemoteURLHandler != nil {
remoteFS.RemoteHandlerFunc = config.RemoteURLHandler
}
idxConfig.AllowRemoteLookup = true
// add to the rolodex
rolodex.AddRemoteFS(config.BaseURL.String(), remoteFS)
}
}
var errs []error
// index all the things!
_ = rolodex.IndexTheRolodex()
// check for circular references
if !config.SkipCircularReferenceCheck {
rolodex.CheckForCircularReferences()
}
// extract errors
roloErrs := rolodex.GetCaughtErrors()
if roloErrs != nil {
errs = append(errs, roloErrs...)
}
// set the index on the document.
doc.Index = rolodex.GetRootIndex()
doc.SpecInfo = info
// build out swagger scalar variables. // build out swagger scalar variables.
_ = low.BuildModel(info.RootNode.Content[0], &doc) _ = low.BuildModel(info.RootNode.Content[0], &doc)
@@ -162,23 +233,13 @@ func createDocument(info *datamodel.SpecInfo, config *datamodel.DocumentConfigur
ctx := context.Background() ctx := context.Background()
// extract externalDocs // extract externalDocs
extDocs, err := low.ExtractObject[*base.ExternalDoc](ctx, base.ExternalDocsLabel, info.RootNode, idx) extDocs, err := low.ExtractObject[*base.ExternalDoc](ctx, base.ExternalDocsLabel, info.RootNode, rolodex.GetRootIndex())
if err != nil { if err != nil {
errors = append(errors, err) errs = append(errs, err)
} }
doc.ExternalDocs = extDocs doc.ExternalDocs = extDocs
// create resolver and check for circular references.
resolve := index.NewResolver(idx)
resolvingErrors := resolve.CheckForCircularReferences()
if len(resolvingErrors) > 0 {
for r := range resolvingErrors {
errors = append(errors, resolvingErrors[r])
}
}
extractionFuncs := []documentFunction{ extractionFuncs := []documentFunction{
extractInfo, extractInfo,
extractPaths, extractPaths,
@@ -192,7 +253,7 @@ func createDocument(info *datamodel.SpecInfo, config *datamodel.DocumentConfigur
doneChan := make(chan bool) doneChan := make(chan bool)
errChan := make(chan error) errChan := make(chan error)
for i := range extractionFuncs { for i := range extractionFuncs {
go extractionFuncs[i](ctx, info.RootNode.Content[0], &doc, idx, doneChan, errChan) go extractionFuncs[i](ctx, info.RootNode.Content[0], &doc, rolodex.GetRootIndex(), doneChan, errChan)
} }
completedExtractions := 0 completedExtractions := 0
for completedExtractions < len(extractionFuncs) { for completedExtractions < len(extractionFuncs) {
@@ -201,11 +262,11 @@ func createDocument(info *datamodel.SpecInfo, config *datamodel.DocumentConfigur
completedExtractions++ completedExtractions++
case e := <-errChan: case e := <-errChan:
completedExtractions++ completedExtractions++
errors = append(errors, e) errs = append(errs, e)
} }
} }
return &doc, errors return &doc, errors.Join(errs...)
} }
func (s *Swagger) GetExternalDocs() *low.NodeReference[any] { func (s *Swagger) GetExternalDocs() *low.NodeReference[any] {

View File

@@ -5,6 +5,7 @@ package v2
import ( import (
"fmt" "fmt"
"github.com/pb33f/libopenapi/utils"
"os" "os"
"testing" "testing"
@@ -20,11 +21,8 @@ func initTest() {
} }
data, _ := os.ReadFile("../../../test_specs/petstorev2-complete.yaml") data, _ := os.ReadFile("../../../test_specs/petstorev2-complete.yaml")
info, _ := datamodel.ExtractSpecInfo(data) info, _ := datamodel.ExtractSpecInfo(data)
var err []error var err error
doc, err = CreateDocumentFromConfig(info, &datamodel.DocumentConfiguration{ doc, err = CreateDocumentFromConfig(info, datamodel.NewDocumentConfiguration())
AllowFileReferences: false,
AllowRemoteReferences: false,
})
wait := true wait := true
for wait { for wait {
select { select {
@@ -42,10 +40,7 @@ func BenchmarkCreateDocument(b *testing.B) {
data, _ := os.ReadFile("../../../test_specs/petstorev2-complete.yaml") data, _ := os.ReadFile("../../../test_specs/petstorev2-complete.yaml")
info, _ := datamodel.ExtractSpecInfo(data) info, _ := datamodel.ExtractSpecInfo(data)
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
doc, _ = CreateDocumentFromConfig(info, &datamodel.DocumentConfiguration{ doc, _ = CreateDocumentFromConfig(info, datamodel.NewDocumentConfiguration())
AllowFileReferences: false,
AllowRemoteReferences: false,
})
} }
} }
@@ -183,8 +178,8 @@ func TestCreateDocument_ExternalDocsBad(t *testing.T) {
$ref: bork` $ref: bork`
info, _ := datamodel.ExtractSpecInfo([]byte(yml)) info, _ := datamodel.ExtractSpecInfo([]byte(yml))
var err []error var err error
doc, err = CreateDocument(info) doc, err = CreateDocumentFromConfig(info, datamodel.NewDocumentConfiguration())
wait := true wait := true
for wait { for wait {
select { select {
@@ -192,7 +187,7 @@ func TestCreateDocument_ExternalDocsBad(t *testing.T) {
wait = false wait = false
} }
} }
assert.Len(t, err, 1) assert.Len(t, utils.UnwrapErrors(err), 2)
} }
func TestCreateDocument_TagsBad(t *testing.T) { func TestCreateDocument_TagsBad(t *testing.T) {
@@ -201,8 +196,8 @@ func TestCreateDocument_TagsBad(t *testing.T) {
$ref: bork` $ref: bork`
info, _ := datamodel.ExtractSpecInfo([]byte(yml)) info, _ := datamodel.ExtractSpecInfo([]byte(yml))
var err []error var err error
doc, err = CreateDocument(info) doc, err = CreateDocumentFromConfig(info, datamodel.NewDocumentConfiguration())
wait := true wait := true
for wait { for wait {
select { select {
@@ -210,7 +205,7 @@ func TestCreateDocument_TagsBad(t *testing.T) {
wait = false wait = false
} }
} }
assert.Len(t, err, 1) assert.Len(t, utils.UnwrapErrors(err), 2)
} }
func TestCreateDocument_PathsBad(t *testing.T) { func TestCreateDocument_PathsBad(t *testing.T) {
@@ -223,8 +218,8 @@ func TestCreateDocument_PathsBad(t *testing.T) {
$ref: bork` $ref: bork`
info, _ := datamodel.ExtractSpecInfo([]byte(yml)) info, _ := datamodel.ExtractSpecInfo([]byte(yml))
var err []error var err error
doc, err = CreateDocument(info) doc, err = CreateDocumentFromConfig(info, datamodel.NewDocumentConfiguration())
wait := true wait := true
for wait { for wait {
select { select {
@@ -232,7 +227,7 @@ func TestCreateDocument_PathsBad(t *testing.T) {
wait = false wait = false
} }
} }
assert.Len(t, err, 1) assert.Len(t, utils.UnwrapErrors(err), 2)
} }
func TestCreateDocument_SecurityBad(t *testing.T) { func TestCreateDocument_SecurityBad(t *testing.T) {
@@ -241,8 +236,8 @@ func TestCreateDocument_SecurityBad(t *testing.T) {
$ref: ` $ref: `
info, _ := datamodel.ExtractSpecInfo([]byte(yml)) info, _ := datamodel.ExtractSpecInfo([]byte(yml))
var err []error var err error
doc, err = CreateDocument(info) doc, err = CreateDocumentFromConfig(info, datamodel.NewDocumentConfiguration())
wait := true wait := true
for wait { for wait {
select { select {
@@ -250,7 +245,7 @@ func TestCreateDocument_SecurityBad(t *testing.T) {
wait = false wait = false
} }
} }
assert.Len(t, err, 1) assert.Len(t, utils.UnwrapErrors(err), 1)
} }
func TestCreateDocument_SecurityDefinitionsBad(t *testing.T) { func TestCreateDocument_SecurityDefinitionsBad(t *testing.T) {
@@ -259,8 +254,8 @@ func TestCreateDocument_SecurityDefinitionsBad(t *testing.T) {
$ref: ` $ref: `
info, _ := datamodel.ExtractSpecInfo([]byte(yml)) info, _ := datamodel.ExtractSpecInfo([]byte(yml))
var err []error var err error
doc, err = CreateDocument(info) doc, err = CreateDocumentFromConfig(info, datamodel.NewDocumentConfiguration())
wait := true wait := true
for wait { for wait {
select { select {
@@ -268,7 +263,7 @@ func TestCreateDocument_SecurityDefinitionsBad(t *testing.T) {
wait = false wait = false
} }
} }
assert.Len(t, err, 1) assert.Len(t, utils.UnwrapErrors(err), 1)
} }
func TestCreateDocument_ResponsesBad(t *testing.T) { func TestCreateDocument_ResponsesBad(t *testing.T) {
@@ -277,8 +272,8 @@ func TestCreateDocument_ResponsesBad(t *testing.T) {
$ref: ` $ref: `
info, _ := datamodel.ExtractSpecInfo([]byte(yml)) info, _ := datamodel.ExtractSpecInfo([]byte(yml))
var err []error var err error
doc, err = CreateDocument(info) doc, err = CreateDocumentFromConfig(info, datamodel.NewDocumentConfiguration())
wait := true wait := true
for wait { for wait {
select { select {
@@ -286,7 +281,7 @@ func TestCreateDocument_ResponsesBad(t *testing.T) {
wait = false wait = false
} }
} }
assert.Len(t, err, 1) assert.Len(t, utils.UnwrapErrors(err), 1)
} }
func TestCreateDocument_ParametersBad(t *testing.T) { func TestCreateDocument_ParametersBad(t *testing.T) {
@@ -295,8 +290,8 @@ func TestCreateDocument_ParametersBad(t *testing.T) {
$ref: ` $ref: `
info, _ := datamodel.ExtractSpecInfo([]byte(yml)) info, _ := datamodel.ExtractSpecInfo([]byte(yml))
var err []error var err error
doc, err = CreateDocument(info) doc, err = CreateDocumentFromConfig(info, datamodel.NewDocumentConfiguration())
wait := true wait := true
for wait { for wait {
select { select {
@@ -304,7 +299,7 @@ func TestCreateDocument_ParametersBad(t *testing.T) {
wait = false wait = false
} }
} }
assert.Len(t, err, 1) assert.Len(t, utils.UnwrapErrors(err), 1)
} }
func TestCreateDocument_DefinitionsBad(t *testing.T) { func TestCreateDocument_DefinitionsBad(t *testing.T) {
@@ -313,8 +308,8 @@ func TestCreateDocument_DefinitionsBad(t *testing.T) {
$ref: ` $ref: `
info, _ := datamodel.ExtractSpecInfo([]byte(yml)) info, _ := datamodel.ExtractSpecInfo([]byte(yml))
var err []error var err error
doc, err = CreateDocument(info) doc, err = CreateDocumentFromConfig(info, datamodel.NewDocumentConfiguration())
wait := true wait := true
for wait { for wait {
select { select {
@@ -322,7 +317,7 @@ func TestCreateDocument_DefinitionsBad(t *testing.T) {
wait = false wait = false
} }
} }
assert.Len(t, err, 1) assert.Len(t, utils.UnwrapErrors(err), 1)
} }
func TestCreateDocument_InfoBad(t *testing.T) { func TestCreateDocument_InfoBad(t *testing.T) {
@@ -331,8 +326,8 @@ func TestCreateDocument_InfoBad(t *testing.T) {
$ref: ` $ref: `
info, _ := datamodel.ExtractSpecInfo([]byte(yml)) info, _ := datamodel.ExtractSpecInfo([]byte(yml))
var err []error var err error
doc, err = CreateDocument(info) doc, err = CreateDocumentFromConfig(info, datamodel.NewDocumentConfiguration())
wait := true wait := true
for wait { for wait {
select { select {
@@ -340,15 +335,15 @@ func TestCreateDocument_InfoBad(t *testing.T) {
wait = false wait = false
} }
} }
assert.Len(t, err, 1) assert.Len(t, utils.UnwrapErrors(err), 1)
} }
func TestCircularReferenceError(t *testing.T) { func TestCircularReferenceError(t *testing.T) {
data, _ := os.ReadFile("../../../test_specs/swagger-circular-tests.yaml") data, _ := os.ReadFile("../../../test_specs/swagger-circular-tests.yaml")
info, _ := datamodel.ExtractSpecInfo(data) info, _ := datamodel.ExtractSpecInfo(data)
circDoc, err := CreateDocument(info) circDoc, err := CreateDocumentFromConfig(info, datamodel.NewDocumentConfiguration())
assert.NotNil(t, circDoc) assert.NotNil(t, circDoc)
assert.Len(t, err, 3) assert.Len(t, utils.UnwrapErrors(err), 3)
} }

View File

@@ -57,6 +57,10 @@ func createDocument(info *datamodel.SpecInfo, config *datamodel.DocumentConfigur
if absError != nil { if absError != nil {
return nil, absError return nil, absError
} }
// if a supplied local filesystem is provided, add it to the rolodex.
if config.LocalFS != nil {
rolodex.AddLocalFS(cwd, config.LocalFS)
} else {
// create a local filesystem // create a local filesystem
localFSConf := index.LocalFSConfig{ localFSConf := index.LocalFSConfig{
@@ -65,21 +69,28 @@ func createDocument(info *datamodel.SpecInfo, config *datamodel.DocumentConfigur
FileFilters: config.FileFilter, FileFilters: config.FileFilter,
} }
fileFS, err := index.NewLocalFSWithConfig(&localFSConf) fileFS, err := index.NewLocalFSWithConfig(&localFSConf)
if err != nil { if err != nil {
return nil, err return nil, err
} }
idxConfig.AllowFileLookup = true idxConfig.AllowFileLookup = true
// add the filesystem to the rolodex // add the filesystem to the rolodex
rolodex.AddLocalFS(cwd, fileFS) rolodex.AddLocalFS(cwd, fileFS)
}
} }
// if base url is provided, add a remote filesystem to the rolodex. // if base url is provided, add a remote filesystem to the rolodex.
if idxConfig.BaseURL != nil { if idxConfig.BaseURL != nil {
// if a supplied remote filesystem is provided, add it to the rolodex.
if config.RemoteFS != nil {
if config.BaseURL == nil {
return nil, errors.New("cannot use remote filesystem without a BaseURL")
}
rolodex.AddRemoteFS(config.BaseURL.String(), config.RemoteFS)
} else {
// create a remote filesystem // create a remote filesystem
remoteFS, fsErr := index.NewRemoteFSWithConfig(idxConfig) remoteFS, fsErr := index.NewRemoteFSWithConfig(idxConfig)
if fsErr != nil { if fsErr != nil {
@@ -88,55 +99,32 @@ func createDocument(info *datamodel.SpecInfo, config *datamodel.DocumentConfigur
if config.RemoteURLHandler != nil { if config.RemoteURLHandler != nil {
remoteFS.RemoteHandlerFunc = config.RemoteURLHandler remoteFS.RemoteHandlerFunc = config.RemoteURLHandler
} }
idxConfig.AllowRemoteLookup = true idxConfig.AllowRemoteLookup = true
// add to the rolodex // add to the rolodex
rolodex.AddRemoteFS(config.BaseURL.String(), remoteFS) rolodex.AddRemoteFS(config.BaseURL.String(), remoteFS)
} }
}
// index the rolodex // index the rolodex
var errs []error var errs []error
// index all the things.
_ = rolodex.IndexTheRolodex() _ = rolodex.IndexTheRolodex()
// check for circular references
if !config.SkipCircularReferenceCheck { if !config.SkipCircularReferenceCheck {
rolodex.CheckForCircularReferences() rolodex.CheckForCircularReferences()
} }
// extract errors
roloErrs := rolodex.GetCaughtErrors() roloErrs := rolodex.GetCaughtErrors()
if roloErrs != nil { if roloErrs != nil {
errs = append(errs, roloErrs...) errs = append(errs, roloErrs...)
} }
// set root index.
doc.Index = rolodex.GetRootIndex() doc.Index = rolodex.GetRootIndex()
//errs = idx.GetReferenceIndexErrors()
// create resolver and check for circular references.
//resolve := resolver.NewResolver(idx)
//
//// if configured, ignore circular references in arrays and polymorphic schemas
//if config.IgnoreArrayCircularReferences {
// resolve.IgnoreArrayCircularReferences()
//}
//if config.IgnorePolymorphicCircularReferences {
// resolve.IgnorePolymorphicCircularReferences()
//}
//
//if !config.AvoidIndexBuild {
// // check for circular references.
// resolvingErrors := resolve.CheckForCircularReferences()
//
// if len(resolvingErrors) > 0 {
// for r := range resolvingErrors {
// errs = append(errs, resolvingErrors[r])
// }
// }
//}
var wg sync.WaitGroup var wg sync.WaitGroup
doc.Extensions = low.ExtractExtensions(info.RootNode.Content[0]) doc.Extensions = low.ExtractExtensions(info.RootNode.Content[0])

View File

@@ -259,13 +259,15 @@ func (d *document) BuildV2Model() (*DocumentModel[v2high.Swagger], []error) {
var lowDoc *v2low.Swagger var lowDoc *v2low.Swagger
if d.config == nil { if d.config == nil {
d.config = &datamodel.DocumentConfiguration{ d.config = datamodel.NewDocumentConfiguration()
AllowFileReferences: false,
AllowRemoteReferences: false,
}
} }
lowDoc, errs = v2low.CreateDocumentFromConfig(d.info, d.config) var docErr error
lowDoc, docErr = v2low.CreateDocumentFromConfig(d.info, d.config)
if docErr != nil {
errs = append(errs, utils.UnwrapErrors(docErr)...)
}
// Do not short-circuit on circular reference errors, so the client // Do not short-circuit on circular reference errors, so the client
// has the option of ignoring them. // has the option of ignoring them.

View File

@@ -50,7 +50,7 @@ definitions:
assert.NoError(t, err) assert.NoError(t, err)
v2Doc, docErr := doc.BuildV2Model() v2Doc, docErr := doc.BuildV2Model()
assert.Len(t, docErr, 2) assert.Len(t, docErr, 3)
assert.Nil(t, v2Doc) assert.Nil(t, v2Doc)
} }
@@ -515,7 +515,7 @@ func TestDocument_BuildModel_CompareDocsV2_Error(t *testing.T) {
originalDoc, _ := NewDocument(burgerShopOriginal) originalDoc, _ := NewDocument(burgerShopOriginal)
updatedDoc, _ := NewDocument(burgerShopUpdated) updatedDoc, _ := NewDocument(burgerShopUpdated)
changes, errors := CompareDocuments(updatedDoc, originalDoc) changes, errors := CompareDocuments(updatedDoc, originalDoc)
assert.Len(t, errors, 2) assert.Len(t, errors, 14)
assert.Nil(t, changes) assert.Nil(t, changes)
} }

View File

@@ -79,8 +79,8 @@ produces:
siLeft, _ := datamodel.ExtractSpecInfo([]byte(left)) siLeft, _ := datamodel.ExtractSpecInfo([]byte(left))
siRight, _ := datamodel.ExtractSpecInfo([]byte(right)) siRight, _ := datamodel.ExtractSpecInfo([]byte(right))
lDoc, _ := v2.CreateDocument(siLeft) lDoc, _ := v2.CreateDocumentFromConfig(siLeft, datamodel.NewDocumentConfiguration())
rDoc, _ := v2.CreateDocument(siRight) rDoc, _ := v2.CreateDocumentFromConfig(siRight, datamodel.NewDocumentConfiguration())
// compare. // compare.
extChanges := CompareDocuments(lDoc, rDoc) extChanges := CompareDocuments(lDoc, rDoc)
@@ -108,8 +108,8 @@ produces:
siLeft, _ := datamodel.ExtractSpecInfo([]byte(left)) siLeft, _ := datamodel.ExtractSpecInfo([]byte(left))
siRight, _ := datamodel.ExtractSpecInfo([]byte(right)) siRight, _ := datamodel.ExtractSpecInfo([]byte(right))
lDoc, _ := v2.CreateDocument(siLeft) lDoc, _ := v2.CreateDocumentFromConfig(siLeft, datamodel.NewDocumentConfiguration())
rDoc, _ := v2.CreateDocument(siRight) rDoc, _ := v2.CreateDocumentFromConfig(siRight, datamodel.NewDocumentConfiguration())
// compare. // compare.
extChanges := CompareDocuments(lDoc, rDoc) extChanges := CompareDocuments(lDoc, rDoc)
@@ -137,8 +137,8 @@ basePath: /api`
siLeft, _ := datamodel.ExtractSpecInfo([]byte(left)) siLeft, _ := datamodel.ExtractSpecInfo([]byte(left))
siRight, _ := datamodel.ExtractSpecInfo([]byte(right)) siRight, _ := datamodel.ExtractSpecInfo([]byte(right))
lDoc, _ := v2.CreateDocument(siLeft) lDoc, _ := v2.CreateDocumentFromConfig(siLeft, datamodel.NewDocumentConfiguration())
rDoc, _ := v2.CreateDocument(siRight) rDoc, _ := v2.CreateDocumentFromConfig(siRight, datamodel.NewDocumentConfiguration())
// compare. // compare.
extChanges := CompareDocuments(lDoc, rDoc) extChanges := CompareDocuments(lDoc, rDoc)
@@ -168,8 +168,8 @@ info:
siLeft, _ := datamodel.ExtractSpecInfo([]byte(left)) siLeft, _ := datamodel.ExtractSpecInfo([]byte(left))
siRight, _ := datamodel.ExtractSpecInfo([]byte(right)) siRight, _ := datamodel.ExtractSpecInfo([]byte(right))
lDoc, _ := v2.CreateDocument(siLeft) lDoc, _ := v2.CreateDocumentFromConfig(siLeft, datamodel.NewDocumentConfiguration())
rDoc, _ := v2.CreateDocument(siRight) rDoc, _ := v2.CreateDocumentFromConfig(siRight, datamodel.NewDocumentConfiguration())
// compare. // compare.
extChanges := CompareDocuments(lDoc, rDoc) extChanges := CompareDocuments(lDoc, rDoc)
@@ -194,8 +194,8 @@ info:
siLeft, _ := datamodel.ExtractSpecInfo([]byte(left)) siLeft, _ := datamodel.ExtractSpecInfo([]byte(left))
siRight, _ := datamodel.ExtractSpecInfo([]byte(right)) siRight, _ := datamodel.ExtractSpecInfo([]byte(right))
lDoc, _ := v2.CreateDocument(siLeft) lDoc, _ := v2.CreateDocumentFromConfig(siLeft, datamodel.NewDocumentConfiguration())
rDoc, _ := v2.CreateDocument(siRight) rDoc, _ := v2.CreateDocumentFromConfig(siRight, datamodel.NewDocumentConfiguration())
// compare. // compare.
extChanges := CompareDocuments(lDoc, rDoc) extChanges := CompareDocuments(lDoc, rDoc)
@@ -221,8 +221,8 @@ info:
siLeft, _ := datamodel.ExtractSpecInfo([]byte(left)) siLeft, _ := datamodel.ExtractSpecInfo([]byte(left))
siRight, _ := datamodel.ExtractSpecInfo([]byte(right)) siRight, _ := datamodel.ExtractSpecInfo([]byte(right))
lDoc, _ := v2.CreateDocument(siLeft) lDoc, _ := v2.CreateDocumentFromConfig(siLeft, datamodel.NewDocumentConfiguration())
rDoc, _ := v2.CreateDocument(siRight) rDoc, _ := v2.CreateDocumentFromConfig(siRight, datamodel.NewDocumentConfiguration())
// compare. // compare.
extChanges := CompareDocuments(rDoc, lDoc) extChanges := CompareDocuments(rDoc, lDoc)
@@ -248,8 +248,8 @@ externalDocs:
siLeft, _ := datamodel.ExtractSpecInfo([]byte(left)) siLeft, _ := datamodel.ExtractSpecInfo([]byte(left))
siRight, _ := datamodel.ExtractSpecInfo([]byte(right)) siRight, _ := datamodel.ExtractSpecInfo([]byte(right))
lDoc, _ := v2.CreateDocument(siLeft) lDoc, _ := v2.CreateDocumentFromConfig(siLeft, datamodel.NewDocumentConfiguration())
rDoc, _ := v2.CreateDocument(siRight) rDoc, _ := v2.CreateDocumentFromConfig(siRight, datamodel.NewDocumentConfiguration())
// compare. // compare.
extChanges := CompareDocuments(lDoc, rDoc) extChanges := CompareDocuments(lDoc, rDoc)
@@ -271,8 +271,8 @@ externalDocs:
siLeft, _ := datamodel.ExtractSpecInfo([]byte(left)) siLeft, _ := datamodel.ExtractSpecInfo([]byte(left))
siRight, _ := datamodel.ExtractSpecInfo([]byte(right)) siRight, _ := datamodel.ExtractSpecInfo([]byte(right))
lDoc, _ := v2.CreateDocument(siLeft) lDoc, _ := v2.CreateDocumentFromConfig(siLeft, datamodel.NewDocumentConfiguration())
rDoc, _ := v2.CreateDocument(siRight) rDoc, _ := v2.CreateDocumentFromConfig(siRight, datamodel.NewDocumentConfiguration())
// compare. // compare.
extChanges := CompareDocuments(lDoc, rDoc) extChanges := CompareDocuments(lDoc, rDoc)
@@ -296,8 +296,8 @@ externalDocs:
siLeft, _ := datamodel.ExtractSpecInfo([]byte(left)) siLeft, _ := datamodel.ExtractSpecInfo([]byte(left))
siRight, _ := datamodel.ExtractSpecInfo([]byte(right)) siRight, _ := datamodel.ExtractSpecInfo([]byte(right))
lDoc, _ := v2.CreateDocument(siLeft) lDoc, _ := v2.CreateDocumentFromConfig(siLeft, datamodel.NewDocumentConfiguration())
rDoc, _ := v2.CreateDocument(siRight) rDoc, _ := v2.CreateDocumentFromConfig(siRight, datamodel.NewDocumentConfiguration())
// compare. // compare.
extChanges := CompareDocuments(rDoc, lDoc) extChanges := CompareDocuments(rDoc, lDoc)
@@ -335,8 +335,8 @@ security:
siLeft, _ := datamodel.ExtractSpecInfo([]byte(left)) siLeft, _ := datamodel.ExtractSpecInfo([]byte(left))
siRight, _ := datamodel.ExtractSpecInfo([]byte(right)) siRight, _ := datamodel.ExtractSpecInfo([]byte(right))
lDoc, _ := v2.CreateDocument(siLeft) lDoc, _ := v2.CreateDocumentFromConfig(siLeft, datamodel.NewDocumentConfiguration())
rDoc, _ := v2.CreateDocument(siRight) rDoc, _ := v2.CreateDocumentFromConfig(siRight, datamodel.NewDocumentConfiguration())
// compare. // compare.
extChanges := CompareDocuments(lDoc, rDoc) extChanges := CompareDocuments(lDoc, rDoc)
@@ -370,8 +370,8 @@ security:
siLeft, _ := datamodel.ExtractSpecInfo([]byte(left)) siLeft, _ := datamodel.ExtractSpecInfo([]byte(left))
siRight, _ := datamodel.ExtractSpecInfo([]byte(right)) siRight, _ := datamodel.ExtractSpecInfo([]byte(right))
lDoc, _ := v2.CreateDocument(siLeft) lDoc, _ := v2.CreateDocumentFromConfig(siLeft, datamodel.NewDocumentConfiguration())
rDoc, _ := v2.CreateDocument(siRight) rDoc, _ := v2.CreateDocumentFromConfig(siRight, datamodel.NewDocumentConfiguration())
// compare. // compare.
extChanges := CompareDocuments(lDoc, rDoc) extChanges := CompareDocuments(lDoc, rDoc)
@@ -403,8 +403,8 @@ definitions:
siLeft, _ := datamodel.ExtractSpecInfo([]byte(left)) siLeft, _ := datamodel.ExtractSpecInfo([]byte(left))
siRight, _ := datamodel.ExtractSpecInfo([]byte(right)) siRight, _ := datamodel.ExtractSpecInfo([]byte(right))
lDoc, _ := v2.CreateDocument(siLeft) lDoc, _ := v2.CreateDocumentFromConfig(siLeft, datamodel.NewDocumentConfiguration())
rDoc, _ := v2.CreateDocument(siRight) rDoc, _ := v2.CreateDocumentFromConfig(siRight, datamodel.NewDocumentConfiguration())
// compare. // compare.
extChanges := CompareDocuments(lDoc, rDoc) extChanges := CompareDocuments(lDoc, rDoc)
@@ -436,8 +436,8 @@ securityDefinitions:
siLeft, _ := datamodel.ExtractSpecInfo([]byte(left)) siLeft, _ := datamodel.ExtractSpecInfo([]byte(left))
siRight, _ := datamodel.ExtractSpecInfo([]byte(right)) siRight, _ := datamodel.ExtractSpecInfo([]byte(right))
lDoc, _ := v2.CreateDocument(siLeft) lDoc, _ := v2.CreateDocumentFromConfig(siLeft, datamodel.NewDocumentConfiguration())
rDoc, _ := v2.CreateDocument(siRight) rDoc, _ := v2.CreateDocumentFromConfig(siRight, datamodel.NewDocumentConfiguration())
// compare. // compare.
extChanges := CompareDocuments(lDoc, rDoc) extChanges := CompareDocuments(lDoc, rDoc)
@@ -464,8 +464,8 @@ securityDefinitions:
siLeft, _ := datamodel.ExtractSpecInfo([]byte(left)) siLeft, _ := datamodel.ExtractSpecInfo([]byte(left))
siRight, _ := datamodel.ExtractSpecInfo([]byte(right)) siRight, _ := datamodel.ExtractSpecInfo([]byte(right))
lDoc, _ := v2.CreateDocument(siLeft) lDoc, _ := v2.CreateDocumentFromConfig(siLeft, datamodel.NewDocumentConfiguration())
rDoc, _ := v2.CreateDocument(siRight) rDoc, _ := v2.CreateDocumentFromConfig(siRight, datamodel.NewDocumentConfiguration())
// compare. // compare.
extChanges := CompareDocuments(lDoc, rDoc) extChanges := CompareDocuments(lDoc, rDoc)
@@ -501,8 +501,8 @@ parameters:
siLeft, _ := datamodel.ExtractSpecInfo([]byte(left)) siLeft, _ := datamodel.ExtractSpecInfo([]byte(left))
siRight, _ := datamodel.ExtractSpecInfo([]byte(right)) siRight, _ := datamodel.ExtractSpecInfo([]byte(right))
lDoc, _ := v2.CreateDocument(siLeft) lDoc, _ := v2.CreateDocumentFromConfig(siLeft, datamodel.NewDocumentConfiguration())
rDoc, _ := v2.CreateDocument(siRight) rDoc, _ := v2.CreateDocumentFromConfig(siRight, datamodel.NewDocumentConfiguration())
// compare. // compare.
extChanges := CompareDocuments(lDoc, rDoc) extChanges := CompareDocuments(lDoc, rDoc)
@@ -533,8 +533,8 @@ parameters:
siLeft, _ := datamodel.ExtractSpecInfo([]byte(left)) siLeft, _ := datamodel.ExtractSpecInfo([]byte(left))
siRight, _ := datamodel.ExtractSpecInfo([]byte(right)) siRight, _ := datamodel.ExtractSpecInfo([]byte(right))
lDoc, _ := v2.CreateDocument(siLeft) lDoc, _ := v2.CreateDocumentFromConfig(siLeft, datamodel.NewDocumentConfiguration())
rDoc, _ := v2.CreateDocument(siRight) rDoc, _ := v2.CreateDocumentFromConfig(siRight, datamodel.NewDocumentConfiguration())
// compare. // compare.
extChanges := CompareDocuments(lDoc, rDoc) extChanges := CompareDocuments(lDoc, rDoc)
@@ -570,8 +570,8 @@ responses:
siLeft, _ := datamodel.ExtractSpecInfo([]byte(left)) siLeft, _ := datamodel.ExtractSpecInfo([]byte(left))
siRight, _ := datamodel.ExtractSpecInfo([]byte(right)) siRight, _ := datamodel.ExtractSpecInfo([]byte(right))
lDoc, _ := v2.CreateDocument(siLeft) lDoc, _ := v2.CreateDocumentFromConfig(siLeft, datamodel.NewDocumentConfiguration())
rDoc, _ := v2.CreateDocument(siRight) rDoc, _ := v2.CreateDocumentFromConfig(siRight, datamodel.NewDocumentConfiguration())
// compare. // compare.
extChanges := CompareDocuments(lDoc, rDoc) extChanges := CompareDocuments(lDoc, rDoc)
@@ -601,8 +601,8 @@ responses:
siLeft, _ := datamodel.ExtractSpecInfo([]byte(left)) siLeft, _ := datamodel.ExtractSpecInfo([]byte(left))
siRight, _ := datamodel.ExtractSpecInfo([]byte(right)) siRight, _ := datamodel.ExtractSpecInfo([]byte(right))
lDoc, _ := v2.CreateDocument(siLeft) lDoc, _ := v2.CreateDocumentFromConfig(siLeft, datamodel.NewDocumentConfiguration())
rDoc, _ := v2.CreateDocument(siRight) rDoc, _ := v2.CreateDocumentFromConfig(siRight, datamodel.NewDocumentConfiguration())
// compare. // compare.
extChanges := CompareDocuments(lDoc, rDoc) extChanges := CompareDocuments(lDoc, rDoc)
@@ -634,8 +634,8 @@ paths:
siLeft, _ := datamodel.ExtractSpecInfo([]byte(left)) siLeft, _ := datamodel.ExtractSpecInfo([]byte(left))
siRight, _ := datamodel.ExtractSpecInfo([]byte(right)) siRight, _ := datamodel.ExtractSpecInfo([]byte(right))
lDoc, _ := v2.CreateDocument(siLeft) lDoc, _ := v2.CreateDocumentFromConfig(siLeft, datamodel.NewDocumentConfiguration())
rDoc, _ := v2.CreateDocument(siRight) rDoc, _ := v2.CreateDocumentFromConfig(siRight, datamodel.NewDocumentConfiguration())
// compare. // compare.
extChanges := CompareDocuments(lDoc, rDoc) extChanges := CompareDocuments(lDoc, rDoc)
@@ -665,8 +665,8 @@ paths:
siLeft, _ := datamodel.ExtractSpecInfo([]byte(left)) siLeft, _ := datamodel.ExtractSpecInfo([]byte(left))
siRight, _ := datamodel.ExtractSpecInfo([]byte(right)) siRight, _ := datamodel.ExtractSpecInfo([]byte(right))
lDoc, _ := v2.CreateDocument(siLeft) lDoc, _ := v2.CreateDocumentFromConfig(siLeft, datamodel.NewDocumentConfiguration())
rDoc, _ := v2.CreateDocument(siRight) rDoc, _ := v2.CreateDocumentFromConfig(siRight, datamodel.NewDocumentConfiguration())
// compare. // compare.
extChanges := CompareDocuments(lDoc, rDoc) extChanges := CompareDocuments(lDoc, rDoc)
@@ -698,8 +698,8 @@ paths:
siLeft, _ := datamodel.ExtractSpecInfo([]byte(left)) siLeft, _ := datamodel.ExtractSpecInfo([]byte(left))
siRight, _ := datamodel.ExtractSpecInfo([]byte(right)) siRight, _ := datamodel.ExtractSpecInfo([]byte(right))
lDoc, _ := v2.CreateDocument(siLeft) lDoc, _ := v2.CreateDocumentFromConfig(siLeft, datamodel.NewDocumentConfiguration())
rDoc, _ := v2.CreateDocument(siRight) rDoc, _ := v2.CreateDocumentFromConfig(siRight, datamodel.NewDocumentConfiguration())
// compare. // compare.
extChanges := CompareDocuments(lDoc, rDoc) extChanges := CompareDocuments(lDoc, rDoc)
@@ -728,8 +728,8 @@ tags:
siLeft, _ := datamodel.ExtractSpecInfo([]byte(left)) siLeft, _ := datamodel.ExtractSpecInfo([]byte(left))
siRight, _ := datamodel.ExtractSpecInfo([]byte(right)) siRight, _ := datamodel.ExtractSpecInfo([]byte(right))
lDoc, _ := v2.CreateDocument(siLeft) lDoc, _ := v2.CreateDocumentFromConfig(siLeft, datamodel.NewDocumentConfiguration())
rDoc, _ := v2.CreateDocument(siRight) rDoc, _ := v2.CreateDocumentFromConfig(siRight, datamodel.NewDocumentConfiguration())
// compare. // compare.
extChanges := CompareDocuments(lDoc, rDoc) extChanges := CompareDocuments(lDoc, rDoc)
@@ -758,8 +758,8 @@ tags:
siLeft, _ := datamodel.ExtractSpecInfo([]byte(left)) siLeft, _ := datamodel.ExtractSpecInfo([]byte(left))
siRight, _ := datamodel.ExtractSpecInfo([]byte(right)) siRight, _ := datamodel.ExtractSpecInfo([]byte(right))
lDoc, _ := v2.CreateDocument(siLeft) lDoc, _ := v2.CreateDocumentFromConfig(siLeft, datamodel.NewDocumentConfiguration())
rDoc, _ := v2.CreateDocument(siRight) rDoc, _ := v2.CreateDocumentFromConfig(siRight, datamodel.NewDocumentConfiguration())
// compare. // compare.
extChanges := CompareDocuments(lDoc, rDoc) extChanges := CompareDocuments(lDoc, rDoc)
@@ -783,8 +783,8 @@ jsonSchemaDialect: https://pb33f.io/schema`
siLeft, _ := datamodel.ExtractSpecInfo([]byte(left)) siLeft, _ := datamodel.ExtractSpecInfo([]byte(left))
siRight, _ := datamodel.ExtractSpecInfo([]byte(right)) siRight, _ := datamodel.ExtractSpecInfo([]byte(right))
lDoc, _ := v3.CreateDocument(siLeft) lDoc, _ := v3.CreateDocumentFromConfig(siLeft, datamodel.NewDocumentConfiguration())
rDoc, _ := v3.CreateDocument(siRight) rDoc, _ := v3.CreateDocumentFromConfig(siRight, datamodel.NewDocumentConfiguration())
// compare. // compare.
extChanges := CompareDocuments(&lDoc, &rDoc) extChanges := CompareDocuments(&lDoc, &rDoc)
@@ -811,8 +811,8 @@ jsonSchemaDialect: https://pb33f.io/schema/changed`
siLeft, _ := datamodel.ExtractSpecInfo([]byte(left)) siLeft, _ := datamodel.ExtractSpecInfo([]byte(left))
siRight, _ := datamodel.ExtractSpecInfo([]byte(right)) siRight, _ := datamodel.ExtractSpecInfo([]byte(right))
lDoc, _ := v3.CreateDocument(siLeft) lDoc, _ := v3.CreateDocumentFromConfig(siLeft, datamodel.NewDocumentConfiguration())
rDoc, _ := v3.CreateDocument(siRight) rDoc, _ := v3.CreateDocumentFromConfig(siRight, datamodel.NewDocumentConfiguration())
// compare. // compare.
extChanges := CompareDocuments(lDoc, rDoc) extChanges := CompareDocuments(lDoc, rDoc)
@@ -840,8 +840,8 @@ components:
siLeft, _ := datamodel.ExtractSpecInfo([]byte(left)) siLeft, _ := datamodel.ExtractSpecInfo([]byte(left))
siRight, _ := datamodel.ExtractSpecInfo([]byte(right)) siRight, _ := datamodel.ExtractSpecInfo([]byte(right))
lDoc, _ := v3.CreateDocument(siLeft) lDoc, _ := v3.CreateDocumentFromConfig(siLeft, datamodel.NewDocumentConfiguration())
rDoc, _ := v3.CreateDocument(siRight) rDoc, _ := v3.CreateDocumentFromConfig(siRight, datamodel.NewDocumentConfiguration())
// compare. // compare.
extChanges := CompareDocuments(lDoc, rDoc) extChanges := CompareDocuments(lDoc, rDoc)
@@ -870,8 +870,8 @@ components:
siLeft, _ := datamodel.ExtractSpecInfo([]byte(left)) siLeft, _ := datamodel.ExtractSpecInfo([]byte(left))
siRight, _ := datamodel.ExtractSpecInfo([]byte(right)) siRight, _ := datamodel.ExtractSpecInfo([]byte(right))
lDoc, _ := v3.CreateDocument(siLeft) lDoc, _ := v3.CreateDocumentFromConfig(siLeft, datamodel.NewDocumentConfiguration())
rDoc, _ := v3.CreateDocument(siRight) rDoc, _ := v3.CreateDocumentFromConfig(siRight, datamodel.NewDocumentConfiguration())
// compare. // compare.
extChanges := CompareDocuments(rDoc, lDoc) extChanges := CompareDocuments(rDoc, lDoc)
@@ -910,8 +910,8 @@ paths:
siLeft, _ := datamodel.ExtractSpecInfo([]byte(left)) siLeft, _ := datamodel.ExtractSpecInfo([]byte(left))
siRight, _ := datamodel.ExtractSpecInfo([]byte(right)) siRight, _ := datamodel.ExtractSpecInfo([]byte(right))
lDoc, _ := v3.CreateDocument(siLeft) lDoc, _ := v3.CreateDocumentFromConfig(siLeft, datamodel.NewDocumentConfiguration())
rDoc, _ := v3.CreateDocument(siRight) rDoc, _ := v3.CreateDocumentFromConfig(siRight, datamodel.NewDocumentConfiguration())
// compare. // compare.
extChanges := CompareDocuments(lDoc, rDoc) extChanges := CompareDocuments(lDoc, rDoc)
@@ -949,8 +949,8 @@ security:
siLeft, _ := datamodel.ExtractSpecInfo([]byte(left)) siLeft, _ := datamodel.ExtractSpecInfo([]byte(left))
siRight, _ := datamodel.ExtractSpecInfo([]byte(right)) siRight, _ := datamodel.ExtractSpecInfo([]byte(right))
lDoc, _ := v3.CreateDocument(siLeft) lDoc, _ := v3.CreateDocumentFromConfig(siLeft, datamodel.NewDocumentConfiguration())
rDoc, _ := v3.CreateDocument(siRight) rDoc, _ := v3.CreateDocumentFromConfig(siRight, datamodel.NewDocumentConfiguration())
// compare. // compare.
extChanges := CompareDocuments(lDoc, rDoc) extChanges := CompareDocuments(lDoc, rDoc)
@@ -983,8 +983,8 @@ components:
siLeft, _ := datamodel.ExtractSpecInfo([]byte(left)) siLeft, _ := datamodel.ExtractSpecInfo([]byte(left))
siRight, _ := datamodel.ExtractSpecInfo([]byte(right)) siRight, _ := datamodel.ExtractSpecInfo([]byte(right))
lDoc, _ := v3.CreateDocument(siLeft) lDoc, _ := v3.CreateDocumentFromConfig(siLeft, datamodel.NewDocumentConfiguration())
rDoc, _ := v3.CreateDocument(siRight) rDoc, _ := v3.CreateDocumentFromConfig(siRight, datamodel.NewDocumentConfiguration())
// compare. // compare.
extChanges := CompareDocuments(lDoc, rDoc) extChanges := CompareDocuments(lDoc, rDoc)
@@ -1015,8 +1015,8 @@ servers:
siLeft, _ := datamodel.ExtractSpecInfo([]byte(left)) siLeft, _ := datamodel.ExtractSpecInfo([]byte(left))
siRight, _ := datamodel.ExtractSpecInfo([]byte(right)) siRight, _ := datamodel.ExtractSpecInfo([]byte(right))
lDoc, _ := v3.CreateDocument(siLeft) lDoc, _ := v3.CreateDocumentFromConfig(siLeft, datamodel.NewDocumentConfiguration())
rDoc, _ := v3.CreateDocument(siRight) rDoc, _ := v3.CreateDocumentFromConfig(siRight, datamodel.NewDocumentConfiguration())
// compare. // compare.
extChanges := CompareDocuments(lDoc, rDoc) extChanges := CompareDocuments(lDoc, rDoc)
@@ -1050,8 +1050,8 @@ components:
siLeft, _ := datamodel.ExtractSpecInfo([]byte(left)) siLeft, _ := datamodel.ExtractSpecInfo([]byte(left))
siRight, _ := datamodel.ExtractSpecInfo([]byte(right)) siRight, _ := datamodel.ExtractSpecInfo([]byte(right))
lDoc, _ := v3.CreateDocument(siLeft) lDoc, _ := v3.CreateDocumentFromConfig(siLeft, datamodel.NewDocumentConfiguration())
rDoc, _ := v3.CreateDocument(siRight) rDoc, _ := v3.CreateDocumentFromConfig(siRight, datamodel.NewDocumentConfiguration())
// compare. // compare.
extChanges := CompareDocuments(lDoc, rDoc) extChanges := CompareDocuments(lDoc, rDoc)
@@ -1089,8 +1089,8 @@ webhooks:
siLeft, _ := datamodel.ExtractSpecInfo([]byte(left)) siLeft, _ := datamodel.ExtractSpecInfo([]byte(left))
siRight, _ := datamodel.ExtractSpecInfo([]byte(right)) siRight, _ := datamodel.ExtractSpecInfo([]byte(right))
lDoc, _ := v3.CreateDocument(siLeft) lDoc, _ := v3.CreateDocumentFromConfig(siLeft, datamodel.NewDocumentConfiguration())
rDoc, _ := v3.CreateDocument(siRight) rDoc, _ := v3.CreateDocumentFromConfig(siRight, datamodel.NewDocumentConfiguration())
// compare. // compare.
extChanges := CompareDocuments(lDoc, rDoc) extChanges := CompareDocuments(lDoc, rDoc)
@@ -1133,8 +1133,8 @@ paths:
siLeft, _ := datamodel.ExtractSpecInfo([]byte(left)) siLeft, _ := datamodel.ExtractSpecInfo([]byte(left))
siRight, _ := datamodel.ExtractSpecInfo([]byte(right)) siRight, _ := datamodel.ExtractSpecInfo([]byte(right))
lDoc, _ := v3.CreateDocument(siLeft) lDoc, _ := v3.CreateDocumentFromConfig(siLeft, datamodel.NewDocumentConfiguration())
rDoc, _ := v3.CreateDocument(siRight) rDoc, _ := v3.CreateDocumentFromConfig(siRight, datamodel.NewDocumentConfiguration())
// compare. // compare.
extChanges := CompareDocuments(lDoc, rDoc) extChanges := CompareDocuments(lDoc, rDoc)
@@ -1176,8 +1176,8 @@ paths:
siLeft, _ := datamodel.ExtractSpecInfo([]byte(left)) siLeft, _ := datamodel.ExtractSpecInfo([]byte(left))
siRight, _ := datamodel.ExtractSpecInfo([]byte(right)) siRight, _ := datamodel.ExtractSpecInfo([]byte(right))
lDoc, _ := v3.CreateDocument(siLeft) lDoc, _ := v3.CreateDocumentFromConfig(siLeft, datamodel.NewDocumentConfiguration())
rDoc, _ := v3.CreateDocument(siRight) rDoc, _ := v3.CreateDocumentFromConfig(siRight, datamodel.NewDocumentConfiguration())
// compare. // compare.
extChanges := CompareDocuments(lDoc, rDoc) extChanges := CompareDocuments(lDoc, rDoc)

View File

@@ -5,6 +5,7 @@ package model
import ( import (
"fmt" "fmt"
"github.com/pb33f/libopenapi/utils"
"testing" "testing"
"github.com/pb33f/libopenapi/datamodel" "github.com/pb33f/libopenapi/datamodel"
@@ -171,14 +172,15 @@ func test_BuildDocv2(l, r string) (*v2.Swagger, *v2.Swagger) {
leftInfo, _ := datamodel.ExtractSpecInfo([]byte(l)) leftInfo, _ := datamodel.ExtractSpecInfo([]byte(l))
rightInfo, _ := datamodel.ExtractSpecInfo([]byte(r)) rightInfo, _ := datamodel.ExtractSpecInfo([]byte(r))
var err []error var err error
var leftDoc, rightDoc *v2.Swagger var leftDoc, rightDoc *v2.Swagger
leftDoc, err = v2.CreateDocumentFromConfig(leftInfo, datamodel.NewDocumentConfiguration()) leftDoc, err = v2.CreateDocumentFromConfig(leftInfo, datamodel.NewDocumentConfiguration())
rightDoc, err = v2.CreateDocumentFromConfig(rightInfo, datamodel.NewDocumentConfiguration()) rightDoc, err = v2.CreateDocumentFromConfig(rightInfo, datamodel.NewDocumentConfiguration())
if len(err) > 0 { uErr := utils.UnwrapErrors(err)
for i := range err { if len(uErr) > 0 {
fmt.Printf("error: %v\n", err[i]) for i := range uErr {
fmt.Printf("error: %v\n", uErr[i])
} }
panic("failed to create doc") panic("failed to create doc")
} }

View File

@@ -21,8 +21,8 @@ func TestCompareOpenAPIDocuments(t *testing.T) {
infoOrig, _ := datamodel.ExtractSpecInfo(original) infoOrig, _ := datamodel.ExtractSpecInfo(original)
infoMod, _ := datamodel.ExtractSpecInfo(modified) infoMod, _ := datamodel.ExtractSpecInfo(modified)
origDoc, _ := v3.CreateDocument(infoOrig) origDoc, _ := v3.CreateDocumentFromConfig(infoOrig, datamodel.NewDocumentConfiguration())
modDoc, _ := v3.CreateDocument(infoMod) modDoc, _ := v3.CreateDocumentFromConfig(infoMod, datamodel.NewDocumentConfiguration())
changes := CompareOpenAPIDocuments(origDoc, modDoc) changes := CompareOpenAPIDocuments(origDoc, modDoc)
assert.Equal(t, 75, changes.TotalChanges()) assert.Equal(t, 75, changes.TotalChanges())
@@ -38,8 +38,8 @@ func TestCompareSwaggerDocuments(t *testing.T) {
infoOrig, _ := datamodel.ExtractSpecInfo(original) infoOrig, _ := datamodel.ExtractSpecInfo(original)
infoMod, _ := datamodel.ExtractSpecInfo(modified) infoMod, _ := datamodel.ExtractSpecInfo(modified)
origDoc, _ := v2.CreateDocument(infoOrig) origDoc, _ := v2.CreateDocumentFromConfig(infoOrig, datamodel.NewDocumentConfiguration())
modDoc, _ := v2.CreateDocument(infoMod) modDoc, _ := v2.CreateDocumentFromConfig(infoMod, datamodel.NewDocumentConfiguration())
changes := CompareSwaggerDocuments(origDoc, modDoc) changes := CompareSwaggerDocuments(origDoc, modDoc)
assert.Equal(t, 52, changes.TotalChanges()) assert.Equal(t, 52, changes.TotalChanges())
@@ -57,8 +57,8 @@ func Benchmark_CompareOpenAPIDocuments(b *testing.B) {
infoOrig, _ := datamodel.ExtractSpecInfo(original) infoOrig, _ := datamodel.ExtractSpecInfo(original)
infoMod, _ := datamodel.ExtractSpecInfo(modified) infoMod, _ := datamodel.ExtractSpecInfo(modified)
origDoc, _ := v3.CreateDocument(infoOrig) origDoc, _ := v3.CreateDocumentFromConfig(infoOrig, datamodel.NewDocumentConfiguration())
modDoc, _ := v3.CreateDocument(infoMod) modDoc, _ := v3.CreateDocumentFromConfig(infoMod, datamodel.NewDocumentConfiguration())
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
CompareOpenAPIDocuments(origDoc, modDoc) CompareOpenAPIDocuments(origDoc, modDoc)
@@ -72,8 +72,8 @@ func Benchmark_CompareSwaggerDocuments(b *testing.B) {
infoOrig, _ := datamodel.ExtractSpecInfo(original) infoOrig, _ := datamodel.ExtractSpecInfo(original)
infoMod, _ := datamodel.ExtractSpecInfo(modified) infoMod, _ := datamodel.ExtractSpecInfo(modified)
origDoc, _ := v2.CreateDocument(infoOrig) origDoc, _ := v2.CreateDocumentFromConfig(infoOrig, datamodel.NewDocumentConfiguration())
modDoc, _ := v2.CreateDocument(infoMod) modDoc, _ := v2.CreateDocumentFromConfig(infoMod, datamodel.NewDocumentConfiguration())
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
CompareSwaggerDocuments(origDoc, modDoc) CompareSwaggerDocuments(origDoc, modDoc)
@@ -87,8 +87,8 @@ func Benchmark_CompareOpenAPIDocuments_NoChange(b *testing.B) {
infoOrig, _ := datamodel.ExtractSpecInfo(original) infoOrig, _ := datamodel.ExtractSpecInfo(original)
infoMod, _ := datamodel.ExtractSpecInfo(modified) infoMod, _ := datamodel.ExtractSpecInfo(modified)
origDoc, _ := v3.CreateDocument(infoOrig) origDoc, _ := v3.CreateDocumentFromConfig(infoOrig, datamodel.NewDocumentConfiguration())
modDoc, _ := v3.CreateDocument(infoMod) modDoc, _ := v3.CreateDocumentFromConfig(infoMod, datamodel.NewDocumentConfiguration())
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
CompareOpenAPIDocuments(origDoc, modDoc) CompareOpenAPIDocuments(origDoc, modDoc)
@@ -102,8 +102,8 @@ func Benchmark_CompareK8s(b *testing.B) {
infoOrig, _ := datamodel.ExtractSpecInfo(original) infoOrig, _ := datamodel.ExtractSpecInfo(original)
infoMod, _ := datamodel.ExtractSpecInfo(modified) infoMod, _ := datamodel.ExtractSpecInfo(modified)
origDoc, _ := v2.CreateDocument(infoOrig) origDoc, _ := v2.CreateDocumentFromConfig(infoOrig, datamodel.NewDocumentConfiguration())
modDoc, _ := v2.CreateDocument(infoMod) modDoc, _ := v2.CreateDocumentFromConfig(infoMod, datamodel.NewDocumentConfiguration())
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
CompareSwaggerDocuments(origDoc, modDoc) CompareSwaggerDocuments(origDoc, modDoc)
@@ -117,8 +117,8 @@ func Benchmark_CompareStripe(b *testing.B) {
infoOrig, _ := datamodel.ExtractSpecInfo(original) infoOrig, _ := datamodel.ExtractSpecInfo(original)
infoMod, _ := datamodel.ExtractSpecInfo(modified) infoMod, _ := datamodel.ExtractSpecInfo(modified)
origDoc, _ := v3.CreateDocument(infoOrig) origDoc, _ := v3.CreateDocumentFromConfig(infoOrig, datamodel.NewDocumentConfiguration())
modDoc, _ := v3.CreateDocument(infoMod) modDoc, _ := v3.CreateDocumentFromConfig(infoMod, datamodel.NewDocumentConfiguration())
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
CompareOpenAPIDocuments(origDoc, modDoc) CompareOpenAPIDocuments(origDoc, modDoc)
@@ -138,8 +138,8 @@ func ExampleCompareOpenAPIDocuments() {
infoModified, _ := datamodel.ExtractSpecInfo(modified) infoModified, _ := datamodel.ExtractSpecInfo(modified)
// Build OpenAPI Documents from SpecInfo // Build OpenAPI Documents from SpecInfo
origDocument, _ := v3.CreateDocument(infoOriginal) origDocument, _ := v3.CreateDocumentFromConfig(infoOriginal, datamodel.NewDocumentConfiguration())
modDocDocument, _ := v3.CreateDocument(infoModified) modDocDocument, _ := v3.CreateDocumentFromConfig(infoModified, datamodel.NewDocumentConfiguration())
// Compare OpenAPI Documents and extract to *DocumentChanges // Compare OpenAPI Documents and extract to *DocumentChanges
changes := CompareOpenAPIDocuments(origDocument, modDocDocument) changes := CompareOpenAPIDocuments(origDocument, modDocDocument)