Skip to content
Open
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: 6 additions & 3 deletions index/resolver_relatives.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
62 changes: 62 additions & 0 deletions index/resolver_relatives_siblings_test.go
Original file line number Diff line number Diff line change
@@ -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())
}
Loading