Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: a parameter in the rolodex should be evaluated using the index that the node is in #336

Merged
merged 6 commits into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions index/extract_refs.go
Original file line number Diff line number Diff line change
Expand Up @@ -640,7 +640,16 @@ func (index *SpecIndex) ExtractComponentsFromRefs(refs []*Reference) []*Referenc
index.refLock.Unlock()
} else {
index.refLock.Unlock()
// If it's local, this is safe to do unlocked
uri := strings.Split(ref.FullDefinition, "#/")
unsafeAsync := len(uri) == 2 && len(uri[0]) > 0
if unsafeAsync {
index.refLock.Lock()
}
located := index.FindComponent(ref.FullDefinition)
if unsafeAsync {
index.refLock.Unlock()
}
if located != nil {

// have we already mapped this?
Expand Down
11 changes: 5 additions & 6 deletions index/rolodex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1541,11 +1541,11 @@ func TestRolodex_SimpleTest_OneDoc(t *testing.T) {
//assert.NotZero(t, rolo.GetIndexingDuration()) comes back as 0 on windows.
assert.NotNil(t, rolo.GetRootIndex())
assert.Len(t, rolo.GetIndexes(), 10)
assert.Len(t, rolo.GetAllReferences(), 7)
assert.Len(t, rolo.GetAllMappedReferences(), 7)
assert.Len(t, rolo.GetAllReferences(), 8)
assert.Len(t, rolo.GetAllMappedReferences(), 8)

lineCount := rolo.GetFullLineCount()
assert.Equal(t, int64(158), lineCount, "total line count in the rolodex is wrong")
assert.Equal(t, int64(167), lineCount, "total line count in the rolodex is wrong")

assert.NoError(t, err)
assert.Len(t, rolo.indexes, 10)
Expand All @@ -1562,10 +1562,9 @@ func TestRolodex_SimpleTest_OneDoc(t *testing.T) {
assert.True(t, strings.HasSuffix(f.GetFullPath(), "rolodex_test_data"+string(os.PathSeparator)+"components.yaml"))
assert.NotNil(t, f.ModTime())
if runtime.GOOS != "windows" {
assert.Equal(t, int64(283), f.Size())
assert.Equal(t, int64(448), f.Size())
} else {
assert.Equal(t, int64(295), f.Size())

assert.Equal(t, int64(467), f.Size())
}
assert.False(t, f.IsDir())
assert.Nil(t, f.Sys())
Expand Down
7 changes: 7 additions & 0 deletions index/rolodex_test_data/components.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ info:
title: Rolodex Test Data
version: 1.0.0
components:
parameters:
SomeParam:
name: someParam
in: query
description: A parameter that does nothing. Ding a ling!
schema:
type: string
schemas:
Ding:
type: object
Expand Down
2 changes: 2 additions & 0 deletions index/rolodex_test_data/paths/paths.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/some/path:
get:
parameters:
- $ref: '../components.yaml#/components/parameters/SomeParam'
responses:
'200':
description: OK
Expand Down
73 changes: 48 additions & 25 deletions index/search_rolodex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ package index

import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/vmware-labs/yaml-jsonpath/pkg/yamlpath"
"gopkg.in/yaml.v3"
"path/filepath"
"strings"
"sync"
"testing"
)

Expand Down Expand Up @@ -129,41 +131,62 @@ func TestRolodex_FindNodeOrigin_ModifyLookup(t *testing.T) {
}

func TestSpecIndex_TestPathsAsRefWithFiles(t *testing.T) {

yml := `paths:
// We're TDD'ing some code that previously had a race condition.
// This test is to ensure that we don't regress.
wg := sync.WaitGroup{}
wg.Add(1000)
for i := 0; i < 1000; i++ {
go func(i int) {
defer wg.Done()
yml := `paths:
/test:
$ref: 'rolodex_test_data/paths/paths.yaml'
$ref: 'rolodex_test_data/paths/paths.yaml#/~1some~1path'
/test-2:
$ref: './rolodex_test_data/paths/paths.yaml'
$ref: './rolodex_test_data/paths/paths.yaml#/~1some~1path'
`

baseDir := "."
baseDir := "."

cf := CreateOpenAPIIndexConfig()
cf.BasePath = baseDir
cf.AvoidCircularReferenceCheck = true
cf := CreateOpenAPIIndexConfig()
cf.BasePath = baseDir
cf.AvoidCircularReferenceCheck = true

fileFS, err := NewLocalFSWithConfig(&LocalFSConfig{
BaseDirectory: baseDir,
IndexConfig: cf,
})
if err != nil {
t.Fatal(err)
}
fileFS, err := NewLocalFSWithConfig(&LocalFSConfig{
BaseDirectory: baseDir,
IndexConfig: cf,
})
if err != nil {
t.Fatal(err)
}

rolo := NewRolodex(cf)
rolo.AddLocalFS(baseDir, fileFS)
rolo := NewRolodex(cf)
rolo.AddLocalFS(baseDir, fileFS)

var rootNode yaml.Node
_ = yaml.Unmarshal([]byte(yml), &rootNode)
var rootNode yaml.Node
_ = yaml.Unmarshal([]byte(yml), &rootNode)

rolo.SetRootNode(&rootNode)
rolo.SetRootNode(&rootNode)

err = rolo.IndexTheRolodex()
assert.NoError(t, err)
rolo.Resolve()
err = rolo.IndexTheRolodex()
assert.NoError(t, err)
require.Len(t, rolo.indexes, 2)

assert.Len(t, rolo.indexes, 2)
assert.Len(t, rolo.GetCaughtErrors(), 0)
rolo.Resolve()

require.Len(t, rolo.GetCaughtErrors(), 0)

params := rolo.rootIndex.GetAllParametersFromOperations()
require.Len(t, params, 2)
lookupPath, ok := params["/test"]
require.True(t, ok)
lookupOperation, ok := lookupPath["get"]
require.True(t, ok)
require.Len(t, lookupOperation, 1)
lookupRef, ok := lookupOperation["../components.yaml#/components/parameters/SomeParam"]
require.True(t, ok)
require.Len(t, lookupRef, 1)
require.Equal(t, lookupRef[0].Name, "SomeParam")
}(i)
}
wg.Wait()
}
26 changes: 25 additions & 1 deletion index/utility_methods.go
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,8 @@ func (index *SpecIndex) scanOperationParams(params []*yaml.Node, keyNode, pathIt
paramRef := index.allMappedRefs[paramRefName]
if paramRef == nil {
// could be in the rolodex
ref := seekRefEnd(index, paramRefName)
searchInIndex := findIndex(index, param.Content[1])
ref := seekRefEnd(searchInIndex, paramRefName)
if ref != nil {
paramRef = ref
if strings.Contains(paramRefName, "%") {
Expand Down Expand Up @@ -510,6 +511,29 @@ func (index *SpecIndex) scanOperationParams(params []*yaml.Node, keyNode, pathIt
}
}

func findIndex(index *SpecIndex, i *yaml.Node) *SpecIndex {
rolodex := index.GetRolodex()
if rolodex == nil {
return index
}
allIndexes := rolodex.GetIndexes()
for _, searchIndex := range allIndexes {
nodeMap := searchIndex.GetNodeMap()
line, ok := nodeMap[i.Line]
if !ok {
continue
}
node, ok := line[i.Column]
if !ok {
continue
}
if node == i {
return searchIndex
}
}
return index
}

func runIndexFunction(funcs []func() int, wg *sync.WaitGroup) {
for _, cFunc := range funcs {
go func(wg *sync.WaitGroup, cf func() int) {
Expand Down
Loading