mirror of
https://github.com/LukeHagar/libopenapi.git
synced 2025-12-09 20:47:44 +00:00
added more coverage and resolved param ref issue
Signed-off-by: quobix <dave@quobix.com>
This commit is contained in:
@@ -164,13 +164,10 @@ func LocateRefNodeWithContext(ctx context.Context, root *yaml.Node, idx *index.S
|
|||||||
u.Path = abs
|
u.Path = abs
|
||||||
rv = u.String()
|
rv = u.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
foundRef, fIdx, newCtx := idx.SearchIndexForReferenceWithContext(ctx, rv)
|
foundRef, fIdx, newCtx := idx.SearchIndexForReferenceWithContext(ctx, rv)
|
||||||
|
|||||||
@@ -915,6 +915,29 @@ func TestExtractArray_BadBuild(t *testing.T) {
|
|||||||
assert.Len(t, things, 0)
|
assert.Len(t, things, 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestExtractArray_BadRefPropsTupe(t *testing.T) {
|
||||||
|
|
||||||
|
yml := `components:
|
||||||
|
parameters:
|
||||||
|
cakes:
|
||||||
|
limes: cake`
|
||||||
|
|
||||||
|
var idxNode yaml.Node
|
||||||
|
mErr := yaml.Unmarshal([]byte(yml), &idxNode)
|
||||||
|
assert.NoError(t, mErr)
|
||||||
|
idx := index.NewSpecIndexWithConfig(&idxNode, index.CreateClosedAPIIndexConfig())
|
||||||
|
|
||||||
|
yml = `limes:
|
||||||
|
$ref: '#/components/parameters/cakes'`
|
||||||
|
|
||||||
|
var cNode yaml.Node
|
||||||
|
e := yaml.Unmarshal([]byte(yml), &cNode)
|
||||||
|
assert.NoError(t, e)
|
||||||
|
things, _, _, err := ExtractArray[*test_noGood](context.Background(), "limes", cNode.Content[0], idx)
|
||||||
|
assert.Error(t, err)
|
||||||
|
assert.Len(t, things, 0)
|
||||||
|
}
|
||||||
|
|
||||||
func TestExtractExample_String(t *testing.T) {
|
func TestExtractExample_String(t *testing.T) {
|
||||||
yml := `hi`
|
yml := `hi`
|
||||||
var e yaml.Node
|
var e yaml.Node
|
||||||
@@ -1573,6 +1596,17 @@ func TestExtractMapFlat_Ref_Bad(t *testing.T) {
|
|||||||
assert.Len(t, things, 0)
|
assert.Len(t, things, 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
'/companies/{companyId}/data/payments/{paymentId}':
|
||||||
|
parameters:
|
||||||
|
- $ref: '#/components/parameters/companyId'
|
||||||
|
get:
|
||||||
|
tags:
|
||||||
|
- Accounts receivable
|
||||||
|
parameters:
|
||||||
|
- $ref: '#/paths/~1companies~1%7BcompanyId%7D~1connections~1%7BconnectionId%7D~1data~1commerce-payments~1%7BpaymentId%7D/parameters/2'
|
||||||
|
*/
|
||||||
|
|
||||||
func TestExtractExtensions(t *testing.T) {
|
func TestExtractExtensions(t *testing.T) {
|
||||||
|
|
||||||
yml := `x-bing: ding
|
yml := `x-bing: ding
|
||||||
|
|||||||
@@ -557,23 +557,20 @@ func (index *SpecIndex) ExtractComponentsFromRefs(refs []*Reference) []*Referenc
|
|||||||
located := index.FindComponent(ref.FullDefinition)
|
located := index.FindComponent(ref.FullDefinition)
|
||||||
if located != nil {
|
if located != nil {
|
||||||
|
|
||||||
index.refLock.Lock()
|
|
||||||
// have we already mapped this?
|
// have we already mapped this?
|
||||||
|
index.refLock.Lock()
|
||||||
if index.allMappedRefs[ref.FullDefinition] == nil {
|
if index.allMappedRefs[ref.FullDefinition] == nil {
|
||||||
found = append(found, located)
|
found = append(found, located)
|
||||||
if located.FullDefinition != ref.FullDefinition {
|
index.allMappedRefs[located.FullDefinition] = located
|
||||||
located.FullDefinition = ref.FullDefinition
|
|
||||||
}
|
|
||||||
|
|
||||||
index.allMappedRefs[ref.FullDefinition] = located
|
|
||||||
rm := &ReferenceMapped{
|
rm := &ReferenceMapped{
|
||||||
Reference: located,
|
Reference: located,
|
||||||
Definition: ref.Definition,
|
Definition: located.Definition,
|
||||||
FullDefinition: ref.FullDefinition,
|
FullDefinition: located.FullDefinition,
|
||||||
}
|
}
|
||||||
sequence[refIndex] = rm
|
sequence[refIndex] = rm
|
||||||
}
|
}
|
||||||
index.refLock.Unlock()
|
index.refLock.Unlock()
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
_, path := utils.ConvertComponentIdIntoFriendlyPathSearch(ref.Definition)
|
_, path := utils.ConvertComponentIdIntoFriendlyPathSearch(ref.Definition)
|
||||||
|
|||||||
@@ -539,7 +539,11 @@ func (resolver *Resolver) extractRelatives(ref *Reference, node, parent *yaml.No
|
|||||||
}
|
}
|
||||||
|
|
||||||
if resolve {
|
if resolve {
|
||||||
ref.Node = locatedRef.Node
|
// if this is a reference also, we want to resolve it.
|
||||||
|
if ok, _, _ := utils.IsNodeRefValue(ref.Node); ok {
|
||||||
|
ref.Node = locatedRef.Node
|
||||||
|
ref.Resolved = true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
schemaType := ""
|
schemaType := ""
|
||||||
|
|||||||
@@ -536,6 +536,30 @@ components:
|
|||||||
assert.Equal(t, "cannot resolve reference `go home, I am drunk`, it's missing: $go home, I am drunk [18:11]", err[0].Error())
|
assert.Equal(t, "cannot resolve reference `go home, I am drunk`, it's missing: $go home, I am drunk [18:11]", err[0].Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestResolver_ResolveThroughPaths(t *testing.T) {
|
||||||
|
yml := `paths:
|
||||||
|
/pizza/{cake}/{pizza}/pie:
|
||||||
|
parameters:
|
||||||
|
- name: juicy
|
||||||
|
/companies/{companyId}/data/payments/{paymentId}:
|
||||||
|
get:
|
||||||
|
tags:
|
||||||
|
- Accounts receivable
|
||||||
|
parameters:
|
||||||
|
- $ref: '#/paths/~1pizza~1%7Bcake%7D~1%7Bpizza%7D~1pie/parameters/0'`
|
||||||
|
|
||||||
|
var rootNode yaml.Node
|
||||||
|
_ = yaml.Unmarshal([]byte(yml), &rootNode)
|
||||||
|
|
||||||
|
idx := NewSpecIndexWithConfig(&rootNode, CreateClosedAPIIndexConfig())
|
||||||
|
|
||||||
|
resolver := NewResolver(idx)
|
||||||
|
assert.NotNil(t, resolver)
|
||||||
|
|
||||||
|
err := resolver.Resolve()
|
||||||
|
assert.Len(t, err, 0)
|
||||||
|
}
|
||||||
|
|
||||||
func TestResolver_ResolveComponents_MixedRef(t *testing.T) {
|
func TestResolver_ResolveComponents_MixedRef(t *testing.T) {
|
||||||
mixedref, _ := os.ReadFile("../test_specs/mixedref-burgershop.openapi.yaml")
|
mixedref, _ := os.ReadFile("../test_specs/mixedref-burgershop.openapi.yaml")
|
||||||
var rootNode yaml.Node
|
var rootNode yaml.Node
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ package index
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/url"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
@@ -89,6 +90,10 @@ func (index *SpecIndex) SearchIndexForReferenceByReferenceWithContext(ctx contex
|
|||||||
}
|
}
|
||||||
ref = uri[0]
|
ref = uri[0]
|
||||||
}
|
}
|
||||||
|
if strings.Contains(ref, "%") {
|
||||||
|
// decode the url.
|
||||||
|
ref, _ = url.QueryUnescape(ref)
|
||||||
|
}
|
||||||
|
|
||||||
if r, ok := index.allMappedRefs[ref]; ok {
|
if r, ok := index.allMappedRefs[ref]; ok {
|
||||||
index.cache.Store(ref, r)
|
index.cache.Store(ref, r)
|
||||||
@@ -164,16 +169,6 @@ func (index *SpecIndex) SearchIndexForReferenceByReferenceWithContext(ctx contex
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
if r, ok := index.allMappedRefs[ref]; ok {
|
|
||||||
index.cache.Store(ref, r)
|
|
||||||
return r, r.Index, context.WithValue(ctx, CurrentPathKey, r.RemoteLocation)
|
|
||||||
}
|
|
||||||
|
|
||||||
if r, ok := index.allMappedRefs[refAlt]; ok {
|
|
||||||
index.cache.Store(refAlt, r)
|
|
||||||
return r, r.Index, context.WithValue(ctx, CurrentPathKey, r.RemoteLocation)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if index.logger != nil {
|
if index.logger != nil {
|
||||||
|
|||||||
@@ -367,6 +367,9 @@ func (index *SpecIndex) scanOperationParams(params []*yaml.Node, pathItemNode *y
|
|||||||
ref := seekRefEnd(index, paramRefName)
|
ref := seekRefEnd(index, paramRefName)
|
||||||
if ref != nil {
|
if ref != nil {
|
||||||
paramRef = ref
|
paramRef = ref
|
||||||
|
if strings.Contains(paramRefName, "%") {
|
||||||
|
paramRefName, _ = url.QueryUnescape(paramRefName)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user