Skip to content

Commit

Permalink
Adding more use-cases for resolving remote docs
Browse files Browse the repository at this point in the history
Signed-off-by: quobix <dave@quobix.com>
  • Loading branch information
daveshanley committed Oct 30, 2023
1 parent d8dfafd commit aca3ed6
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 21 deletions.
44 changes: 24 additions & 20 deletions index/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -393,33 +393,38 @@ func (resolver *Resolver) extractRelatives(ref *Reference, node, parent *yaml.No
definition = fmt.Sprintf("#/%s", exp[1])
if exp[0] != "" {

if strings.HasPrefix(ref.FullDefinition, "http") {
if strings.HasPrefix(exp[0], "http") {
fullDef = value

Check warning on line 397 in index/resolver.go

View check run for this annotation

Codecov / codecov/patch

index/resolver.go#L397

Added line #L397 was not covered by tests
} else {

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

u, _ := url.Parse(httpExp[0])
abs, _ := filepath.Abs(filepath.Join(filepath.Dir(u.Path), exp[0]))
u.Path = abs
u.Fragment = ""
fullDef = fmt.Sprintf("%s#/%s", u.String(), exp[1])
// split the http URI into parts
httpExp := strings.Split(ref.FullDefinition, "#/")

} else {

if filepath.IsAbs(exp[0]) {
fullDef = value
u, _ := url.Parse(httpExp[0])
abs, _ := filepath.Abs(filepath.Join(filepath.Dir(u.Path), exp[0]))
u.Path = abs
u.Fragment = ""
fullDef = fmt.Sprintf("%s#/%s", u.String(), exp[1])

} else {

// split the referring ref full def into parts
fileDef := strings.Split(ref.FullDefinition, "#/")
if filepath.IsAbs(exp[0]) {
fullDef = value

Check warning on line 414 in index/resolver.go

View check run for this annotation

Codecov / codecov/patch

index/resolver.go#L414

Added line #L414 was not covered by tests

// extract the location of the ref and build a full def path.
abs, _ := filepath.Abs(filepath.Join(filepath.Dir(fileDef[0]), exp[0]))
fullDef = fmt.Sprintf("%s#/%s", abs, exp[1])
} else {

}
// split the referring ref full def into parts
fileDef := strings.Split(ref.FullDefinition, "#/")

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

}

}
}
} else {

Expand Down Expand Up @@ -606,7 +611,7 @@ func (resolver *Resolver) extractRelatives(ref *Reference, node, parent *yaml.No
}
}
} else {
panic("mummmmma mia")
def = l
}

} else {
Expand Down Expand Up @@ -648,7 +653,6 @@ func (resolver *Resolver) extractRelatives(ref *Reference, node, parent *yaml.No
}
}
}
//panic("oh no")
}

mappedRefs, _ := resolver.specIndex.SearchIndexForReference(def)
Expand Down
91 changes: 90 additions & 1 deletion index/rolodex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,96 @@ func test_rolodexDeepRefServer(a, b, c []byte) *httptest.Server {
}))
}

func TestRolodex_IndexCircularLookup_PolyHttpOnly(t *testing.T) {

second := `openapi: 3.1.0
components:
schemas:
CircleTest:
type: "object"
properties:
name:
type: "string"
children:
type: "object"
anyOf:
- $ref: "https://kjahsdkjahdkjashdas.com/first.yaml#/components/schemas/StartTest"
required:
- "name"
- "children"`

first := `openapi: 3.1.0
components:
schemas:
StartTest:
type: object
required:
- muffins
properties:
muffins:
type: object
anyOf:
- $ref: "https://kjahsdkjahdkjashdas.com/second.yaml#/components/schemas/CircleTest"`

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

cf := CreateOpenAPIIndexConfig()
cf.IgnorePolymorphicCircularReferences = true
rolodex := NewRolodex(cf)

srv := test_rolodexDeepRefServer([]byte(first), []byte(second), nil)
defer srv.Close()

u, _ := url.Parse(srv.URL)
cf.BaseURL = u
remoteFS, rErr := NewRemoteFSWithConfig(cf)
assert.NoError(t, rErr)

rolodex.AddRemoteFS(srv.URL, remoteFS)
rolodex.SetRootNode(&rootNode)

err := rolodex.IndexTheRolodex()
assert.NoError(t, err)
assert.Len(t, rolodex.GetCaughtErrors(), 0)

// should only be a single loop.
assert.Len(t, rolodex.GetIgnoredCircularReferences(), 1)
}

func TestRolodex_IndexCircularLookup_LookupHttpNoBaseURL(t *testing.T) {

first := `openapi: 3.1.0
components:
schemas:
StartTest:
type: object
required:
- muffins
properties:
muffins:
type: object
anyOf:
- $ref: "https://raw.githubusercontent.com/pb33f/libopenapi/main/test_specs/circular-tests.yaml#/components/schemas/One"`

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

cf := CreateOpenAPIIndexConfig()
cf.IgnorePolymorphicCircularReferences = true
rolodex := NewRolodex(cf)

remoteFS, rErr := NewRemoteFSWithConfig(cf)
assert.NoError(t, rErr)

rolodex.AddRemoteFS("", remoteFS)
rolodex.SetRootNode(&rootNode)

err := rolodex.IndexTheRolodex()
assert.Error(t, err)
assert.Len(t, rolodex.GetCaughtErrors(), 1)
}

func TestRolodex_IndexCircularLookup_ignorePoly(t *testing.T) {

spinny := `openapi: 3.1.0
Expand All @@ -350,7 +440,6 @@ components:
_ = yaml.Unmarshal([]byte(spinny), &rootNode)

cf := CreateOpenAPIIndexConfig()
//cf.IgnoreArrayCircularReferences = true
cf.IgnorePolymorphicCircularReferences = true
rolodex := NewRolodex(cf)
rolodex.SetRootNode(&rootNode)
Expand Down

0 comments on commit aca3ed6

Please sign in to comment.