added more coverage and resolved param ref issue

Signed-off-by: quobix <dave@quobix.com>
This commit is contained in:
quobix
2023-11-03 18:25:27 -04:00
parent fde5a9972d
commit 7d8762fdd9
7 changed files with 76 additions and 22 deletions

View File

@@ -164,13 +164,10 @@ func LocateRefNodeWithContext(ctx context.Context, root *yaml.Node, idx *index.S
u.Path = abs
rv = u.String()
}
}
}
}
}
}
foundRef, fIdx, newCtx := idx.SearchIndexForReferenceWithContext(ctx, rv)

View File

@@ -915,6 +915,29 @@ func TestExtractArray_BadBuild(t *testing.T) {
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) {
yml := `hi`
var e yaml.Node
@@ -1573,6 +1596,17 @@ func TestExtractMapFlat_Ref_Bad(t *testing.T) {
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) {
yml := `x-bing: ding

View File

@@ -557,23 +557,20 @@ func (index *SpecIndex) ExtractComponentsFromRefs(refs []*Reference) []*Referenc
located := index.FindComponent(ref.FullDefinition)
if located != nil {
index.refLock.Lock()
// have we already mapped this?
index.refLock.Lock()
if index.allMappedRefs[ref.FullDefinition] == nil {
found = append(found, located)
if located.FullDefinition != ref.FullDefinition {
located.FullDefinition = ref.FullDefinition
}
index.allMappedRefs[ref.FullDefinition] = located
index.allMappedRefs[located.FullDefinition] = located
rm := &ReferenceMapped{
Reference: located,
Definition: ref.Definition,
FullDefinition: ref.FullDefinition,
Definition: located.Definition,
FullDefinition: located.FullDefinition,
}
sequence[refIndex] = rm
}
index.refLock.Unlock()
} else {
_, path := utils.ConvertComponentIdIntoFriendlyPathSearch(ref.Definition)

View File

@@ -539,7 +539,11 @@ func (resolver *Resolver) extractRelatives(ref *Reference, node, parent *yaml.No
}
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 := ""

View File

@@ -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())
}
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) {
mixedref, _ := os.ReadFile("../test_specs/mixedref-burgershop.openapi.yaml")
var rootNode yaml.Node

View File

@@ -6,6 +6,7 @@ package index
import (
"context"
"fmt"
"net/url"
"path/filepath"
"strings"
)
@@ -89,6 +90,10 @@ func (index *SpecIndex) SearchIndexForReferenceByReferenceWithContext(ctx contex
}
ref = uri[0]
}
if strings.Contains(ref, "%") {
// decode the url.
ref, _ = url.QueryUnescape(ref)
}
if r, ok := index.allMappedRefs[ref]; ok {
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 {

View File

@@ -367,6 +367,9 @@ func (index *SpecIndex) scanOperationParams(params []*yaml.Node, pathItemNode *y
ref := seekRefEnd(index, paramRefName)
if ref != nil {
paramRef = ref
if strings.Contains(paramRefName, "%") {
paramRefName, _ = url.QueryUnescape(paramRefName)
}
}
}