Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Properties named ‘enum’ were being indexed as enums #169

Merged
merged 2 commits into from
Sep 16, 2023
Merged
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
6 changes: 6 additions & 0 deletions index/extract_refs.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,12 @@ func (index *SpecIndex) ExtractRefs(node, parent *yaml.Node, seenPath []string,
// capture enums
if n.Value == "enum" {

if len(seenPath) > 0 {
lastItem := seenPath[len(seenPath)-1]
if lastItem == "properties" {
continue
}
}
// all enums need to have a type, extract the type from the node where the enum was found.
_, enumKeyValueNode := utils.FindKeyNodeTop("type", node.Content)

Expand Down
38 changes: 38 additions & 0 deletions index/extract_refs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,41 @@ components:
assert.Len(t, idx.allMappedRefs, 1)
assert.Equal(t, "Cake[Burger]", idx.allMappedRefs["#/components/schemas/Cake[Burger]"].Name)
}

// https://github.com/daveshanley/vacuum/issues/339
func TestSpecIndex_ExtractRefs_CheckEnumNotPropertyCalledEnum(t *testing.T) {

yml := `openapi: 3.0.0
components:
schemas:
SimpleFieldSchema:
description: Schema of a field as described in JSON Schema draft 2019-09
type: object
required:
- type
- description
properties:
type:
type: string
enum:
- string
- number
description:
type: string
description: A description of the property
enum:
type: array
description: A array of describing the possible values
items:
type: string
example:
- yo
- hello
`
var rootNode yaml.Node
_ = yaml.Unmarshal([]byte(yml), &rootNode)
c := CreateOpenAPIIndexConfig()
idx := NewSpecIndexWithConfig(&rootNode, c)
assert.Len(t, idx.allEnums, 1)

}