fix: Index incorrectly handling file references on the local file system.

This was reported in https://github.com/daveshanley/vacuum/issues/225, it's a continuation of the issue found in https://github.com/pb33f/libopenapi/issues/37. This fix allows the full import of the file (not just a path/component) to be imported.

Signed-off-by: Dave Shanley <dave@quobix.com>
This commit is contained in:
Dave Shanley
2023-01-04 06:43:32 -05:00
parent 07a11be5a3
commit 6b28e414bd
3 changed files with 56 additions and 7 deletions

View File

@@ -555,6 +555,40 @@ func TestSpecIndex_lookupRemoteReference_NoComponent(t *testing.T) {
assert.Nil(t, b)
}
// Discovered in issue https://github.com/daveshanley/vacuum/issues/225
func TestSpecIndex_lookupFileReference_NoComponent(t *testing.T) {
index := new(SpecIndex)
_ = ioutil.WriteFile("coffee-time.yaml", []byte("time: for coffee"), 0o664)
defer os.Remove("coffee-time.yaml")
index.seenRemoteSources = make(map[string]*yaml.Node)
a, b, err := index.lookupFileReference("coffee-time.yaml")
assert.NoError(t, err)
assert.NotNil(t, a)
assert.NotNil(t, b)
}
func TestSpecIndex_CheckIndexDiscoversNoComponentLocalFileReference(t *testing.T) {
_ = ioutil.WriteFile("coffee-time.yaml", []byte("name: time for coffee"), 0o664)
defer os.Remove("coffee-time.yaml")
yml := `openapi: 3.0.3
paths:
/cakes:
post:
parameters:
- $ref: 'coffee-time.yaml'`
var rootNode yaml.Node
yaml.Unmarshal([]byte(yml), &rootNode)
index := NewSpecIndex(&rootNode)
assert.NotNil(t, index.GetAllParametersFromOperations()["/cakes"]["post"]["coffee-time.yaml"].Node)
}
func TestSpecIndex_lookupRemoteReference_SeenSourceSimulation_BadJSON(t *testing.T) {
index := new(SpecIndex)
index.seenRemoteSources = make(map[string]*yaml.Node)