From 81dfb37f17aa8cfaaa935ccb58adb6448fd3a2fe Mon Sep 17 00:00:00 2001 From: Nico Date: Fri, 27 Nov 2026 02:00:42 +0100 Subject: [PATCH] fix(index): avoid out-of-range panic when a $ref with siblings is resolved in place --- index/resolver_relatives.go | 9 ++-- index/resolver_relatives_siblings_test.go | 62 +++++++++++++++++++++++ 2 files changed, 68 insertions(+), 3 deletions(-) create mode 100644 index/resolver_relatives_siblings_test.go diff --git a/index/resolver_relatives.go b/index/resolver_relatives.go index 41f1ea72..b9d3cd0b 100644 --- a/index/resolver_relatives.go +++ b/index/resolver_relatives.go @@ -80,8 +80,11 @@ func (resolver *Resolver) extractRelativesWithState( var found []*Reference if node != nil && len(node.Content) > 0 { + // extractRelativeReference may replace node.Content with the resolved target while this loop + // is still running (a $ref with sibling keys), so walk and index one snapshot of the content. + content := node.Content skip := false - for i, n := range node.Content { + for i, n := range content { if skip { skip = false continue @@ -91,14 +94,14 @@ func (resolver *Resolver) extractRelativesWithState( childState := state childPath := walkPath if node.Kind == yaml.MappingNode && i%2 == 1 { - childPath = append(walkPath, node.Content[i-1].Value) + childPath = append(walkPath, content[i-1].Value) } else if node.Kind == yaml.SequenceNode { childPath = append(walkPath, "[]") } found = append(found, resolver.extractNestedRelatives(ref, node, n, childState, childPath)...) } - if i%2 == 0 && n.Value == "$ref" && len(node.Content) > i%2+1 { + if i%2 == 0 && n.Value == "$ref" && len(content) > i%2+1 { if relative, handled, skipNext := resolver.extractRelativeReference(ref, node, parent, n, i, state); handled { if relative != nil { found = append(found, relative) diff --git a/index/resolver_relatives_siblings_test.go b/index/resolver_relatives_siblings_test.go new file mode 100644 index 00000000..5eddd771 --- /dev/null +++ b/index/resolver_relatives_siblings_test.go @@ -0,0 +1,62 @@ +package index + +import ( + "context" + "os" + "path/filepath" + "testing" + + "github.com/pb33f/go-yaml" + "github.com/pb33f/testify/require" +) + +// A schema holding an external $ref next to sibling keys (allowed in OpenAPI 3.1) has its content +// replaced by the resolved target while extractRelativesWithState is still walking it. When the +// target has fewer entries than the referencing node, the walk must not index past the end. +func TestResolver_ExternalRefWithSiblingsLargerThanTarget(t *testing.T) { + base := `openapi: 3.1.0 +info: {title: base, version: '1'} +components: + schemas: + Base: + type: object + description: base + required: [id] + properties: + id: {type: string} +` + root := `openapi: 3.1.0 +info: {title: root, version: '1'} +paths: {} +components: + schemas: + Thing: + type: object + description: thing + required: [id, name] + $ref: './base.yaml#/components/schemas/Base' + properties: + name: {type: string} +` + tempDir := t.TempDir() + rootPath := filepath.Join(tempDir, "root.yaml") + require.NoError(t, os.WriteFile(rootPath, []byte(root), 0o600)) + require.NoError(t, os.WriteFile(filepath.Join(tempDir, "base.yaml"), []byte(base), 0o600)) + + var rootNode yaml.Node + require.NoError(t, yaml.Unmarshal([]byte(root), &rootNode)) + + config := CreateOpenAPIIndexConfig() + config.BasePath = tempDir + config.SpecAbsolutePath = rootPath + + rolo := NewRolodex(config) + rolo.SetRootNode(&rootNode) + fileFS, err := NewLocalFSWithConfig(&LocalFSConfig{BaseDirectory: tempDir, IndexConfig: config}) + require.NoError(t, err) + rolo.AddLocalFS(tempDir, fileFS) + require.NoError(t, rolo.IndexTheRolodex(context.Background())) + + require.NotPanics(t, rolo.Resolve) + require.Empty(t, rolo.GetRootIndex().GetResolver().GetResolvingErrors()) +}