Adding more use-cases for resolving remote docs

Signed-off-by: quobix <dave@quobix.com>
This commit is contained in:
quobix
2023-10-30 10:43:51 -04:00
parent d8dfafd0a4
commit aca3ed66d7
2 changed files with 115 additions and 22 deletions

View File

@@ -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") {
// split the http URI into parts
httpExp := strings.Split(ref.FullDefinition, "#/")
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])
if strings.HasPrefix(exp[0], "http") {
fullDef = value
} else {
if filepath.IsAbs(exp[0]) {
fullDef = value
if strings.HasPrefix(ref.FullDefinition, "http") {
// split the http URI into parts
httpExp := strings.Split(ref.FullDefinition, "#/")
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
// 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 {
@@ -606,7 +611,7 @@ func (resolver *Resolver) extractRelatives(ref *Reference, node, parent *yaml.No
}
}
} else {
panic("mummmmma mia")
def = l
}
} else {
@@ -648,7 +653,6 @@ func (resolver *Resolver) extractRelatives(ref *Reference, node, parent *yaml.No
}
}
}
//panic("oh no")
}
mappedRefs, _ := resolver.specIndex.SearchIndexForReference(def)

View File

@@ -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
@@ -350,7 +440,6 @@ components:
_ = yaml.Unmarshal([]byte(spinny), &rootNode)
cf := CreateOpenAPIIndexConfig()
//cf.IgnoreArrayCircularReferences = true
cf.IgnorePolymorphicCircularReferences = true
rolodex := NewRolodex(cf)
rolodex.SetRootNode(&rootNode)