Skip to content

Commit

Permalink
Added in full resolving for specs is virtual filesystems
Browse files Browse the repository at this point in the history
added last remaining coverage

Signed-off-by: quobix <[email protected]>
  • Loading branch information
daveshanley committed Nov 4, 2023
1 parent f134ac2 commit 9b92a55
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 14 deletions.
9 changes: 2 additions & 7 deletions index/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,8 @@ func (r *ResolvingError) Error() string {
errs := utils.UnwrapErrors(r.ErrorRef)
var msgs []string
for _, e := range errs {
if idxErr, ok := e.(*IndexingError); ok {
msgs = append(msgs, fmt.Sprintf("%s: %s [%d:%d]", idxErr.Error(),
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))
}
msgs = append(msgs, fmt.Sprintf("%s: %s [%d:%d]", e.Error(),
r.Path, r.Node.Line, r.Node.Column))
}
return strings.Join(msgs, "\n")
}
Expand Down
63 changes: 61 additions & 2 deletions index/resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"net/http"
"net/url"
"os"
"path/filepath"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -18,6 +19,31 @@ func TestNewResolver(t *testing.T) {
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) {
baseDir := "../test_specs/stripe.yaml"
resolveFile, _ := os.ReadFile(baseDir)
Expand Down Expand Up @@ -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)
}
20 changes: 15 additions & 5 deletions index/rolodex.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,19 +327,29 @@ func (r *Rolodex) CheckForCircularReferences() {

// Resolve resolves references in the rolodex.
func (r *Rolodex) Resolve() {

var resolvers []*Resolver
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 {
r.caughtErrors = append(r.caughtErrors, resolvingErrors[e])
}
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 {
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.infiniteCircularReferences = append(r.infiniteCircularReferences, r.rootIndex.resolver.GetInfiniteCircularReferences()...)
r.safeCircularReferences = append(r.safeCircularReferences, res.GetSafeCircularReferences()...)
r.infiniteCircularReferences = append(r.infiniteCircularReferences, res.GetInfiniteCircularReferences()...)
}
r.resolved = true
}
Expand Down

0 comments on commit 9b92a55

Please sign in to comment.