Signed-off-by: quobix <dave@quobix.com>
This commit is contained in:
quobix
2024-02-20 08:48:54 -05:00
parent 3d5ab30755
commit 79d49faf70
3 changed files with 115 additions and 5 deletions

View File

@@ -489,7 +489,12 @@ func (resolver *Resolver) extractRelatives(ref *Reference, node, parent *yaml.No
var found []*Reference var found []*Reference
if len(node.Content) > 0 { if len(node.Content) > 0 {
skip := false
for i, n := range node.Content { for i, n := range node.Content {
if skip {
skip = false
continue
}
if utils.IsNodeMap(n) || utils.IsNodeArray(n) { if utils.IsNodeMap(n) || utils.IsNodeArray(n) {
depth++ depth++
@@ -790,7 +795,8 @@ func (resolver *Resolver) extractRelatives(ref *Reference, node, parent *yaml.No
} }
} }
} }
break skip = true
continue
} }
} }
} }

View File

@@ -98,7 +98,7 @@ func Benchmark_ResolveDocumentStripe(b *testing.B) {
rolo.SetRootNode(&rootNode) rolo.SetRootNode(&rootNode)
indexedErr := rolo.IndexTheRolodex() indexedErr := rolo.IndexTheRolodex()
assert.Len(b, utils.UnwrapErrors(indexedErr), 3) assert.Len(b, utils.UnwrapErrors(indexedErr), 1)
} }
} }
@@ -1270,3 +1270,106 @@ paths:
assert.Equal(t, nodes[0].Content[1].Value, "message") assert.Equal(t, nodes[0].Content[1].Value, "message")
} }
func TestDocument_LoopThroughAnArray(t *testing.T) {
var d = `openapi: "3.0.1"
components:
schemas:
B:
type: object
properties:
children:
type: array
items:
$ref: '#/components/schemas/B'`
var rootNode yaml.Node
_ = yaml.Unmarshal([]byte(d), &rootNode)
config := CreateClosedAPIIndexConfig()
config.IgnoreArrayCircularReferences = true
idx := NewSpecIndexWithConfig(&rootNode, config)
resolver := NewResolver(idx)
resolver.IgnoreArrayCircularReferences()
assert.NotNil(t, resolver)
circ := resolver.Resolve()
assert.Len(t, circ, 0)
assert.Len(t, resolver.GetIgnoredCircularArrayReferences(), 1)
}
func TestDocument_ObjectWithPolyAndArray(t *testing.T) {
var d = `openapi: "3.0.1"
components:
schemas:
A:
type: object
properties: {}
B:
type: object
allOf:
- $ref: '#/components/schemas/A'
properties:
children:
type: array
items:
$ref: '#/components/schemas/B'`
var rootNode yaml.Node
_ = yaml.Unmarshal([]byte(d), &rootNode)
config := CreateClosedAPIIndexConfig()
config.IgnoreArrayCircularReferences = true
idx := NewSpecIndexWithConfig(&rootNode, config)
resolver := NewResolver(idx)
resolver.IgnoreArrayCircularReferences()
assert.NotNil(t, resolver)
circ := resolver.Resolve()
assert.Len(t, circ, 0)
assert.Len(t, resolver.GetIgnoredCircularArrayReferences(), 1)
}
func TestDocument_ObjectWithMultiPolyAndArray(t *testing.T) {
var d = `openapi: "3.0.1"
components:
schemas:
A:
type: object
properties: {}
B:
type: object
allOf:
- $ref: '#/components/schemas/A'
oneOf:
- $ref: '#/components/schemas/B'
properties:
children:
type: array
items:
$ref: '#/components/schemas/B'`
var rootNode yaml.Node
_ = yaml.Unmarshal([]byte(d), &rootNode)
config := CreateClosedAPIIndexConfig()
config.IgnoreArrayCircularReferences = true
idx := NewSpecIndexWithConfig(&rootNode, config)
resolver := NewResolver(idx)
resolver.IgnoreArrayCircularReferences()
assert.NotNil(t, resolver)
circ := resolver.Resolve()
assert.Len(t, circ, 0)
assert.Len(t, resolver.GetSafeCircularReferences(), 1)
assert.Len(t, resolver.GetIgnoredCircularArrayReferences(), 0)
}

View File

@@ -1233,13 +1233,13 @@ components:
tmp := "tmp-g" tmp := "tmp-g"
_ = os.Mkdir(tmp, 0755) _ = os.Mkdir(tmp, 0755)
firstFile, fErr = os.CreateTemp(tmp, "*-first.yaml") firstFile, fErr = os.CreateTemp(tmp, "first-*.yaml")
assert.NoError(t, fErr) assert.NoError(t, fErr)
secondFile, fErr = os.CreateTemp(tmp, "*-second.yaml") secondFile, fErr = os.CreateTemp(tmp, "second-*.yaml")
assert.NoError(t, fErr) assert.NoError(t, fErr)
thirdFile, fErr = os.CreateTemp(tmp, "*-third.yaml") thirdFile, fErr = os.CreateTemp(tmp, "third-*.yaml")
assert.NoError(t, fErr) assert.NoError(t, fErr)
first = strings.ReplaceAll(first, "$2", secondFile.Name()) first = strings.ReplaceAll(first, "$2", secondFile.Name())
@@ -1257,6 +1257,7 @@ components:
cf := CreateOpenAPIIndexConfig() cf := CreateOpenAPIIndexConfig()
cf.IgnorePolymorphicCircularReferences = true cf.IgnorePolymorphicCircularReferences = true
cf.ExtractRefsSequentially = true
rolodex := NewRolodex(cf) rolodex := NewRolodex(cf)
baseDir := tmp baseDir := tmp