mirror of
https://github.com/LukeHagar/libopenapi.git
synced 2025-12-06 12:37:49 +00:00
fix: issue-280 Index.GetAllSchemas() regression in values returned
This commit is contained in:
committed by
quobix
parent
7e86e991b2
commit
9d1ca6f541
@@ -6,13 +6,14 @@ package index
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/pb33f/libopenapi/utils"
|
|
||||||
"golang.org/x/exp/slices"
|
|
||||||
"gopkg.in/yaml.v3"
|
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"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.
|
// 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])
|
fullDefinitionPath = fmt.Sprintf("%s#/%s", index.specAbsolutePath, uri[1])
|
||||||
componentName = value
|
componentName = value
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
if strings.HasPrefix(uri[0], "http") {
|
if strings.HasPrefix(uri[0], "http") {
|
||||||
fullDefinitionPath = value
|
fullDefinitionPath = value
|
||||||
componentName = fmt.Sprintf("#/%s", uri[1])
|
componentName = fmt.Sprintf("#/%s", uri[1])
|
||||||
@@ -257,7 +257,6 @@ func (index *SpecIndex) ExtractRefs(node, parent *yaml.Node, seenPath []string,
|
|||||||
fullDefinitionPath = value
|
fullDefinitionPath = value
|
||||||
componentName = fmt.Sprintf("#/%s", uri[1])
|
componentName = fmt.Sprintf("#/%s", uri[1])
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
// if the index has a base path, use that to resolve the path
|
// if the index has a base path, use that to resolve the path
|
||||||
if index.config.BasePath != "" && index.config.BaseURL == nil {
|
if index.config.BasePath != "" && index.config.BaseURL == nil {
|
||||||
abs, _ := filepath.Abs(utils.CheckPathOverlap(index.config.BasePath, uri[0], string(os.PathSeparator)))
|
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 {
|
} else {
|
||||||
u = *index.config.BaseURL
|
u = *index.config.BaseURL
|
||||||
}
|
}
|
||||||
//abs, _ := filepath.Abs(filepath.Join(u.Path, uri[0]))
|
// abs, _ := filepath.Abs(filepath.Join(u.Path, uri[0]))
|
||||||
//abs, _ := filepath.Abs(utils.CheckPathOverlap(u.Path, uri[0], string(os.PathSeparator)))
|
// abs, _ := filepath.Abs(utils.CheckPathOverlap(u.Path, uri[0], string(os.PathSeparator)))
|
||||||
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)
|
u.Path = utils.ReplaceWindowsDriveWithLinuxPath(abs)
|
||||||
fullDefinitionPath = fmt.Sprintf("%s#/%s", u.String(), uri[1])
|
fullDefinitionPath = fmt.Sprintf("%s#/%s", u.String(), uri[1])
|
||||||
@@ -293,19 +292,17 @@ func (index *SpecIndex) ExtractRefs(node, parent *yaml.Node, seenPath []string,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
if strings.HasPrefix(uri[0], "http") {
|
if strings.HasPrefix(uri[0], "http") {
|
||||||
fullDefinitionPath = value
|
fullDefinitionPath = value
|
||||||
} else {
|
} else {
|
||||||
// is it a relative file include?
|
// is it a relative file include?
|
||||||
if !strings.Contains(uri[0], "#") {
|
if !strings.Contains(uri[0], "#") {
|
||||||
|
|
||||||
if strings.HasPrefix(defRoot, "http") {
|
if strings.HasPrefix(defRoot, "http") {
|
||||||
if !filepath.IsAbs(uri[0]) {
|
if !filepath.IsAbs(uri[0]) {
|
||||||
u, _ := url.Parse(defRoot)
|
u, _ := url.Parse(defRoot)
|
||||||
pathDir := filepath.Dir(u.Path)
|
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, _ := filepath.Abs(utils.CheckPathOverlap(pathDir, uri[0], string(os.PathSeparator)))
|
||||||
pathAbs = utils.ReplaceWindowsDriveWithLinuxPath(pathAbs)
|
pathAbs = utils.ReplaceWindowsDriveWithLinuxPath(pathAbs)
|
||||||
u.Path = pathAbs
|
u.Path = pathAbs
|
||||||
@@ -444,22 +441,20 @@ func (index *SpecIndex) ExtractRefs(node, parent *yaml.Node, seenPath []string,
|
|||||||
if utils.IsNodeArray(node) {
|
if utils.IsNodeArray(node) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if slices.Contains(seenPath, "example") || slices.Contains(seenPath, "examples") {
|
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,
|
||||||
|
}
|
||||||
|
|
||||||
ref := &DescriptionReference{
|
if !utils.IsNodeMap(ref.Node) {
|
||||||
ParentNode: parent,
|
index.allDescriptions = append(index.allDescriptions, ref)
|
||||||
Content: node.Content[i+1].Value,
|
index.descriptionCount++
|
||||||
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++
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -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, strings.ReplaceAll(n.Value, "/", "~1"))
|
||||||
//seenPath = append(seenPath, n.Value)
|
// seenPath = append(seenPath, n.Value)
|
||||||
prev = n.Value
|
prev = n.Value
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1109,7 +1109,7 @@ func TestSpecIndex_lookupFileReference_MultiRes(t *testing.T) {
|
|||||||
assert.NotNil(t, embieRoloFile)
|
assert.NotNil(t, embieRoloFile)
|
||||||
|
|
||||||
index := rolo.GetRootIndex()
|
index := rolo.GetRootIndex()
|
||||||
//index.seenRemoteSources = make(map[string]*yaml.Node)
|
// index.seenRemoteSources = make(map[string]*yaml.Node)
|
||||||
absoluteRef, _ := filepath.Abs("embie.yaml#/naughty")
|
absoluteRef, _ := filepath.Abs("embie.yaml#/naughty")
|
||||||
fRef, _ := index.SearchIndexForReference(absoluteRef)
|
fRef, _ := index.SearchIndexForReference(absoluteRef)
|
||||||
assert.NotNil(t, fRef)
|
assert.NotNil(t, fRef)
|
||||||
@@ -1542,7 +1542,6 @@ paths:
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestSpecIndex_TestInlineSchemaPaths(t *testing.T) {
|
func TestSpecIndex_TestInlineSchemaPaths(t *testing.T) {
|
||||||
|
|
||||||
yml := `openapi: 3.1.0
|
yml := `openapi: 3.1.0
|
||||||
info:
|
info:
|
||||||
title: Test
|
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", 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.code", schemas[1].Path)
|
||||||
assert.Equal(t, "$.paths['/test'].get.parameters.schema.properties.message", schemas[2].Path)
|
assert.Equal(t, "$.paths['/test'].get.parameters.schema.properties.message", schemas[2].Path)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSpecIndex_TestPathsAsRef(t *testing.T) {
|
func TestSpecIndex_TestPathsAsRef(t *testing.T) {
|
||||||
@@ -1717,6 +1715,58 @@ components:
|
|||||||
assert.Equal(t, 0, len(schemas))
|
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) {
|
func TestSpecIndex_Issue481(t *testing.T) {
|
||||||
yml := `openapi: 3.0.1
|
yml := `openapi: 3.0.1
|
||||||
components:
|
components:
|
||||||
|
|||||||
Reference in New Issue
Block a user