Added in full resolving for specs is virtual filesystems

added last remaining coverage

Signed-off-by: quobix <dave@quobix.com>
This commit is contained in:
quobix
2023-11-04 11:28:22 -04:00
parent f134ac27b6
commit 9b92a55536
3 changed files with 78 additions and 14 deletions

View File

@@ -32,13 +32,8 @@ func (r *ResolvingError) Error() string {
errs := utils.UnwrapErrors(r.ErrorRef) errs := utils.UnwrapErrors(r.ErrorRef)
var msgs []string var msgs []string
for _, e := range errs { for _, e := range errs {
if idxErr, ok := e.(*IndexingError); ok { msgs = append(msgs, fmt.Sprintf("%s: %s [%d:%d]", e.Error(),
msgs = append(msgs, fmt.Sprintf("%s: %s [%d:%d]", idxErr.Error(), r.Path, r.Node.Line, r.Node.Column))
idxErr.Path, idxErr.Node.Line, idxErr.Node.Column))
} else {
msgs = append(msgs, fmt.Sprintf("%s: %s [%d:%d]", e.Error(),
r.Path, r.Node.Line, r.Node.Column))
}
} }
return strings.Join(msgs, "\n") return strings.Join(msgs, "\n")
} }

View File

@@ -8,6 +8,7 @@ import (
"net/http" "net/http"
"net/url" "net/url"
"os" "os"
"path/filepath"
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
@@ -18,6 +19,31 @@ func TestNewResolver(t *testing.T) {
assert.Nil(t, NewResolver(nil)) assert.Nil(t, NewResolver(nil))
} }
func TestResolvingError_Error(t *testing.T) {
errs := []error{
&ResolvingError{
Path: "$.test1",
ErrorRef: errors.New("test1"),
Node: &yaml.Node{
Line: 1,
Column: 1,
},
},
&ResolvingError{
Path: "$.test2",
ErrorRef: errors.New("test2"),
Node: &yaml.Node{
Line: 1,
Column: 1,
},
},
}
assert.Equal(t, "test1: $.test1 [1:1]", errs[0].Error())
assert.Equal(t, "test2: $.test2 [1:1]", errs[1].Error())
}
func Benchmark_ResolveDocumentStripe(b *testing.B) { func Benchmark_ResolveDocumentStripe(b *testing.B) {
baseDir := "../test_specs/stripe.yaml" baseDir := "../test_specs/stripe.yaml"
resolveFile, _ := os.ReadFile(baseDir) resolveFile, _ := os.ReadFile(baseDir)
@@ -918,7 +944,40 @@ components:
} }
/* func TestLocateRefEnd_WithResolve(t *testing.T) {
yml, _ := os.ReadFile("../../test_specs/first.yaml")
var bsn yaml.Node
_ = yaml.Unmarshal(yml, &bsn)
*/ cf := CreateOpenAPIIndexConfig()
cf.BasePath = "../test_specs"
localFSConfig := &LocalFSConfig{
BaseDirectory: cf.BasePath,
FileFilters: []string{"first.yaml", "second.yaml", "third.yaml", "fourth.yaml"},
DirFS: os.DirFS(cf.BasePath),
}
localFs, _ := NewLocalFSWithConfig(localFSConfig)
rolo := NewRolodex(cf)
rolo.AddLocalFS(cf.BasePath, localFs)
rolo.SetRootNode(&bsn)
rolo.IndexTheRolodex()
wd, _ := os.Getwd()
cp, _ := filepath.Abs(filepath.Join(wd, "../test_specs/third.yaml"))
third := localFs.GetFiles()[cp]
refs := third.GetIndex().GetMappedReferences()
fullDef := fmt.Sprintf("%s#/properties/property/properties/statistics", cp)
ref := refs[fullDef]
assert.Equal(t, "statistics", ref.Name)
isRef, _, _ := utils.IsNodeRefValue(ref.Node)
assert.True(t, isRef)
// resolve the stack, it should convert the ref to a node.
rolo.Resolve()
isRef, _, _ = utils.IsNodeRefValue(ref.Node)
assert.False(t, isRef)
}

View File

@@ -327,19 +327,29 @@ func (r *Rolodex) CheckForCircularReferences() {
// Resolve resolves references in the rolodex. // Resolve resolves references in the rolodex.
func (r *Rolodex) Resolve() { func (r *Rolodex) Resolve() {
var resolvers []*Resolver
if r.rootIndex != nil && r.rootIndex.resolver != nil { if r.rootIndex != nil && r.rootIndex.resolver != nil {
resolvingErrors := r.rootIndex.resolver.Resolve() resolvers = append(resolvers, r.rootIndex.resolver)
}
for _, idx := range r.indexes {
if idx.resolver != nil {
resolvers = append(resolvers, idx.resolver)
}
}
for _, res := range resolvers {
resolvingErrors := res.Resolve()
for e := range resolvingErrors { for e := range resolvingErrors {
r.caughtErrors = append(r.caughtErrors, resolvingErrors[e]) r.caughtErrors = append(r.caughtErrors, resolvingErrors[e])
} }
if len(r.rootIndex.resolver.ignoredPolyReferences) > 0 { if len(r.rootIndex.resolver.ignoredPolyReferences) > 0 {
r.ignoredCircularReferences = append(r.ignoredCircularReferences, r.rootIndex.resolver.ignoredPolyReferences...) r.ignoredCircularReferences = append(r.ignoredCircularReferences, res.ignoredPolyReferences...)
} }
if len(r.rootIndex.resolver.ignoredArrayReferences) > 0 { if len(r.rootIndex.resolver.ignoredArrayReferences) > 0 {
r.ignoredCircularReferences = append(r.ignoredCircularReferences, r.rootIndex.resolver.ignoredArrayReferences...) r.ignoredCircularReferences = append(r.ignoredCircularReferences, res.ignoredArrayReferences...)
} }
r.safeCircularReferences = append(r.safeCircularReferences, r.rootIndex.resolver.GetSafeCircularReferences()...) r.safeCircularReferences = append(r.safeCircularReferences, res.GetSafeCircularReferences()...)
r.infiniteCircularReferences = append(r.infiniteCircularReferences, r.rootIndex.resolver.GetInfiniteCircularReferences()...) r.infiniteCircularReferences = append(r.infiniteCircularReferences, res.GetInfiniteCircularReferences()...)
} }
r.resolved = true r.resolved = true
} }