From 4a378de02e9e868e2cf4835f36c4f09e6a34cf69 Mon Sep 17 00:00:00 2001 From: Dave Shanley Date: Mon, 27 Feb 2023 10:10:31 -0500 Subject: [PATCH] Fix: bug in index with description handling. Issue for description being incorrectly picked up, is because index was collecting all descriptions, even if they were labels on schemas as reported in https://github.com/daveshanley/vacuum/issues/239 --- index/extract_refs.go | 6 ++++-- index/extract_refs_test.go | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 index/extract_refs_test.go diff --git a/index/extract_refs.go b/index/extract_refs.go index bb79e6c..1f71ae2 100644 --- a/index/extract_refs.go +++ b/index/extract_refs.go @@ -177,8 +177,10 @@ func (index *SpecIndex) ExtractRefs(node, parent *yaml.Node, seenPath []string, IsSummary: false, } - index.allDescriptions = append(index.allDescriptions, ref) - index.descriptionCount++ + if !utils.IsNodeMap(ref.Node) { + index.allDescriptions = append(index.allDescriptions, ref) + index.descriptionCount++ + } } if n.Value == "summary" { diff --git a/index/extract_refs_test.go b/index/extract_refs_test.go new file mode 100644 index 0000000..12b8e9d --- /dev/null +++ b/index/extract_refs_test.go @@ -0,0 +1,37 @@ +// Copyright 2023 Princess B33f Heavy Industries / Dave Shanley +// SPDX-License-Identifier: MIT + +package index + +import ( + "github.com/stretchr/testify/assert" + "gopkg.in/yaml.v3" + "testing" +) + +func TestSpecIndex_ExtractRefs_CheckDescriptionNotMap(t *testing.T) { + + yml := `openapi: 3.1.0 +info: + description: This is a description +paths: + /herbs/and/spice: + get: + description: This is a also a description + responses: + 200: + content: + application/json: + schema: + type: array + properties: + description: + type: string + ` + var rootNode yaml.Node + _ = yaml.Unmarshal([]byte(yml), &rootNode) + c := CreateOpenAPIIndexConfig() + idx := NewSpecIndexWithConfig(&rootNode, c) + assert.Len(t, idx.allDescriptions, 2) + assert.Equal(t, 2, idx.descriptionCount) +}