Skip to content

Commit

Permalink
Fixed issue with $refs being picked up in extensions.
Browse files Browse the repository at this point in the history
extensions that contain references will not be parsed, they should not be parsed.
  • Loading branch information
daveshanley committed Feb 1, 2025
1 parent 2b2d734 commit 6d37c1f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
13 changes: 13 additions & 0 deletions index/extract_refs.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,19 @@ func (index *SpecIndex) ExtractRefs(node, parent *yaml.Node, seenPath []string,

if i%2 == 0 && n.Value == "$ref" {

// check if this reference is under an extension or not, if so, drop it from the index.
ext := false
for _, spi := range seenPath {
if strings.HasPrefix(spi, "x-") {
ext = true
break
}
}

if ext {
continue
}

// only look at scalar values, not maps (looking at you k8s)
if len(node.Content) > i+1 {
if !utils.IsNodeStringValue(node.Content[i+1]) {
Expand Down
18 changes: 18 additions & 0 deletions index/extract_refs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,3 +194,21 @@ components:
idx := NewSpecIndexWithConfig(&rootNode, c)
assert.Len(t, idx.allEnums, 3)
}

func TestSpecIndex_ExtractRefs_CheckRefsUnderExtensionsAreNotIncluded(t *testing.T) {
yml := `openapi: 3.1.0
components:
schemas:
Pasta:
x-hello:
thing:
$ref: '404'
`
var rootNode yaml.Node
_ = yaml.Unmarshal([]byte(yml), &rootNode)
c := CreateOpenAPIIndexConfig()
idx := NewSpecIndexWithConfig(&rootNode, c)
assert.Len(t, idx.allMappedRefs, 0)
assert.Len(t, idx.allRefs, 0)
assert.Len(t, idx.refErrors, 0)
}

0 comments on commit 6d37c1f

Please sign in to comment.