mirror of
https://github.com/LukeHagar/libopenapi.git
synced 2025-12-10 12:37:48 +00:00
Off by one issue fixed https://github.com/daveshanley/vacuum/issues/356
Reported by vacuum issue, this use-case is now handled correctly and prevents a panic. Signed-off-by: quobix <dave@quobix.com>
This commit is contained in:
@@ -253,6 +253,9 @@ func FindKeyNode(key string, nodes []*yaml.Node) (keyNode *yaml.Node, valueNode
|
|||||||
//numNodes := len(nodes)
|
//numNodes := len(nodes)
|
||||||
for i, v := range nodes {
|
for i, v := range nodes {
|
||||||
if i%2 == 0 && key == v.Value {
|
if i%2 == 0 && key == v.Value {
|
||||||
|
if len(nodes) <= i+1 {
|
||||||
|
return v, nodes[i]
|
||||||
|
}
|
||||||
return v, nodes[i+1] // next node is what we need.
|
return v, nodes[i+1] // next node is what we need.
|
||||||
}
|
}
|
||||||
for x, j := range v.Content {
|
for x, j := range v.Content {
|
||||||
|
|||||||
@@ -317,6 +317,19 @@ func TestFindKeyNode(t *testing.T) {
|
|||||||
assert.Equal(t, 47, k.Line)
|
assert.Equal(t, 47, k.Line)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestFindKeyNodeOffByOne(t *testing.T) {
|
||||||
|
|
||||||
|
k, v := FindKeyNode("key", []*yaml.Node{
|
||||||
|
{
|
||||||
|
Value: "key",
|
||||||
|
Line: 999,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
assert.NotNil(t, k)
|
||||||
|
assert.NotNil(t, v)
|
||||||
|
assert.Equal(t, 999, k.Line)
|
||||||
|
}
|
||||||
|
|
||||||
func TestFindKeyNode_ValueIsKey(t *testing.T) {
|
func TestFindKeyNode_ValueIsKey(t *testing.T) {
|
||||||
|
|
||||||
a := &yaml.Node{
|
a := &yaml.Node{
|
||||||
|
|||||||
Reference in New Issue
Block a user