mirror of
https://github.com/LukeHagar/libopenapi.git
synced 2025-12-06 12:37:49 +00:00
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:
@@ -4,6 +4,7 @@
|
||||
package datamodel
|
||||
|
||||
import (
|
||||
"io/fs"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"net/url"
|
||||
@@ -29,10 +30,6 @@ type DocumentConfiguration struct {
|
||||
// Resolves [#132]: https://github.com/pb33f/libopenapi/issues/132
|
||||
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.
|
||||
// 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.
|
||||
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.
|
||||
//
|
||||
// Deprecated: This behavior is now driven by the inclusion of a BasePath. If a BasePath is set, then the
|
||||
|
||||
@@ -302,7 +302,6 @@ func NewSchema(schema *base.Schema) *Schema {
|
||||
s.Anchor = schema.Anchor.Value
|
||||
}
|
||||
|
||||
// TODO: check this behavior.
|
||||
for i := range schema.Enum.Value {
|
||||
enum = append(enum, schema.Enum.Value[i].Value)
|
||||
}
|
||||
|
||||
@@ -18,8 +18,8 @@ var doc *v2.Swagger
|
||||
func initTest() {
|
||||
data, _ := os.ReadFile("../../../test_specs/petstorev2-complete.yaml")
|
||||
info, _ := datamodel.ExtractSpecInfo(data)
|
||||
var err []error
|
||||
doc, err = v2.CreateDocument(info)
|
||||
var err error
|
||||
doc, err = v2.CreateDocumentFromConfig(info, datamodel.NewDocumentConfiguration())
|
||||
if err != nil {
|
||||
panic("broken something")
|
||||
}
|
||||
|
||||
@@ -398,10 +398,10 @@ func TestStripeAsDoc(t *testing.T) {
|
||||
func TestK8sAsDoc(t *testing.T) {
|
||||
data, _ := os.ReadFile("../../../test_specs/k8s.json")
|
||||
info, _ := datamodel.ExtractSpecInfo(data)
|
||||
var err []error
|
||||
var err error
|
||||
lowSwag, err := lowv2.CreateDocumentFromConfig(info, datamodel.NewDocumentConfiguration())
|
||||
d := v2.NewSwaggerDocument(lowSwag)
|
||||
assert.Len(t, err, 0)
|
||||
assert.Len(t, utils.UnwrapErrors(err), 0)
|
||||
assert.NotNil(t, d)
|
||||
}
|
||||
|
||||
|
||||
@@ -74,25 +74,3 @@ func (t *Tag) Hash() [32]byte {
|
||||
f = append(f, keys...)
|
||||
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)
|
||||
//}
|
||||
|
||||
@@ -5,6 +5,7 @@ package v2
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/pb33f/libopenapi/utils"
|
||||
"os"
|
||||
|
||||
"github.com/pb33f/libopenapi/datamodel"
|
||||
@@ -22,12 +23,13 @@ func Example_createLowLevelSwaggerDocument() {
|
||||
info, _ := datamodel.ExtractSpecInfo(petstoreBytes)
|
||||
|
||||
// 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 len(errors) > 0 {
|
||||
for i := range errors {
|
||||
fmt.Printf("error: %s\n", errors[i].Error())
|
||||
errs := utils.UnwrapErrors(err)
|
||||
if len(errs) > 0 {
|
||||
for i := range errs {
|
||||
fmt.Printf("error: %s\n", errs[i].Error())
|
||||
}
|
||||
panic("cannot build document")
|
||||
}
|
||||
@@ -50,12 +52,13 @@ func ExampleCreateDocument() {
|
||||
info, _ := datamodel.ExtractSpecInfo(petstoreBytes)
|
||||
|
||||
// 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 len(errors) > 0 {
|
||||
for i := range errors {
|
||||
fmt.Printf("error: %s\n", errors[i].Error())
|
||||
errs := utils.UnwrapErrors(err)
|
||||
if len(errs) > 0 {
|
||||
for i := range errs {
|
||||
fmt.Printf("error: %s\n", errs[i].Error())
|
||||
}
|
||||
panic("cannot build document")
|
||||
}
|
||||
|
||||
@@ -13,11 +13,14 @@ package v2
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"github.com/pb33f/libopenapi/datamodel"
|
||||
"github.com/pb33f/libopenapi/datamodel/low"
|
||||
"github.com/pb33f/libopenapi/datamodel/low/base"
|
||||
"github.com/pb33f/libopenapi/index"
|
||||
"gopkg.in/yaml.v3"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
// 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.
|
||||
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.
|
||||
@@ -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.
|
||||
func CreateDocumentFromConfig(info *datamodel.SpecInfo,
|
||||
configuration *datamodel.DocumentConfiguration) (*Swagger, []error) {
|
||||
configuration *datamodel.DocumentConfiguration) (*Swagger, error) {
|
||||
return createDocument(info, configuration)
|
||||
}
|
||||
|
||||
// CreateDocument will create a new Swagger document from the provided SpecInfo.
|
||||
//
|
||||
// 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) {
|
||||
func createDocument(info *datamodel.SpecInfo, config *datamodel.DocumentConfiguration) (*Swagger, error) {
|
||||
doc := Swagger{Swagger: low.ValueReference[string]{Value: info.Version, ValueNode: info.RootNode}}
|
||||
doc.Extensions = low.ExtractExtensions(info.RootNode.Content[0])
|
||||
|
||||
// build an index
|
||||
idx := index.NewSpecIndexWithConfig(info.RootNode, &index.SpecIndexConfig{
|
||||
BaseURL: config.BaseURL,
|
||||
RemoteURLHandler: config.RemoteURLHandler,
|
||||
//AllowRemoteLookup: config.AllowRemoteReferences,
|
||||
//AllowFileLookup: config.AllowFileReferences,
|
||||
})
|
||||
doc.Index = idx
|
||||
doc.SpecInfo = info
|
||||
// create an index config and shadow the document configuration.
|
||||
idxConfig := index.CreateClosedAPIIndexConfig()
|
||||
idxConfig.SpecInfo = info
|
||||
idxConfig.IgnoreArrayCircularReferences = config.IgnoreArrayCircularReferences
|
||||
idxConfig.IgnorePolymorphicCircularReferences = config.IgnorePolymorphicCircularReferences
|
||||
idxConfig.AvoidCircularReferenceCheck = true
|
||||
idxConfig.BaseURL = config.BaseURL
|
||||
idxConfig.BasePath = config.BasePath
|
||||
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.
|
||||
_ = low.BuildModel(info.RootNode.Content[0], &doc)
|
||||
@@ -162,23 +233,13 @@ func createDocument(info *datamodel.SpecInfo, config *datamodel.DocumentConfigur
|
||||
ctx := context.Background()
|
||||
|
||||
// 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 {
|
||||
errors = append(errors, err)
|
||||
errs = append(errs, err)
|
||||
}
|
||||
|
||||
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{
|
||||
extractInfo,
|
||||
extractPaths,
|
||||
@@ -192,7 +253,7 @@ func createDocument(info *datamodel.SpecInfo, config *datamodel.DocumentConfigur
|
||||
doneChan := make(chan bool)
|
||||
errChan := make(chan error)
|
||||
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
|
||||
for completedExtractions < len(extractionFuncs) {
|
||||
@@ -201,11 +262,11 @@ func createDocument(info *datamodel.SpecInfo, config *datamodel.DocumentConfigur
|
||||
completedExtractions++
|
||||
case e := <-errChan:
|
||||
completedExtractions++
|
||||
errors = append(errors, e)
|
||||
errs = append(errs, e)
|
||||
}
|
||||
}
|
||||
|
||||
return &doc, errors
|
||||
return &doc, errors.Join(errs...)
|
||||
}
|
||||
|
||||
func (s *Swagger) GetExternalDocs() *low.NodeReference[any] {
|
||||
|
||||
@@ -5,6 +5,7 @@ package v2
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/pb33f/libopenapi/utils"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
@@ -20,11 +21,8 @@ func initTest() {
|
||||
}
|
||||
data, _ := os.ReadFile("../../../test_specs/petstorev2-complete.yaml")
|
||||
info, _ := datamodel.ExtractSpecInfo(data)
|
||||
var err []error
|
||||
doc, err = CreateDocumentFromConfig(info, &datamodel.DocumentConfiguration{
|
||||
AllowFileReferences: false,
|
||||
AllowRemoteReferences: false,
|
||||
})
|
||||
var err error
|
||||
doc, err = CreateDocumentFromConfig(info, datamodel.NewDocumentConfiguration())
|
||||
wait := true
|
||||
for wait {
|
||||
select {
|
||||
@@ -42,10 +40,7 @@ func BenchmarkCreateDocument(b *testing.B) {
|
||||
data, _ := os.ReadFile("../../../test_specs/petstorev2-complete.yaml")
|
||||
info, _ := datamodel.ExtractSpecInfo(data)
|
||||
for i := 0; i < b.N; i++ {
|
||||
doc, _ = CreateDocumentFromConfig(info, &datamodel.DocumentConfiguration{
|
||||
AllowFileReferences: false,
|
||||
AllowRemoteReferences: false,
|
||||
})
|
||||
doc, _ = CreateDocumentFromConfig(info, datamodel.NewDocumentConfiguration())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -183,8 +178,8 @@ func TestCreateDocument_ExternalDocsBad(t *testing.T) {
|
||||
$ref: bork`
|
||||
|
||||
info, _ := datamodel.ExtractSpecInfo([]byte(yml))
|
||||
var err []error
|
||||
doc, err = CreateDocument(info)
|
||||
var err error
|
||||
doc, err = CreateDocumentFromConfig(info, datamodel.NewDocumentConfiguration())
|
||||
wait := true
|
||||
for wait {
|
||||
select {
|
||||
@@ -192,7 +187,7 @@ func TestCreateDocument_ExternalDocsBad(t *testing.T) {
|
||||
wait = false
|
||||
}
|
||||
}
|
||||
assert.Len(t, err, 1)
|
||||
assert.Len(t, utils.UnwrapErrors(err), 2)
|
||||
}
|
||||
|
||||
func TestCreateDocument_TagsBad(t *testing.T) {
|
||||
@@ -201,8 +196,8 @@ func TestCreateDocument_TagsBad(t *testing.T) {
|
||||
$ref: bork`
|
||||
|
||||
info, _ := datamodel.ExtractSpecInfo([]byte(yml))
|
||||
var err []error
|
||||
doc, err = CreateDocument(info)
|
||||
var err error
|
||||
doc, err = CreateDocumentFromConfig(info, datamodel.NewDocumentConfiguration())
|
||||
wait := true
|
||||
for wait {
|
||||
select {
|
||||
@@ -210,7 +205,7 @@ func TestCreateDocument_TagsBad(t *testing.T) {
|
||||
wait = false
|
||||
}
|
||||
}
|
||||
assert.Len(t, err, 1)
|
||||
assert.Len(t, utils.UnwrapErrors(err), 2)
|
||||
}
|
||||
|
||||
func TestCreateDocument_PathsBad(t *testing.T) {
|
||||
@@ -223,8 +218,8 @@ func TestCreateDocument_PathsBad(t *testing.T) {
|
||||
$ref: bork`
|
||||
|
||||
info, _ := datamodel.ExtractSpecInfo([]byte(yml))
|
||||
var err []error
|
||||
doc, err = CreateDocument(info)
|
||||
var err error
|
||||
doc, err = CreateDocumentFromConfig(info, datamodel.NewDocumentConfiguration())
|
||||
wait := true
|
||||
for wait {
|
||||
select {
|
||||
@@ -232,7 +227,7 @@ func TestCreateDocument_PathsBad(t *testing.T) {
|
||||
wait = false
|
||||
}
|
||||
}
|
||||
assert.Len(t, err, 1)
|
||||
assert.Len(t, utils.UnwrapErrors(err), 2)
|
||||
}
|
||||
|
||||
func TestCreateDocument_SecurityBad(t *testing.T) {
|
||||
@@ -241,8 +236,8 @@ func TestCreateDocument_SecurityBad(t *testing.T) {
|
||||
$ref: `
|
||||
|
||||
info, _ := datamodel.ExtractSpecInfo([]byte(yml))
|
||||
var err []error
|
||||
doc, err = CreateDocument(info)
|
||||
var err error
|
||||
doc, err = CreateDocumentFromConfig(info, datamodel.NewDocumentConfiguration())
|
||||
wait := true
|
||||
for wait {
|
||||
select {
|
||||
@@ -250,7 +245,7 @@ func TestCreateDocument_SecurityBad(t *testing.T) {
|
||||
wait = false
|
||||
}
|
||||
}
|
||||
assert.Len(t, err, 1)
|
||||
assert.Len(t, utils.UnwrapErrors(err), 1)
|
||||
}
|
||||
|
||||
func TestCreateDocument_SecurityDefinitionsBad(t *testing.T) {
|
||||
@@ -259,8 +254,8 @@ func TestCreateDocument_SecurityDefinitionsBad(t *testing.T) {
|
||||
$ref: `
|
||||
|
||||
info, _ := datamodel.ExtractSpecInfo([]byte(yml))
|
||||
var err []error
|
||||
doc, err = CreateDocument(info)
|
||||
var err error
|
||||
doc, err = CreateDocumentFromConfig(info, datamodel.NewDocumentConfiguration())
|
||||
wait := true
|
||||
for wait {
|
||||
select {
|
||||
@@ -268,7 +263,7 @@ func TestCreateDocument_SecurityDefinitionsBad(t *testing.T) {
|
||||
wait = false
|
||||
}
|
||||
}
|
||||
assert.Len(t, err, 1)
|
||||
assert.Len(t, utils.UnwrapErrors(err), 1)
|
||||
}
|
||||
|
||||
func TestCreateDocument_ResponsesBad(t *testing.T) {
|
||||
@@ -277,8 +272,8 @@ func TestCreateDocument_ResponsesBad(t *testing.T) {
|
||||
$ref: `
|
||||
|
||||
info, _ := datamodel.ExtractSpecInfo([]byte(yml))
|
||||
var err []error
|
||||
doc, err = CreateDocument(info)
|
||||
var err error
|
||||
doc, err = CreateDocumentFromConfig(info, datamodel.NewDocumentConfiguration())
|
||||
wait := true
|
||||
for wait {
|
||||
select {
|
||||
@@ -286,7 +281,7 @@ func TestCreateDocument_ResponsesBad(t *testing.T) {
|
||||
wait = false
|
||||
}
|
||||
}
|
||||
assert.Len(t, err, 1)
|
||||
assert.Len(t, utils.UnwrapErrors(err), 1)
|
||||
}
|
||||
|
||||
func TestCreateDocument_ParametersBad(t *testing.T) {
|
||||
@@ -295,8 +290,8 @@ func TestCreateDocument_ParametersBad(t *testing.T) {
|
||||
$ref: `
|
||||
|
||||
info, _ := datamodel.ExtractSpecInfo([]byte(yml))
|
||||
var err []error
|
||||
doc, err = CreateDocument(info)
|
||||
var err error
|
||||
doc, err = CreateDocumentFromConfig(info, datamodel.NewDocumentConfiguration())
|
||||
wait := true
|
||||
for wait {
|
||||
select {
|
||||
@@ -304,7 +299,7 @@ func TestCreateDocument_ParametersBad(t *testing.T) {
|
||||
wait = false
|
||||
}
|
||||
}
|
||||
assert.Len(t, err, 1)
|
||||
assert.Len(t, utils.UnwrapErrors(err), 1)
|
||||
}
|
||||
|
||||
func TestCreateDocument_DefinitionsBad(t *testing.T) {
|
||||
@@ -313,8 +308,8 @@ func TestCreateDocument_DefinitionsBad(t *testing.T) {
|
||||
$ref: `
|
||||
|
||||
info, _ := datamodel.ExtractSpecInfo([]byte(yml))
|
||||
var err []error
|
||||
doc, err = CreateDocument(info)
|
||||
var err error
|
||||
doc, err = CreateDocumentFromConfig(info, datamodel.NewDocumentConfiguration())
|
||||
wait := true
|
||||
for wait {
|
||||
select {
|
||||
@@ -322,7 +317,7 @@ func TestCreateDocument_DefinitionsBad(t *testing.T) {
|
||||
wait = false
|
||||
}
|
||||
}
|
||||
assert.Len(t, err, 1)
|
||||
assert.Len(t, utils.UnwrapErrors(err), 1)
|
||||
}
|
||||
|
||||
func TestCreateDocument_InfoBad(t *testing.T) {
|
||||
@@ -331,8 +326,8 @@ func TestCreateDocument_InfoBad(t *testing.T) {
|
||||
$ref: `
|
||||
|
||||
info, _ := datamodel.ExtractSpecInfo([]byte(yml))
|
||||
var err []error
|
||||
doc, err = CreateDocument(info)
|
||||
var err error
|
||||
doc, err = CreateDocumentFromConfig(info, datamodel.NewDocumentConfiguration())
|
||||
wait := true
|
||||
for wait {
|
||||
select {
|
||||
@@ -340,15 +335,15 @@ func TestCreateDocument_InfoBad(t *testing.T) {
|
||||
wait = false
|
||||
}
|
||||
}
|
||||
assert.Len(t, err, 1)
|
||||
assert.Len(t, utils.UnwrapErrors(err), 1)
|
||||
}
|
||||
|
||||
func TestCircularReferenceError(t *testing.T) {
|
||||
|
||||
data, _ := os.ReadFile("../../../test_specs/swagger-circular-tests.yaml")
|
||||
info, _ := datamodel.ExtractSpecInfo(data)
|
||||
circDoc, err := CreateDocument(info)
|
||||
circDoc, err := CreateDocumentFromConfig(info, datamodel.NewDocumentConfiguration())
|
||||
assert.NotNil(t, circDoc)
|
||||
assert.Len(t, err, 3)
|
||||
assert.Len(t, utils.UnwrapErrors(err), 3)
|
||||
|
||||
}
|
||||
|
||||
@@ -57,86 +57,74 @@ func createDocument(info *datamodel.SpecInfo, config *datamodel.DocumentConfigur
|
||||
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,
|
||||
// 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)
|
||||
}
|
||||
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 {
|
||||
|
||||
// create a remote filesystem
|
||||
remoteFS, fsErr := index.NewRemoteFSWithConfig(idxConfig)
|
||||
if fsErr != nil {
|
||||
return nil, fsErr
|
||||
}
|
||||
if config.RemoteURLHandler != nil {
|
||||
remoteFS.RemoteHandlerFunc = config.RemoteURLHandler
|
||||
}
|
||||
// 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)
|
||||
|
||||
idxConfig.AllowRemoteLookup = true
|
||||
} 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)
|
||||
// add to the rolodex
|
||||
rolodex.AddRemoteFS(config.BaseURL.String(), remoteFS)
|
||||
}
|
||||
}
|
||||
|
||||
// index the rolodex
|
||||
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 root index.
|
||||
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
|
||||
|
||||
doc.Extensions = low.ExtractExtensions(info.RootNode.Content[0])
|
||||
|
||||
12
document.go
12
document.go
@@ -259,13 +259,15 @@ func (d *document) BuildV2Model() (*DocumentModel[v2high.Swagger], []error) {
|
||||
|
||||
var lowDoc *v2low.Swagger
|
||||
if d.config == nil {
|
||||
d.config = &datamodel.DocumentConfiguration{
|
||||
AllowFileReferences: false,
|
||||
AllowRemoteReferences: false,
|
||||
}
|
||||
d.config = datamodel.NewDocumentConfiguration()
|
||||
}
|
||||
|
||||
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
|
||||
// has the option of ignoring them.
|
||||
|
||||
@@ -50,7 +50,7 @@ definitions:
|
||||
assert.NoError(t, err)
|
||||
|
||||
v2Doc, docErr := doc.BuildV2Model()
|
||||
assert.Len(t, docErr, 2)
|
||||
assert.Len(t, docErr, 3)
|
||||
assert.Nil(t, v2Doc)
|
||||
}
|
||||
|
||||
@@ -515,7 +515,7 @@ func TestDocument_BuildModel_CompareDocsV2_Error(t *testing.T) {
|
||||
originalDoc, _ := NewDocument(burgerShopOriginal)
|
||||
updatedDoc, _ := NewDocument(burgerShopUpdated)
|
||||
changes, errors := CompareDocuments(updatedDoc, originalDoc)
|
||||
assert.Len(t, errors, 2)
|
||||
assert.Len(t, errors, 14)
|
||||
assert.Nil(t, changes)
|
||||
|
||||
}
|
||||
|
||||
@@ -79,8 +79,8 @@ produces:
|
||||
siLeft, _ := datamodel.ExtractSpecInfo([]byte(left))
|
||||
siRight, _ := datamodel.ExtractSpecInfo([]byte(right))
|
||||
|
||||
lDoc, _ := v2.CreateDocument(siLeft)
|
||||
rDoc, _ := v2.CreateDocument(siRight)
|
||||
lDoc, _ := v2.CreateDocumentFromConfig(siLeft, datamodel.NewDocumentConfiguration())
|
||||
rDoc, _ := v2.CreateDocumentFromConfig(siRight, datamodel.NewDocumentConfiguration())
|
||||
|
||||
// compare.
|
||||
extChanges := CompareDocuments(lDoc, rDoc)
|
||||
@@ -108,8 +108,8 @@ produces:
|
||||
siLeft, _ := datamodel.ExtractSpecInfo([]byte(left))
|
||||
siRight, _ := datamodel.ExtractSpecInfo([]byte(right))
|
||||
|
||||
lDoc, _ := v2.CreateDocument(siLeft)
|
||||
rDoc, _ := v2.CreateDocument(siRight)
|
||||
lDoc, _ := v2.CreateDocumentFromConfig(siLeft, datamodel.NewDocumentConfiguration())
|
||||
rDoc, _ := v2.CreateDocumentFromConfig(siRight, datamodel.NewDocumentConfiguration())
|
||||
|
||||
// compare.
|
||||
extChanges := CompareDocuments(lDoc, rDoc)
|
||||
@@ -137,8 +137,8 @@ basePath: /api`
|
||||
siLeft, _ := datamodel.ExtractSpecInfo([]byte(left))
|
||||
siRight, _ := datamodel.ExtractSpecInfo([]byte(right))
|
||||
|
||||
lDoc, _ := v2.CreateDocument(siLeft)
|
||||
rDoc, _ := v2.CreateDocument(siRight)
|
||||
lDoc, _ := v2.CreateDocumentFromConfig(siLeft, datamodel.NewDocumentConfiguration())
|
||||
rDoc, _ := v2.CreateDocumentFromConfig(siRight, datamodel.NewDocumentConfiguration())
|
||||
|
||||
// compare.
|
||||
extChanges := CompareDocuments(lDoc, rDoc)
|
||||
@@ -168,8 +168,8 @@ info:
|
||||
siLeft, _ := datamodel.ExtractSpecInfo([]byte(left))
|
||||
siRight, _ := datamodel.ExtractSpecInfo([]byte(right))
|
||||
|
||||
lDoc, _ := v2.CreateDocument(siLeft)
|
||||
rDoc, _ := v2.CreateDocument(siRight)
|
||||
lDoc, _ := v2.CreateDocumentFromConfig(siLeft, datamodel.NewDocumentConfiguration())
|
||||
rDoc, _ := v2.CreateDocumentFromConfig(siRight, datamodel.NewDocumentConfiguration())
|
||||
|
||||
// compare.
|
||||
extChanges := CompareDocuments(lDoc, rDoc)
|
||||
@@ -194,8 +194,8 @@ info:
|
||||
siLeft, _ := datamodel.ExtractSpecInfo([]byte(left))
|
||||
siRight, _ := datamodel.ExtractSpecInfo([]byte(right))
|
||||
|
||||
lDoc, _ := v2.CreateDocument(siLeft)
|
||||
rDoc, _ := v2.CreateDocument(siRight)
|
||||
lDoc, _ := v2.CreateDocumentFromConfig(siLeft, datamodel.NewDocumentConfiguration())
|
||||
rDoc, _ := v2.CreateDocumentFromConfig(siRight, datamodel.NewDocumentConfiguration())
|
||||
|
||||
// compare.
|
||||
extChanges := CompareDocuments(lDoc, rDoc)
|
||||
@@ -221,8 +221,8 @@ info:
|
||||
siLeft, _ := datamodel.ExtractSpecInfo([]byte(left))
|
||||
siRight, _ := datamodel.ExtractSpecInfo([]byte(right))
|
||||
|
||||
lDoc, _ := v2.CreateDocument(siLeft)
|
||||
rDoc, _ := v2.CreateDocument(siRight)
|
||||
lDoc, _ := v2.CreateDocumentFromConfig(siLeft, datamodel.NewDocumentConfiguration())
|
||||
rDoc, _ := v2.CreateDocumentFromConfig(siRight, datamodel.NewDocumentConfiguration())
|
||||
|
||||
// compare.
|
||||
extChanges := CompareDocuments(rDoc, lDoc)
|
||||
@@ -248,8 +248,8 @@ externalDocs:
|
||||
siLeft, _ := datamodel.ExtractSpecInfo([]byte(left))
|
||||
siRight, _ := datamodel.ExtractSpecInfo([]byte(right))
|
||||
|
||||
lDoc, _ := v2.CreateDocument(siLeft)
|
||||
rDoc, _ := v2.CreateDocument(siRight)
|
||||
lDoc, _ := v2.CreateDocumentFromConfig(siLeft, datamodel.NewDocumentConfiguration())
|
||||
rDoc, _ := v2.CreateDocumentFromConfig(siRight, datamodel.NewDocumentConfiguration())
|
||||
|
||||
// compare.
|
||||
extChanges := CompareDocuments(lDoc, rDoc)
|
||||
@@ -271,8 +271,8 @@ externalDocs:
|
||||
siLeft, _ := datamodel.ExtractSpecInfo([]byte(left))
|
||||
siRight, _ := datamodel.ExtractSpecInfo([]byte(right))
|
||||
|
||||
lDoc, _ := v2.CreateDocument(siLeft)
|
||||
rDoc, _ := v2.CreateDocument(siRight)
|
||||
lDoc, _ := v2.CreateDocumentFromConfig(siLeft, datamodel.NewDocumentConfiguration())
|
||||
rDoc, _ := v2.CreateDocumentFromConfig(siRight, datamodel.NewDocumentConfiguration())
|
||||
|
||||
// compare.
|
||||
extChanges := CompareDocuments(lDoc, rDoc)
|
||||
@@ -296,8 +296,8 @@ externalDocs:
|
||||
siLeft, _ := datamodel.ExtractSpecInfo([]byte(left))
|
||||
siRight, _ := datamodel.ExtractSpecInfo([]byte(right))
|
||||
|
||||
lDoc, _ := v2.CreateDocument(siLeft)
|
||||
rDoc, _ := v2.CreateDocument(siRight)
|
||||
lDoc, _ := v2.CreateDocumentFromConfig(siLeft, datamodel.NewDocumentConfiguration())
|
||||
rDoc, _ := v2.CreateDocumentFromConfig(siRight, datamodel.NewDocumentConfiguration())
|
||||
|
||||
// compare.
|
||||
extChanges := CompareDocuments(rDoc, lDoc)
|
||||
@@ -335,8 +335,8 @@ security:
|
||||
siLeft, _ := datamodel.ExtractSpecInfo([]byte(left))
|
||||
siRight, _ := datamodel.ExtractSpecInfo([]byte(right))
|
||||
|
||||
lDoc, _ := v2.CreateDocument(siLeft)
|
||||
rDoc, _ := v2.CreateDocument(siRight)
|
||||
lDoc, _ := v2.CreateDocumentFromConfig(siLeft, datamodel.NewDocumentConfiguration())
|
||||
rDoc, _ := v2.CreateDocumentFromConfig(siRight, datamodel.NewDocumentConfiguration())
|
||||
|
||||
// compare.
|
||||
extChanges := CompareDocuments(lDoc, rDoc)
|
||||
@@ -370,8 +370,8 @@ security:
|
||||
siLeft, _ := datamodel.ExtractSpecInfo([]byte(left))
|
||||
siRight, _ := datamodel.ExtractSpecInfo([]byte(right))
|
||||
|
||||
lDoc, _ := v2.CreateDocument(siLeft)
|
||||
rDoc, _ := v2.CreateDocument(siRight)
|
||||
lDoc, _ := v2.CreateDocumentFromConfig(siLeft, datamodel.NewDocumentConfiguration())
|
||||
rDoc, _ := v2.CreateDocumentFromConfig(siRight, datamodel.NewDocumentConfiguration())
|
||||
|
||||
// compare.
|
||||
extChanges := CompareDocuments(lDoc, rDoc)
|
||||
@@ -403,8 +403,8 @@ definitions:
|
||||
siLeft, _ := datamodel.ExtractSpecInfo([]byte(left))
|
||||
siRight, _ := datamodel.ExtractSpecInfo([]byte(right))
|
||||
|
||||
lDoc, _ := v2.CreateDocument(siLeft)
|
||||
rDoc, _ := v2.CreateDocument(siRight)
|
||||
lDoc, _ := v2.CreateDocumentFromConfig(siLeft, datamodel.NewDocumentConfiguration())
|
||||
rDoc, _ := v2.CreateDocumentFromConfig(siRight, datamodel.NewDocumentConfiguration())
|
||||
|
||||
// compare.
|
||||
extChanges := CompareDocuments(lDoc, rDoc)
|
||||
@@ -436,8 +436,8 @@ securityDefinitions:
|
||||
siLeft, _ := datamodel.ExtractSpecInfo([]byte(left))
|
||||
siRight, _ := datamodel.ExtractSpecInfo([]byte(right))
|
||||
|
||||
lDoc, _ := v2.CreateDocument(siLeft)
|
||||
rDoc, _ := v2.CreateDocument(siRight)
|
||||
lDoc, _ := v2.CreateDocumentFromConfig(siLeft, datamodel.NewDocumentConfiguration())
|
||||
rDoc, _ := v2.CreateDocumentFromConfig(siRight, datamodel.NewDocumentConfiguration())
|
||||
|
||||
// compare.
|
||||
extChanges := CompareDocuments(lDoc, rDoc)
|
||||
@@ -464,8 +464,8 @@ securityDefinitions:
|
||||
siLeft, _ := datamodel.ExtractSpecInfo([]byte(left))
|
||||
siRight, _ := datamodel.ExtractSpecInfo([]byte(right))
|
||||
|
||||
lDoc, _ := v2.CreateDocument(siLeft)
|
||||
rDoc, _ := v2.CreateDocument(siRight)
|
||||
lDoc, _ := v2.CreateDocumentFromConfig(siLeft, datamodel.NewDocumentConfiguration())
|
||||
rDoc, _ := v2.CreateDocumentFromConfig(siRight, datamodel.NewDocumentConfiguration())
|
||||
|
||||
// compare.
|
||||
extChanges := CompareDocuments(lDoc, rDoc)
|
||||
@@ -501,8 +501,8 @@ parameters:
|
||||
siLeft, _ := datamodel.ExtractSpecInfo([]byte(left))
|
||||
siRight, _ := datamodel.ExtractSpecInfo([]byte(right))
|
||||
|
||||
lDoc, _ := v2.CreateDocument(siLeft)
|
||||
rDoc, _ := v2.CreateDocument(siRight)
|
||||
lDoc, _ := v2.CreateDocumentFromConfig(siLeft, datamodel.NewDocumentConfiguration())
|
||||
rDoc, _ := v2.CreateDocumentFromConfig(siRight, datamodel.NewDocumentConfiguration())
|
||||
|
||||
// compare.
|
||||
extChanges := CompareDocuments(lDoc, rDoc)
|
||||
@@ -533,8 +533,8 @@ parameters:
|
||||
siLeft, _ := datamodel.ExtractSpecInfo([]byte(left))
|
||||
siRight, _ := datamodel.ExtractSpecInfo([]byte(right))
|
||||
|
||||
lDoc, _ := v2.CreateDocument(siLeft)
|
||||
rDoc, _ := v2.CreateDocument(siRight)
|
||||
lDoc, _ := v2.CreateDocumentFromConfig(siLeft, datamodel.NewDocumentConfiguration())
|
||||
rDoc, _ := v2.CreateDocumentFromConfig(siRight, datamodel.NewDocumentConfiguration())
|
||||
|
||||
// compare.
|
||||
extChanges := CompareDocuments(lDoc, rDoc)
|
||||
@@ -570,8 +570,8 @@ responses:
|
||||
siLeft, _ := datamodel.ExtractSpecInfo([]byte(left))
|
||||
siRight, _ := datamodel.ExtractSpecInfo([]byte(right))
|
||||
|
||||
lDoc, _ := v2.CreateDocument(siLeft)
|
||||
rDoc, _ := v2.CreateDocument(siRight)
|
||||
lDoc, _ := v2.CreateDocumentFromConfig(siLeft, datamodel.NewDocumentConfiguration())
|
||||
rDoc, _ := v2.CreateDocumentFromConfig(siRight, datamodel.NewDocumentConfiguration())
|
||||
|
||||
// compare.
|
||||
extChanges := CompareDocuments(lDoc, rDoc)
|
||||
@@ -601,8 +601,8 @@ responses:
|
||||
siLeft, _ := datamodel.ExtractSpecInfo([]byte(left))
|
||||
siRight, _ := datamodel.ExtractSpecInfo([]byte(right))
|
||||
|
||||
lDoc, _ := v2.CreateDocument(siLeft)
|
||||
rDoc, _ := v2.CreateDocument(siRight)
|
||||
lDoc, _ := v2.CreateDocumentFromConfig(siLeft, datamodel.NewDocumentConfiguration())
|
||||
rDoc, _ := v2.CreateDocumentFromConfig(siRight, datamodel.NewDocumentConfiguration())
|
||||
|
||||
// compare.
|
||||
extChanges := CompareDocuments(lDoc, rDoc)
|
||||
@@ -634,8 +634,8 @@ paths:
|
||||
siLeft, _ := datamodel.ExtractSpecInfo([]byte(left))
|
||||
siRight, _ := datamodel.ExtractSpecInfo([]byte(right))
|
||||
|
||||
lDoc, _ := v2.CreateDocument(siLeft)
|
||||
rDoc, _ := v2.CreateDocument(siRight)
|
||||
lDoc, _ := v2.CreateDocumentFromConfig(siLeft, datamodel.NewDocumentConfiguration())
|
||||
rDoc, _ := v2.CreateDocumentFromConfig(siRight, datamodel.NewDocumentConfiguration())
|
||||
|
||||
// compare.
|
||||
extChanges := CompareDocuments(lDoc, rDoc)
|
||||
@@ -665,8 +665,8 @@ paths:
|
||||
siLeft, _ := datamodel.ExtractSpecInfo([]byte(left))
|
||||
siRight, _ := datamodel.ExtractSpecInfo([]byte(right))
|
||||
|
||||
lDoc, _ := v2.CreateDocument(siLeft)
|
||||
rDoc, _ := v2.CreateDocument(siRight)
|
||||
lDoc, _ := v2.CreateDocumentFromConfig(siLeft, datamodel.NewDocumentConfiguration())
|
||||
rDoc, _ := v2.CreateDocumentFromConfig(siRight, datamodel.NewDocumentConfiguration())
|
||||
|
||||
// compare.
|
||||
extChanges := CompareDocuments(lDoc, rDoc)
|
||||
@@ -698,8 +698,8 @@ paths:
|
||||
siLeft, _ := datamodel.ExtractSpecInfo([]byte(left))
|
||||
siRight, _ := datamodel.ExtractSpecInfo([]byte(right))
|
||||
|
||||
lDoc, _ := v2.CreateDocument(siLeft)
|
||||
rDoc, _ := v2.CreateDocument(siRight)
|
||||
lDoc, _ := v2.CreateDocumentFromConfig(siLeft, datamodel.NewDocumentConfiguration())
|
||||
rDoc, _ := v2.CreateDocumentFromConfig(siRight, datamodel.NewDocumentConfiguration())
|
||||
|
||||
// compare.
|
||||
extChanges := CompareDocuments(lDoc, rDoc)
|
||||
@@ -728,8 +728,8 @@ tags:
|
||||
siLeft, _ := datamodel.ExtractSpecInfo([]byte(left))
|
||||
siRight, _ := datamodel.ExtractSpecInfo([]byte(right))
|
||||
|
||||
lDoc, _ := v2.CreateDocument(siLeft)
|
||||
rDoc, _ := v2.CreateDocument(siRight)
|
||||
lDoc, _ := v2.CreateDocumentFromConfig(siLeft, datamodel.NewDocumentConfiguration())
|
||||
rDoc, _ := v2.CreateDocumentFromConfig(siRight, datamodel.NewDocumentConfiguration())
|
||||
|
||||
// compare.
|
||||
extChanges := CompareDocuments(lDoc, rDoc)
|
||||
@@ -758,8 +758,8 @@ tags:
|
||||
siLeft, _ := datamodel.ExtractSpecInfo([]byte(left))
|
||||
siRight, _ := datamodel.ExtractSpecInfo([]byte(right))
|
||||
|
||||
lDoc, _ := v2.CreateDocument(siLeft)
|
||||
rDoc, _ := v2.CreateDocument(siRight)
|
||||
lDoc, _ := v2.CreateDocumentFromConfig(siLeft, datamodel.NewDocumentConfiguration())
|
||||
rDoc, _ := v2.CreateDocumentFromConfig(siRight, datamodel.NewDocumentConfiguration())
|
||||
|
||||
// compare.
|
||||
extChanges := CompareDocuments(lDoc, rDoc)
|
||||
@@ -783,8 +783,8 @@ jsonSchemaDialect: https://pb33f.io/schema`
|
||||
siLeft, _ := datamodel.ExtractSpecInfo([]byte(left))
|
||||
siRight, _ := datamodel.ExtractSpecInfo([]byte(right))
|
||||
|
||||
lDoc, _ := v3.CreateDocument(siLeft)
|
||||
rDoc, _ := v3.CreateDocument(siRight)
|
||||
lDoc, _ := v3.CreateDocumentFromConfig(siLeft, datamodel.NewDocumentConfiguration())
|
||||
rDoc, _ := v3.CreateDocumentFromConfig(siRight, datamodel.NewDocumentConfiguration())
|
||||
|
||||
// compare.
|
||||
extChanges := CompareDocuments(&lDoc, &rDoc)
|
||||
@@ -811,8 +811,8 @@ jsonSchemaDialect: https://pb33f.io/schema/changed`
|
||||
siLeft, _ := datamodel.ExtractSpecInfo([]byte(left))
|
||||
siRight, _ := datamodel.ExtractSpecInfo([]byte(right))
|
||||
|
||||
lDoc, _ := v3.CreateDocument(siLeft)
|
||||
rDoc, _ := v3.CreateDocument(siRight)
|
||||
lDoc, _ := v3.CreateDocumentFromConfig(siLeft, datamodel.NewDocumentConfiguration())
|
||||
rDoc, _ := v3.CreateDocumentFromConfig(siRight, datamodel.NewDocumentConfiguration())
|
||||
|
||||
// compare.
|
||||
extChanges := CompareDocuments(lDoc, rDoc)
|
||||
@@ -840,8 +840,8 @@ components:
|
||||
siLeft, _ := datamodel.ExtractSpecInfo([]byte(left))
|
||||
siRight, _ := datamodel.ExtractSpecInfo([]byte(right))
|
||||
|
||||
lDoc, _ := v3.CreateDocument(siLeft)
|
||||
rDoc, _ := v3.CreateDocument(siRight)
|
||||
lDoc, _ := v3.CreateDocumentFromConfig(siLeft, datamodel.NewDocumentConfiguration())
|
||||
rDoc, _ := v3.CreateDocumentFromConfig(siRight, datamodel.NewDocumentConfiguration())
|
||||
|
||||
// compare.
|
||||
extChanges := CompareDocuments(lDoc, rDoc)
|
||||
@@ -870,8 +870,8 @@ components:
|
||||
siLeft, _ := datamodel.ExtractSpecInfo([]byte(left))
|
||||
siRight, _ := datamodel.ExtractSpecInfo([]byte(right))
|
||||
|
||||
lDoc, _ := v3.CreateDocument(siLeft)
|
||||
rDoc, _ := v3.CreateDocument(siRight)
|
||||
lDoc, _ := v3.CreateDocumentFromConfig(siLeft, datamodel.NewDocumentConfiguration())
|
||||
rDoc, _ := v3.CreateDocumentFromConfig(siRight, datamodel.NewDocumentConfiguration())
|
||||
|
||||
// compare.
|
||||
extChanges := CompareDocuments(rDoc, lDoc)
|
||||
@@ -910,8 +910,8 @@ paths:
|
||||
siLeft, _ := datamodel.ExtractSpecInfo([]byte(left))
|
||||
siRight, _ := datamodel.ExtractSpecInfo([]byte(right))
|
||||
|
||||
lDoc, _ := v3.CreateDocument(siLeft)
|
||||
rDoc, _ := v3.CreateDocument(siRight)
|
||||
lDoc, _ := v3.CreateDocumentFromConfig(siLeft, datamodel.NewDocumentConfiguration())
|
||||
rDoc, _ := v3.CreateDocumentFromConfig(siRight, datamodel.NewDocumentConfiguration())
|
||||
|
||||
// compare.
|
||||
extChanges := CompareDocuments(lDoc, rDoc)
|
||||
@@ -949,8 +949,8 @@ security:
|
||||
siLeft, _ := datamodel.ExtractSpecInfo([]byte(left))
|
||||
siRight, _ := datamodel.ExtractSpecInfo([]byte(right))
|
||||
|
||||
lDoc, _ := v3.CreateDocument(siLeft)
|
||||
rDoc, _ := v3.CreateDocument(siRight)
|
||||
lDoc, _ := v3.CreateDocumentFromConfig(siLeft, datamodel.NewDocumentConfiguration())
|
||||
rDoc, _ := v3.CreateDocumentFromConfig(siRight, datamodel.NewDocumentConfiguration())
|
||||
|
||||
// compare.
|
||||
extChanges := CompareDocuments(lDoc, rDoc)
|
||||
@@ -983,8 +983,8 @@ components:
|
||||
siLeft, _ := datamodel.ExtractSpecInfo([]byte(left))
|
||||
siRight, _ := datamodel.ExtractSpecInfo([]byte(right))
|
||||
|
||||
lDoc, _ := v3.CreateDocument(siLeft)
|
||||
rDoc, _ := v3.CreateDocument(siRight)
|
||||
lDoc, _ := v3.CreateDocumentFromConfig(siLeft, datamodel.NewDocumentConfiguration())
|
||||
rDoc, _ := v3.CreateDocumentFromConfig(siRight, datamodel.NewDocumentConfiguration())
|
||||
|
||||
// compare.
|
||||
extChanges := CompareDocuments(lDoc, rDoc)
|
||||
@@ -1015,8 +1015,8 @@ servers:
|
||||
siLeft, _ := datamodel.ExtractSpecInfo([]byte(left))
|
||||
siRight, _ := datamodel.ExtractSpecInfo([]byte(right))
|
||||
|
||||
lDoc, _ := v3.CreateDocument(siLeft)
|
||||
rDoc, _ := v3.CreateDocument(siRight)
|
||||
lDoc, _ := v3.CreateDocumentFromConfig(siLeft, datamodel.NewDocumentConfiguration())
|
||||
rDoc, _ := v3.CreateDocumentFromConfig(siRight, datamodel.NewDocumentConfiguration())
|
||||
|
||||
// compare.
|
||||
extChanges := CompareDocuments(lDoc, rDoc)
|
||||
@@ -1050,8 +1050,8 @@ components:
|
||||
siLeft, _ := datamodel.ExtractSpecInfo([]byte(left))
|
||||
siRight, _ := datamodel.ExtractSpecInfo([]byte(right))
|
||||
|
||||
lDoc, _ := v3.CreateDocument(siLeft)
|
||||
rDoc, _ := v3.CreateDocument(siRight)
|
||||
lDoc, _ := v3.CreateDocumentFromConfig(siLeft, datamodel.NewDocumentConfiguration())
|
||||
rDoc, _ := v3.CreateDocumentFromConfig(siRight, datamodel.NewDocumentConfiguration())
|
||||
|
||||
// compare.
|
||||
extChanges := CompareDocuments(lDoc, rDoc)
|
||||
@@ -1089,8 +1089,8 @@ webhooks:
|
||||
siLeft, _ := datamodel.ExtractSpecInfo([]byte(left))
|
||||
siRight, _ := datamodel.ExtractSpecInfo([]byte(right))
|
||||
|
||||
lDoc, _ := v3.CreateDocument(siLeft)
|
||||
rDoc, _ := v3.CreateDocument(siRight)
|
||||
lDoc, _ := v3.CreateDocumentFromConfig(siLeft, datamodel.NewDocumentConfiguration())
|
||||
rDoc, _ := v3.CreateDocumentFromConfig(siRight, datamodel.NewDocumentConfiguration())
|
||||
|
||||
// compare.
|
||||
extChanges := CompareDocuments(lDoc, rDoc)
|
||||
@@ -1133,8 +1133,8 @@ paths:
|
||||
siLeft, _ := datamodel.ExtractSpecInfo([]byte(left))
|
||||
siRight, _ := datamodel.ExtractSpecInfo([]byte(right))
|
||||
|
||||
lDoc, _ := v3.CreateDocument(siLeft)
|
||||
rDoc, _ := v3.CreateDocument(siRight)
|
||||
lDoc, _ := v3.CreateDocumentFromConfig(siLeft, datamodel.NewDocumentConfiguration())
|
||||
rDoc, _ := v3.CreateDocumentFromConfig(siRight, datamodel.NewDocumentConfiguration())
|
||||
|
||||
// compare.
|
||||
extChanges := CompareDocuments(lDoc, rDoc)
|
||||
@@ -1176,8 +1176,8 @@ paths:
|
||||
siLeft, _ := datamodel.ExtractSpecInfo([]byte(left))
|
||||
siRight, _ := datamodel.ExtractSpecInfo([]byte(right))
|
||||
|
||||
lDoc, _ := v3.CreateDocument(siLeft)
|
||||
rDoc, _ := v3.CreateDocument(siRight)
|
||||
lDoc, _ := v3.CreateDocumentFromConfig(siLeft, datamodel.NewDocumentConfiguration())
|
||||
rDoc, _ := v3.CreateDocumentFromConfig(siRight, datamodel.NewDocumentConfiguration())
|
||||
|
||||
// compare.
|
||||
extChanges := CompareDocuments(lDoc, rDoc)
|
||||
|
||||
@@ -5,6 +5,7 @@ package model
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/pb33f/libopenapi/utils"
|
||||
"testing"
|
||||
|
||||
"github.com/pb33f/libopenapi/datamodel"
|
||||
@@ -171,14 +172,15 @@ func test_BuildDocv2(l, r string) (*v2.Swagger, *v2.Swagger) {
|
||||
leftInfo, _ := datamodel.ExtractSpecInfo([]byte(l))
|
||||
rightInfo, _ := datamodel.ExtractSpecInfo([]byte(r))
|
||||
|
||||
var err []error
|
||||
var err error
|
||||
var leftDoc, rightDoc *v2.Swagger
|
||||
leftDoc, err = v2.CreateDocumentFromConfig(leftInfo, datamodel.NewDocumentConfiguration())
|
||||
rightDoc, err = v2.CreateDocumentFromConfig(rightInfo, datamodel.NewDocumentConfiguration())
|
||||
|
||||
if len(err) > 0 {
|
||||
for i := range err {
|
||||
fmt.Printf("error: %v\n", err[i])
|
||||
uErr := utils.UnwrapErrors(err)
|
||||
if len(uErr) > 0 {
|
||||
for i := range uErr {
|
||||
fmt.Printf("error: %v\n", uErr[i])
|
||||
}
|
||||
panic("failed to create doc")
|
||||
}
|
||||
|
||||
@@ -21,8 +21,8 @@ func TestCompareOpenAPIDocuments(t *testing.T) {
|
||||
infoOrig, _ := datamodel.ExtractSpecInfo(original)
|
||||
infoMod, _ := datamodel.ExtractSpecInfo(modified)
|
||||
|
||||
origDoc, _ := v3.CreateDocument(infoOrig)
|
||||
modDoc, _ := v3.CreateDocument(infoMod)
|
||||
origDoc, _ := v3.CreateDocumentFromConfig(infoOrig, datamodel.NewDocumentConfiguration())
|
||||
modDoc, _ := v3.CreateDocumentFromConfig(infoMod, datamodel.NewDocumentConfiguration())
|
||||
|
||||
changes := CompareOpenAPIDocuments(origDoc, modDoc)
|
||||
assert.Equal(t, 75, changes.TotalChanges())
|
||||
@@ -38,8 +38,8 @@ func TestCompareSwaggerDocuments(t *testing.T) {
|
||||
infoOrig, _ := datamodel.ExtractSpecInfo(original)
|
||||
infoMod, _ := datamodel.ExtractSpecInfo(modified)
|
||||
|
||||
origDoc, _ := v2.CreateDocument(infoOrig)
|
||||
modDoc, _ := v2.CreateDocument(infoMod)
|
||||
origDoc, _ := v2.CreateDocumentFromConfig(infoOrig, datamodel.NewDocumentConfiguration())
|
||||
modDoc, _ := v2.CreateDocumentFromConfig(infoMod, datamodel.NewDocumentConfiguration())
|
||||
|
||||
changes := CompareSwaggerDocuments(origDoc, modDoc)
|
||||
assert.Equal(t, 52, changes.TotalChanges())
|
||||
@@ -57,8 +57,8 @@ func Benchmark_CompareOpenAPIDocuments(b *testing.B) {
|
||||
|
||||
infoOrig, _ := datamodel.ExtractSpecInfo(original)
|
||||
infoMod, _ := datamodel.ExtractSpecInfo(modified)
|
||||
origDoc, _ := v3.CreateDocument(infoOrig)
|
||||
modDoc, _ := v3.CreateDocument(infoMod)
|
||||
origDoc, _ := v3.CreateDocumentFromConfig(infoOrig, datamodel.NewDocumentConfiguration())
|
||||
modDoc, _ := v3.CreateDocumentFromConfig(infoMod, datamodel.NewDocumentConfiguration())
|
||||
|
||||
for i := 0; i < b.N; i++ {
|
||||
CompareOpenAPIDocuments(origDoc, modDoc)
|
||||
@@ -72,8 +72,8 @@ func Benchmark_CompareSwaggerDocuments(b *testing.B) {
|
||||
infoOrig, _ := datamodel.ExtractSpecInfo(original)
|
||||
infoMod, _ := datamodel.ExtractSpecInfo(modified)
|
||||
|
||||
origDoc, _ := v2.CreateDocument(infoOrig)
|
||||
modDoc, _ := v2.CreateDocument(infoMod)
|
||||
origDoc, _ := v2.CreateDocumentFromConfig(infoOrig, datamodel.NewDocumentConfiguration())
|
||||
modDoc, _ := v2.CreateDocumentFromConfig(infoMod, datamodel.NewDocumentConfiguration())
|
||||
|
||||
for i := 0; i < b.N; i++ {
|
||||
CompareSwaggerDocuments(origDoc, modDoc)
|
||||
@@ -87,8 +87,8 @@ func Benchmark_CompareOpenAPIDocuments_NoChange(b *testing.B) {
|
||||
|
||||
infoOrig, _ := datamodel.ExtractSpecInfo(original)
|
||||
infoMod, _ := datamodel.ExtractSpecInfo(modified)
|
||||
origDoc, _ := v3.CreateDocument(infoOrig)
|
||||
modDoc, _ := v3.CreateDocument(infoMod)
|
||||
origDoc, _ := v3.CreateDocumentFromConfig(infoOrig, datamodel.NewDocumentConfiguration())
|
||||
modDoc, _ := v3.CreateDocumentFromConfig(infoMod, datamodel.NewDocumentConfiguration())
|
||||
|
||||
for i := 0; i < b.N; i++ {
|
||||
CompareOpenAPIDocuments(origDoc, modDoc)
|
||||
@@ -102,8 +102,8 @@ func Benchmark_CompareK8s(b *testing.B) {
|
||||
|
||||
infoOrig, _ := datamodel.ExtractSpecInfo(original)
|
||||
infoMod, _ := datamodel.ExtractSpecInfo(modified)
|
||||
origDoc, _ := v2.CreateDocument(infoOrig)
|
||||
modDoc, _ := v2.CreateDocument(infoMod)
|
||||
origDoc, _ := v2.CreateDocumentFromConfig(infoOrig, datamodel.NewDocumentConfiguration())
|
||||
modDoc, _ := v2.CreateDocumentFromConfig(infoMod, datamodel.NewDocumentConfiguration())
|
||||
|
||||
for i := 0; i < b.N; i++ {
|
||||
CompareSwaggerDocuments(origDoc, modDoc)
|
||||
@@ -117,8 +117,8 @@ func Benchmark_CompareStripe(b *testing.B) {
|
||||
|
||||
infoOrig, _ := datamodel.ExtractSpecInfo(original)
|
||||
infoMod, _ := datamodel.ExtractSpecInfo(modified)
|
||||
origDoc, _ := v3.CreateDocument(infoOrig)
|
||||
modDoc, _ := v3.CreateDocument(infoMod)
|
||||
origDoc, _ := v3.CreateDocumentFromConfig(infoOrig, datamodel.NewDocumentConfiguration())
|
||||
modDoc, _ := v3.CreateDocumentFromConfig(infoMod, datamodel.NewDocumentConfiguration())
|
||||
|
||||
for i := 0; i < b.N; i++ {
|
||||
CompareOpenAPIDocuments(origDoc, modDoc)
|
||||
@@ -138,8 +138,8 @@ func ExampleCompareOpenAPIDocuments() {
|
||||
infoModified, _ := datamodel.ExtractSpecInfo(modified)
|
||||
|
||||
// Build OpenAPI Documents from SpecInfo
|
||||
origDocument, _ := v3.CreateDocument(infoOriginal)
|
||||
modDocDocument, _ := v3.CreateDocument(infoModified)
|
||||
origDocument, _ := v3.CreateDocumentFromConfig(infoOriginal, datamodel.NewDocumentConfiguration())
|
||||
modDocDocument, _ := v3.CreateDocumentFromConfig(infoModified, datamodel.NewDocumentConfiguration())
|
||||
|
||||
// Compare OpenAPI Documents and extract to *DocumentChanges
|
||||
changes := CompareOpenAPIDocuments(origDocument, modDocDocument)
|
||||
|
||||
Reference in New Issue
Block a user