diff --git a/index/resolver.go b/index/resolver.go index 5006b009..45916e46 100644 --- a/index/resolver.go +++ b/index/resolver.go @@ -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 + } 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 - // 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) diff --git a/index/rolodex_test.go b/index/rolodex_test.go index e65b4a07..f9c1c8d7 100644 --- a/index/rolodex_test.go +++ b/index/rolodex_test.go @@ -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)