fix: issue-280 Index.GetAllSchemas() regression in values returned

This commit is contained in:
Tristan Cartledge
2024-04-30 00:41:30 +01:00
committed by quobix
parent 7e86e991b2
commit 9d1ca6f541
2 changed files with 74 additions and 29 deletions

View File

@@ -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,10 +441,7 @@ 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
}
if !slices.Contains(seenPath, "example") && !slices.Contains(seenPath, "examples") {
ref := &DescriptionReference{
ParentNode: parent,
Content: node.Content[i+1].Value,
@@ -462,6 +456,7 @@ func (index *SpecIndex) ExtractRefs(node, parent *yaml.Node, seenPath []string,
index.descriptionCount++
}
}
}
if n.Value == "summary" {
@@ -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
}

View File

@@ -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: