diff --git a/utils/utils.go b/utils/utils.go index 61c357d..e9244b8 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -336,7 +336,7 @@ func FindKeyNodeFull(key string, nodes []*yaml.Node) (keyNode *yaml.Node, labelN } } - if key == v.Content[x].Value { + if len(v.Content) > 0 && key == v.Content[x].Value { if IsNodeMap(v) { if x+1 == len(v.Content) { return v, v.Content[x], NodeAlias(v.Content[x]) diff --git a/utils/utils_test.go b/utils/utils_test.go index 5290ed6..244cf21 100644 --- a/utils/utils_test.go +++ b/utils/utils_test.go @@ -1002,6 +1002,47 @@ func TestDetermineJSONWhitespaceLength_None(t *testing.T) { assert.Equal(t, 0, DetermineWhitespaceLength(string(someBytes))) } +func TestFindFirstKeyNode_MergeTest(t *testing.T) { + yml := []byte(`openapi: 3.0.3 + +x-a: &anchor + important-field: true + +x-b: + <<: *anchor +`) + + var rootNode yaml.Node + _ = yaml.Unmarshal(yml, &rootNode) + + k, v := FindFirstKeyNode("important-field", rootNode.Content[0].Content[5].Content, 0) + assert.NotNil(t, k) + assert.NotNil(t, v) + assert.Equal(t, "true", v.Value) + +} + +func TestFindKeyNodeFull_MergeTest(t *testing.T) { + yml := []byte(`openapi: 3.0.3 + +x-a: &anchor + important-field: true + +x-b: + <<: *anchor +`) + + var rootNode yaml.Node + _ = yaml.Unmarshal(yml, &rootNode) + + k, l, v := FindKeyNodeFull("servers", rootNode.Content[0].Content) + assert.NotNil(t, k) + assert.NotNil(t, v) + assert.NotNil(t, l) + assert.Equal(t, "true", v.Value) + +} + func TestFindFirstKeyNode_DoubleMerge(t *testing.T) { yml := []byte(`openapi: 3.0.3