Skip to content

Commit

Permalink
bumping test coverage
Browse files Browse the repository at this point in the history
more to go, more cleaning inbound also

Signed-off-by: quobix <dave@quobix.com>
  • Loading branch information
daveshanley committed Oct 25, 2023
1 parent b82b46e commit a87d923
Show file tree
Hide file tree
Showing 11 changed files with 238 additions and 58 deletions.
4 changes: 0 additions & 4 deletions datamodel/low/serializing.go

This file was deleted.

2 changes: 1 addition & 1 deletion index/extract_refs.go
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,7 @@ func (index *SpecIndex) ExtractComponentsFromRefs(refs []*Reference) []*Referenc
c := make(chan bool)

locate := func(ref *Reference, refIndex int, sequence []*ReferenceMapped) {
located := index.FindComponent(ref.FullDefinition, ref.Node)
located := index.FindComponent(ref.FullDefinition)
if located != nil {

index.refLock.Lock()
Expand Down
12 changes: 4 additions & 8 deletions index/find_component.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
// FindComponent will locate a component by its reference, returns nil if nothing is found.
// This method will recurse through remote, local and file references. For each new external reference
// a new index will be created. These indexes can then be traversed recursively.
func (index *SpecIndex) FindComponent(componentId string, parent *yaml.Node) *Reference {
func (index *SpecIndex) FindComponent(componentId string) *Reference {
if index.root == nil {
return nil
}
Expand All @@ -43,7 +43,6 @@ func (index *SpecIndex) FindComponent(componentId string, parent *yaml.Node) *Re

// root search
return index.FindComponentInRoot(componentId)

}
}

Expand All @@ -55,6 +54,9 @@ func FindComponent(root *yaml.Node, componentId, absoluteFilePath string, index
}

name, friendlySearch := utils.ConvertComponentIdIntoFriendlyPathSearch(componentId)
if friendlySearch == "$." {
friendlySearch = "$"
}
path, err := yamlpath.NewPath(friendlySearch)
if path == nil || err != nil {
return nil // no component found
Expand All @@ -63,12 +65,7 @@ func FindComponent(root *yaml.Node, componentId, absoluteFilePath string, index

if len(res) == 1 {
resNode := res[0]
if res[0].Kind == yaml.DocumentNode {
resNode = res[0].Content[0]
}

fullDef := fmt.Sprintf("%s%s", absoluteFilePath, componentId)

// extract properties
ref := &Reference{
FullDefinition: fullDef,
Expand All @@ -80,7 +77,6 @@ func FindComponent(root *yaml.Node, componentId, absoluteFilePath string, index
Index: index,
RequiredRefProperties: extractDefinitionRequiredRefProperties(resNode, map[string][]string{}, fullDef),
}

return ref
}
return nil
Expand Down
84 changes: 84 additions & 0 deletions index/find_component_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,3 +253,87 @@ paths:
index := NewSpecIndexWithConfig(&rootNode, c)
assert.Len(t, index.GetReferenceIndexErrors(), 1)
}

func TestFindComponent_LookupRolodex_GrabRoot(t *testing.T) {

spec := `openapi: 3.0.2
info:
title: Test
version: 1.0.0
components:
schemas:
thang:
type: object
`

var rootNode yaml.Node
_ = yaml.Unmarshal([]byte(spec), &rootNode)

c := CreateOpenAPIIndexConfig()

index := NewSpecIndexWithConfig(&rootNode, c)
r := NewRolodex(c)
index.rolodex = r

n := index.lookupRolodex([]string{"bingobango"})

// if the reference is not found, it should return the root.
assert.NotNil(t, n)

}

func TestFindComponentInRoot_GrabDocRoot(t *testing.T) {

spec := `openapi: 3.0.2
info:
title: Test
version: 1.0.0
components:
schemas:
thang:
type: object
`

var rootNode yaml.Node
_ = yaml.Unmarshal([]byte(spec), &rootNode)

c := CreateOpenAPIIndexConfig()

index := NewSpecIndexWithConfig(&rootNode, c)
r := NewRolodex(c)
index.rolodex = r

n := index.FindComponentInRoot("#/")

// if the reference is not found, it should return the root.
assert.NotNil(t, n)

}

func TestFindComponent_LookupRolodex_NoURL(t *testing.T) {

spec := `openapi: 3.0.2
info:
title: Test
version: 1.0.0
components:
schemas:
thang:
type: object
`

var rootNode yaml.Node
_ = yaml.Unmarshal([]byte(spec), &rootNode)

c := CreateOpenAPIIndexConfig()

index := NewSpecIndexWithConfig(&rootNode, c)
r := NewRolodex(c)
index.rolodex = r

n := index.lookupRolodex(nil)

// no url, no ref.
assert.Nil(t, n)

}
51 changes: 16 additions & 35 deletions index/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ func (resolver *Resolver) VisitReference(ref *Reference, seen map[string]bool, j
seen = make(map[string]bool)

seen[ref.Definition] = true
for i, r := range relatives {
for _, r := range relatives {
// check if we have seen this on the journey before, if so! it's circular
skip := false
for i, j := range journey {
Expand Down Expand Up @@ -311,10 +311,6 @@ func (resolver *Resolver) VisitReference(ref *Reference, seen map[string]bool, j
if foundRef != nil {
original = foundRef
}
if original == nil {
panic(i)
}

resolved := resolver.VisitReference(original, seen, journey, resolve)
if resolve && !original.Circular {
r.Node.Content = resolved // this is where we perform the actual resolving.
Expand Down Expand Up @@ -422,47 +418,32 @@ func (resolver *Resolver) extractRelatives(ref *Reference, node, parent *yaml.No
}
} else {

if strings.HasPrefix(exp[0], "http") {
fullDef = value // remote component, full def is based on value

} else {

if filepath.IsAbs(value) {
fullDef = value
} else {

// local component, full def is based on passed in ref
if strings.HasPrefix(ref.FullDefinition, "http") {

// split the http URI into parts
httpExp := strings.Split(ref.FullDefinition, "#/")

// parse an URL from the full def
u, _ := url.Parse(httpExp[0])

// extract the location of the ref and build a full def path.
fullDef = fmt.Sprintf("%s#/%s", u.String(), exp[1])
// local component, full def is based on passed in ref
if strings.HasPrefix(ref.FullDefinition, "http") {

} else {
// split the http URI into parts
httpExp := strings.Split(ref.FullDefinition, "#/")

// split the full def into parts
fileDef := strings.Split(ref.FullDefinition, "#/")
// parse a URL from the full def
u, _ := url.Parse(httpExp[0])

// extract the location of the ref and build a full def path.
//loc, _ := filepath.Abs(fileDef[0]), exp[1]))
// extract the location of the ref and build a full def path.
fullDef = fmt.Sprintf("%s#/%s", u.String(), exp[1])

fullDef = fmt.Sprintf("%s#/%s", fileDef[0], exp[1])
} else {

}
// split the full def into parts
fileDef := strings.Split(ref.FullDefinition, "#/")
fullDef = fmt.Sprintf("%s#/%s", fileDef[0], exp[1])

}
}

}
} else {

definition = value

// if the reference is an http link
// if the reference is a http link
if strings.HasPrefix(value, "http") {
fullDef = value
} else {
Expand All @@ -474,7 +455,7 @@ func (resolver *Resolver) extractRelatives(ref *Reference, node, parent *yaml.No
// split the full def into parts
fileDef := strings.Split(ref.FullDefinition, "#/")

// is the file def an http link?
// is the file def a http link?
if strings.HasPrefix(fileDef[0], "http") {

u, _ := url.Parse(fileDef[0])
Expand Down
109 changes: 109 additions & 0 deletions index/resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -662,3 +662,112 @@ func ExampleResolvingError() {
fmt.Printf("%s", re.Error())
// Output: je suis une erreur: #/definitions/JeSuisUneErreur [5:21]
}

func TestDocument_IgnoreArrayCircularReferences(t *testing.T) {

var d = `openapi: 3.1.0
components:
schemas:
ProductCategory:
type: "object"
properties:
name:
type: "string"
children:
type: "array"
items:
$ref: "#/components/schemas/ProductCategory"
description: "Array of sub-categories in the same format."
required:
- "name"
- "children"`

var rootNode yaml.Node
_ = yaml.Unmarshal([]byte(d), &rootNode)

idx := NewSpecIndexWithConfig(&rootNode, CreateClosedAPIIndexConfig())

resolver := NewResolver(idx)
resolver.IgnoreArrayCircularReferences()
assert.NotNil(t, resolver)

circ := resolver.Resolve()
assert.Len(t, circ, 0)
assert.Len(t, resolver.GetIgnoredCircularArrayReferences(), 1)

}

func TestDocument_IgnorePolyCircularReferences(t *testing.T) {

var d = `openapi: 3.1.0
components:
schemas:
ProductCategory:
type: "object"
properties:
name:
type: "string"
children:
type: "object"
anyOf:
- $ref: "#/components/schemas/ProductCategory"
description: "Array of sub-categories in the same format."
required:
- "name"
- "children"`

var rootNode yaml.Node
_ = yaml.Unmarshal([]byte(d), &rootNode)

idx := NewSpecIndexWithConfig(&rootNode, CreateClosedAPIIndexConfig())

resolver := NewResolver(idx)
resolver.IgnorePolymorphicCircularReferences()
assert.NotNil(t, resolver)

circ := resolver.Resolve()
assert.Len(t, circ, 0)
assert.Len(t, resolver.GetIgnoredCircularPolyReferences(), 1)

}

func TestDocument_IgnorePolyCircularReferences_NoArrayForRef(t *testing.T) {

var d = `openapi: 3.1.0
components:
schemas:
bingo:
type: object
properties:
bango:
$ref: "#/components/schemas/ProductCategory"
ProductCategory:
type: "object"
properties:
name:
type: "string"
children:
type: "object"
items:
anyOf:
items:
$ref: "#/components/schemas/ProductCategory"
description: "Array of sub-categories in the same format."
required:
- "name"
- "children"`

var rootNode yaml.Node
_ = yaml.Unmarshal([]byte(d), &rootNode)

idx := NewSpecIndexWithConfig(&rootNode, CreateClosedAPIIndexConfig())

resolver := NewResolver(idx)
resolver.IgnorePolymorphicCircularReferences()
assert.NotNil(t, resolver)

circ := resolver.Resolve()
assert.Len(t, circ, 0)
assert.Len(t, resolver.GetIgnoredCircularPolyReferences(), 1)

}
2 changes: 2 additions & 0 deletions index/rolodex.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,10 +174,12 @@ func (rf *rolodexFile) Size() int64 {
}

func (rf *rolodexFile) IsDir() bool {
// always false.
return false
}

func (rf *rolodexFile) Sys() interface{} {
// not implemented.
return nil
}

Expand Down
5 changes: 0 additions & 5 deletions index/rolodex_remote_loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,11 +293,6 @@ func (i *RemoteFS) Open(remoteURL string) (fs.File, error) {

i.logger.Debug("loading remote file", "file", remoteURL, "remoteURL", remoteParsedURL.String())

//// no handler func? use the default client.
//if i.RemoteHandlerFunc == nil {
// i.RemoteHandlerFunc = i.defaultClient.Get
//}

response, clientErr := i.RemoteHandlerFunc(remoteParsedURL.String())
if clientErr != nil {

Expand Down
Loading

0 comments on commit a87d923

Please sign in to comment.