fix: correctly handling of extracting enums for index

This commit is contained in:
Tristan Cartledge
2023-10-05 11:15:44 +00:00
committed by quobix
parent 2723ed974d
commit d4dabca04f
2 changed files with 16 additions and 5 deletions

View File

@@ -23,7 +23,6 @@ func (index *SpecIndex) ExtractRefs(node, parent *yaml.Node, seenPath []string,
if len(node.Content) > 0 { if len(node.Content) > 0 {
var prev, polyName string var prev, polyName string
for i, n := range node.Content { for i, n := range node.Content {
if utils.IsNodeMap(n) || utils.IsNodeArray(n) { if utils.IsNodeMap(n) || utils.IsNodeArray(n) {
level++ level++
// check if we're using polymorphic values. These tend to create rabbit warrens of circular // check if we're using polymorphic values. These tend to create rabbit warrens of circular
@@ -332,9 +331,12 @@ func (index *SpecIndex) ExtractRefs(node, parent *yaml.Node, seenPath []string,
if len(seenPath) > 0 { if len(seenPath) > 0 {
lastItem := seenPath[len(seenPath)-1] lastItem := seenPath[len(seenPath)-1]
if lastItem == "properties" { if lastItem == "properties" {
seenPath = append(seenPath, n.Value)
prev = n.Value
continue continue
} }
} }
// all enums need to have a type, extract the type from the node where the enum was found. // all enums need to have a type, extract the type from the node where the enum was found.
_, enumKeyValueNode := utils.FindKeyNodeTop("type", node.Content) _, enumKeyValueNode := utils.FindKeyNodeTop("type", node.Content)

View File

@@ -114,7 +114,6 @@ components:
// https://github.com/pb33f/libopenapi/issues/112 // https://github.com/pb33f/libopenapi/issues/112
func TestSpecIndex_ExtractRefs_CheckReferencesWithBracketsInName(t *testing.T) { func TestSpecIndex_ExtractRefs_CheckReferencesWithBracketsInName(t *testing.T) {
yml := `openapi: 3.0.0 yml := `openapi: 3.0.0
components: components:
schemas: schemas:
@@ -137,7 +136,6 @@ components:
// https://github.com/daveshanley/vacuum/issues/339 // https://github.com/daveshanley/vacuum/issues/339
func TestSpecIndex_ExtractRefs_CheckEnumNotPropertyCalledEnum(t *testing.T) { func TestSpecIndex_ExtractRefs_CheckEnumNotPropertyCalledEnum(t *testing.T) {
yml := `openapi: 3.0.0 yml := `openapi: 3.0.0
components: components:
schemas: schemas:
@@ -164,11 +162,22 @@ components:
example: example:
- yo - yo
- hello - hello
Schema2:
type: object
properties:
enumRef:
$ref: '#/components/schemas/enum'
enum:
type: string
enum: [big, small]
nullable: true
enum:
type: [string, null]
enum: [big, small]
` `
var rootNode yaml.Node var rootNode yaml.Node
_ = yaml.Unmarshal([]byte(yml), &rootNode) _ = yaml.Unmarshal([]byte(yml), &rootNode)
c := CreateOpenAPIIndexConfig() c := CreateOpenAPIIndexConfig()
idx := NewSpecIndexWithConfig(&rootNode, c) idx := NewSpecIndexWithConfig(&rootNode, c)
assert.Len(t, idx.allEnums, 1) assert.Len(t, idx.allEnums, 3)
} }