From fcc12cb7dc403e2ac3193735d9a79d6ee19007d9 Mon Sep 17 00:00:00 2001 From: quobix Date: Sat, 16 Sep 2023 09:36:07 -0400 Subject: [PATCH] =?UTF-8?q?Properties=20named=20=E2=80=98enum=E2=80=99=20w?= =?UTF-8?q?ere=20being=20indexed=20as=20enums?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit An edge case reported in https://github.com/daveshanley/vacuum/issues/339 shows up when enums are being indexed when they are actually a property of a schema called ‘enum’. This has been fixed. Signed-off-by: quobix --- index/extract_refs.go | 5 +++++ index/extract_refs_test.go | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/index/extract_refs.go b/index/extract_refs.go index 637c3b5..e8e7323 100644 --- a/index/extract_refs.go +++ b/index/extract_refs.go @@ -329,6 +329,11 @@ func (index *SpecIndex) ExtractRefs(node, parent *yaml.Node, seenPath []string, // capture enums if n.Value == "enum" { + 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) diff --git a/index/extract_refs_test.go b/index/extract_refs_test.go index afdc492..41b9ce0 100644 --- a/index/extract_refs_test.go +++ b/index/extract_refs_test.go @@ -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) + +}