From 6d37c1f105d5d812457e4316f3a60fe770524843 Mon Sep 17 00:00:00 2001 From: quobix Date: Sat, 1 Feb 2025 18:06:34 -0500 Subject: [PATCH] Fixed issue with `$refs` being picked up in extensions. extensions that contain references will not be parsed, they should not be parsed. --- index/extract_refs.go | 13 +++++++++++++ index/extract_refs_test.go | 18 ++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/index/extract_refs.go b/index/extract_refs.go index a3ef1dd3..6d96a28b 100644 --- a/index/extract_refs.go +++ b/index/extract_refs.go @@ -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]) { diff --git a/index/extract_refs_test.go b/index/extract_refs_test.go index c3b07250..a74c9517 100644 --- a/index/extract_refs_test.go +++ b/index/extract_refs_test.go @@ -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) +}