Skip to content

Commit

Permalink
fix: issue-280 Index.GetAllSchemas() regression in values returned
Browse files Browse the repository at this point in the history
  • Loading branch information
TristanSpeakEasy authored and daveshanley committed Apr 30, 2024
1 parent 7e86e99 commit 9d1ca6f
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 29 deletions.
47 changes: 21 additions & 26 deletions index/extract_refs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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])
Expand All @@ -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)))
Expand All @@ -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])
Expand All @@ -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
Expand Down Expand Up @@ -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++
}
}
}

Expand Down Expand Up @@ -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
}

Expand Down
56 changes: 53 additions & 3 deletions index/spec_index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -1542,7 +1542,6 @@ paths:
}

func TestSpecIndex_TestInlineSchemaPaths(t *testing.T) {

yml := `openapi: 3.1.0
info:
title: Test
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 9d1ca6f

Please sign in to comment.