From 9b0dd0019af2860442f87bddd600ab1f164900d6 Mon Sep 17 00:00:00 2001 From: Tristan Cartledge Date: Tue, 30 Apr 2024 00:41:30 +0100 Subject: [PATCH] fix: issue-280 Index.GetAllSchemas() regression in values returned --- index/extract_refs.go | 47 +++++++++++++++------------------ index/spec_index_test.go | 56 +++++++++++++++++++++++++++++++++++++--- 2 files changed, 74 insertions(+), 29 deletions(-) diff --git a/index/extract_refs.go b/index/extract_refs.go index b20d30ad..deddc1ef 100644 --- a/index/extract_refs.go +++ b/index/extract_refs.go @@ -6,13 +6,14 @@ package index import ( "errors" "fmt" - "github.com/pb33f/libopenapi/utils" - "golang.org/x/exp/slices" - "gopkg.in/yaml.v3" "net/url" "os" "path/filepath" "strings" + + "github.com/pb33f/libopenapi/utils" + "golang.org/x/exp/slices" + "gopkg.in/yaml.v3" ) // ExtractRefs will return a deduplicated slice of references for every unique ref found in the document. @@ -248,7 +249,6 @@ func (index *SpecIndex) ExtractRefs(node, parent *yaml.Node, seenPath []string, fullDefinitionPath = fmt.Sprintf("%s#/%s", index.specAbsolutePath, uri[1]) componentName = value } else { - if strings.HasPrefix(uri[0], "http") { fullDefinitionPath = value componentName = fmt.Sprintf("#/%s", uri[1]) @@ -257,7 +257,6 @@ func (index *SpecIndex) ExtractRefs(node, parent *yaml.Node, seenPath []string, fullDefinitionPath = value componentName = fmt.Sprintf("#/%s", uri[1]) } else { - // if the index has a base path, use that to resolve the path if index.config.BasePath != "" && index.config.BaseURL == nil { abs, _ := filepath.Abs(utils.CheckPathOverlap(index.config.BasePath, uri[0], string(os.PathSeparator))) @@ -277,8 +276,8 @@ func (index *SpecIndex) ExtractRefs(node, parent *yaml.Node, seenPath []string, } else { u = *index.config.BaseURL } - //abs, _ := filepath.Abs(filepath.Join(u.Path, uri[0])) - //abs, _ := filepath.Abs(utils.CheckPathOverlap(u.Path, uri[0], string(os.PathSeparator))) + // abs, _ := filepath.Abs(filepath.Join(u.Path, uri[0])) + // abs, _ := filepath.Abs(utils.CheckPathOverlap(u.Path, uri[0], string(os.PathSeparator))) abs := utils.CheckPathOverlap(u.Path, uri[0], string(os.PathSeparator)) u.Path = utils.ReplaceWindowsDriveWithLinuxPath(abs) fullDefinitionPath = fmt.Sprintf("%s#/%s", u.String(), uri[1]) @@ -293,19 +292,17 @@ func (index *SpecIndex) ExtractRefs(node, parent *yaml.Node, seenPath []string, } } } - } else { if strings.HasPrefix(uri[0], "http") { fullDefinitionPath = value } else { // is it a relative file include? if !strings.Contains(uri[0], "#") { - if strings.HasPrefix(defRoot, "http") { if !filepath.IsAbs(uri[0]) { u, _ := url.Parse(defRoot) pathDir := filepath.Dir(u.Path) - //pathAbs, _ := filepath.Abs(filepath.Join(pathDir, uri[0])) + // pathAbs, _ := filepath.Abs(filepath.Join(pathDir, uri[0])) pathAbs, _ := filepath.Abs(utils.CheckPathOverlap(pathDir, uri[0], string(os.PathSeparator))) pathAbs = utils.ReplaceWindowsDriveWithLinuxPath(pathAbs) u.Path = pathAbs @@ -444,22 +441,20 @@ func (index *SpecIndex) ExtractRefs(node, parent *yaml.Node, seenPath []string, if utils.IsNodeArray(node) { continue } - if slices.Contains(seenPath, "example") || slices.Contains(seenPath, "examples") { - continue - } - - ref := &DescriptionReference{ - ParentNode: parent, - Content: node.Content[i+1].Value, - Path: jsonPath, - Node: node.Content[i+1], - KeyNode: node.Content[i], - IsSummary: false, - } + if !slices.Contains(seenPath, "example") && !slices.Contains(seenPath, "examples") { + ref := &DescriptionReference{ + ParentNode: parent, + Content: node.Content[i+1].Value, + Path: jsonPath, + Node: node.Content[i+1], + KeyNode: node.Content[i], + IsSummary: false, + } - if !utils.IsNodeMap(ref.Node) { - index.allDescriptions = append(index.allDescriptions, ref) - index.descriptionCount++ + if !utils.IsNodeMap(ref.Node) { + index.allDescriptions = append(index.allDescriptions, ref) + index.descriptionCount++ + } } } @@ -593,7 +588,7 @@ func (index *SpecIndex) ExtractRefs(node, parent *yaml.Node, seenPath []string, } seenPath = append(seenPath, strings.ReplaceAll(n.Value, "/", "~1")) - //seenPath = append(seenPath, n.Value) + // seenPath = append(seenPath, n.Value) prev = n.Value } diff --git a/index/spec_index_test.go b/index/spec_index_test.go index 9de7525a..cc29edbc 100644 --- a/index/spec_index_test.go +++ b/index/spec_index_test.go @@ -1109,7 +1109,7 @@ func TestSpecIndex_lookupFileReference_MultiRes(t *testing.T) { assert.NotNil(t, embieRoloFile) index := rolo.GetRootIndex() - //index.seenRemoteSources = make(map[string]*yaml.Node) + // index.seenRemoteSources = make(map[string]*yaml.Node) absoluteRef, _ := filepath.Abs("embie.yaml#/naughty") fRef, _ := index.SearchIndexForReference(absoluteRef) assert.NotNil(t, fRef) @@ -1542,7 +1542,6 @@ paths: } func TestSpecIndex_TestInlineSchemaPaths(t *testing.T) { - yml := `openapi: 3.1.0 info: title: Test @@ -1612,7 +1611,6 @@ paths: assert.Equal(t, "$.paths['/test'].get.parameters.schema", schemas[0].Path) assert.Equal(t, "$.paths['/test'].get.parameters.schema.properties.code", schemas[1].Path) assert.Equal(t, "$.paths['/test'].get.parameters.schema.properties.message", schemas[2].Path) - } func TestSpecIndex_TestPathsAsRef(t *testing.T) { @@ -1717,6 +1715,58 @@ components: assert.Equal(t, 0, len(schemas)) } +func TestSpecIndex_CheckIgnoreSchemaLikeObjectsInExamples(t *testing.T) { + yml := `openapi: 3.1.0 +paths: + '/test': + get: + responses: + '200': + content: + application/json: + schema: + type: object + examples: + test example: + value: + type: Object + description: test + properties: + lineItems: + type: Array + description: test + properties: + description: + required: false + taxRateRef: + type: Object + description: test + properties: + effectiveTaxRate: + type: Number + description: test + required: false + required: true + paymentAllocations: + type: Array + description: test + properties: + payment: + type: Object + description: test + properties: + accountRef: + type: Object` + + var rootNode yaml.Node + _ = yaml.Unmarshal([]byte(yml), &rootNode) + + index := NewSpecIndexWithConfig(&rootNode, CreateOpenAPIIndexConfig()) + schemas := index.GetAllSchemas() + + assert.Equal(t, 1, len(schemas)) +} + func TestSpecIndex_Issue481(t *testing.T) { yml := `openapi: 3.0.1 components: