diff --git a/datamodel/low/extraction_functions.go b/datamodel/low/extraction_functions.go index f355fc0..4bcefbd 100644 --- a/datamodel/low/extraction_functions.go +++ b/datamodel/low/extraction_functions.go @@ -99,9 +99,12 @@ func LocateRefNodeWithContext(ctx context.Context, root *yaml.Node, idx *index.S if strings.HasPrefix(specPath, "http") { u, _ := url.Parse(specPath) - p := filepath.Dir(u.Path) - abs, _ := filepath.Abs(filepath.Join(p, explodedRefValue[0])) - u.Path = abs + p := "" + if u.Path != "" { + p = filepath.Dir(u.Path) + } + u.Path = filepath.Join(p, explodedRefValue[0]) + u.Fragment = "" rv = fmt.Sprintf("%s#%s", u.String(), explodedRefValue[1]) } else { @@ -116,9 +119,11 @@ func LocateRefNodeWithContext(ctx context.Context, root *yaml.Node, idx *index.S if idx.GetConfig().BaseURL != nil { u := *idx.GetConfig().BaseURL - - abs, _ := filepath.Abs(filepath.Join(u.Path, rv)) - u.Path = abs + p := "" + if u.Path != "" { + p = filepath.Dir(u.Path) + } + u.Path = filepath.Join(p, explodedRefValue[0]) rv = fmt.Sprintf("%s#%s", u.String(), explodedRefValue[1]) } diff --git a/datamodel/low/extraction_functions_test.go b/datamodel/low/extraction_functions_test.go index 27525a2..6a75386 100644 --- a/datamodel/low/extraction_functions_test.go +++ b/datamodel/low/extraction_functions_test.go @@ -7,6 +7,7 @@ import ( "context" "crypto/sha256" "fmt" + "net/url" "strings" "testing" @@ -1678,3 +1679,213 @@ func TestSetReference_nil(t *testing.T) { SetReference(nil, "#/pigeon/street") assert.NotEqual(t, "#/pigeon/street", n.GetReference()) } + +func TestLocateRefNode_CurrentPathKey_HttpLink(t *testing.T) { + + no := yaml.Node{ + Kind: yaml.MappingNode, + Content: []*yaml.Node{ + { + Kind: yaml.ScalarNode, + Value: "$ref", + }, + { + Kind: yaml.ScalarNode, + Value: "http://cakes.com#/components/schemas/thing", + }, + }, + } + + ctx := context.WithValue(context.Background(), index.CurrentPathKey, "http://cakes.com#/components/schemas/thing") + + idx := index.NewSpecIndexWithConfig(&no, index.CreateClosedAPIIndexConfig()) + n, i, e, c := LocateRefNodeWithContext(ctx, &no, idx) + assert.Nil(t, n) + assert.NotNil(t, i) + assert.NotNil(t, e) + assert.NotNil(t, c) +} + +func TestLocateRefNode_CurrentPathKey_HttpLink_RemoteCtx(t *testing.T) { + + no := yaml.Node{ + Kind: yaml.MappingNode, + Content: []*yaml.Node{ + { + Kind: yaml.ScalarNode, + Value: "$ref", + }, + { + Kind: yaml.ScalarNode, + Value: "#/components/schemas/thing", + }, + }, + } + + ctx := context.WithValue(context.Background(), index.CurrentPathKey, "https://cakes.com#/components/schemas/thing") + idx := index.NewSpecIndexWithConfig(&no, index.CreateClosedAPIIndexConfig()) + n, i, e, c := LocateRefNodeWithContext(ctx, &no, idx) + assert.Nil(t, n) + assert.NotNil(t, i) + assert.NotNil(t, e) + assert.NotNil(t, c) +} + +func TestLocateRefNode_CurrentPathKey_HttpLink_RemoteCtx_WithPath(t *testing.T) { + + no := yaml.Node{ + Kind: yaml.MappingNode, + Content: []*yaml.Node{ + { + Kind: yaml.ScalarNode, + Value: "$ref", + }, + { + Kind: yaml.ScalarNode, + Value: "#/components/schemas/thing", + }, + }, + } + + ctx := context.WithValue(context.Background(), index.CurrentPathKey, "https://cakes.com/jazzzy/shoes#/components/schemas/thing") + idx := index.NewSpecIndexWithConfig(&no, index.CreateClosedAPIIndexConfig()) + n, i, e, c := LocateRefNodeWithContext(ctx, &no, idx) + assert.Nil(t, n) + assert.NotNil(t, i) + assert.NotNil(t, e) + assert.NotNil(t, c) +} + +func TestLocateRefNode_CurrentPathKey_Path_Link(t *testing.T) { + + no := yaml.Node{ + Kind: yaml.MappingNode, + Content: []*yaml.Node{ + { + Kind: yaml.ScalarNode, + Value: "$ref", + }, + { + Kind: yaml.ScalarNode, + Value: "yazzy.yaml#/components/schemas/thing", + }, + }, + } + + ctx := context.WithValue(context.Background(), index.CurrentPathKey, "/jazzzy/shoes.yaml") + idx := index.NewSpecIndexWithConfig(&no, index.CreateClosedAPIIndexConfig()) + n, i, e, c := LocateRefNodeWithContext(ctx, &no, idx) + assert.Nil(t, n) + assert.NotNil(t, i) + assert.NotNil(t, e) + assert.NotNil(t, c) +} + +func TestLocateRefNode_CurrentPathKey_Path_URL(t *testing.T) { + + no := yaml.Node{ + Kind: yaml.MappingNode, + Content: []*yaml.Node{ + { + Kind: yaml.ScalarNode, + Value: "$ref", + }, + { + Kind: yaml.ScalarNode, + Value: "yazzy.yaml#/components/schemas/thing", + }, + }, + } + + cf := index.CreateClosedAPIIndexConfig() + u, _ := url.Parse("https://herbs-and-coffee-in-the-fall.com") + cf.BaseURL = u + idx := index.NewSpecIndexWithConfig(&no, cf) + n, i, e, c := LocateRefNodeWithContext(context.Background(), &no, idx) + assert.Nil(t, n) + assert.NotNil(t, i) + assert.NotNil(t, e) + assert.NotNil(t, c) +} + +func TestLocateRefNode_CurrentPathKey_DeeperPath_URL(t *testing.T) { + + no := yaml.Node{ + Kind: yaml.MappingNode, + Content: []*yaml.Node{ + { + Kind: yaml.ScalarNode, + Value: "$ref", + }, + { + Kind: yaml.ScalarNode, + Value: "slasshy/mazsshy/yazzy.yaml#/components/schemas/thing", + }, + }, + } + + cf := index.CreateClosedAPIIndexConfig() + u, _ := url.Parse("https://herbs-and-coffee-in-the-fall.com/pizza/burgers") + cf.BaseURL = u + idx := index.NewSpecIndexWithConfig(&no, cf) + n, i, e, c := LocateRefNodeWithContext(context.Background(), &no, idx) + assert.Nil(t, n) + assert.NotNil(t, i) + assert.NotNil(t, e) + assert.NotNil(t, c) +} + +func TestLocateRefNode_NoExplode(t *testing.T) { + + no := yaml.Node{ + Kind: yaml.MappingNode, + Content: []*yaml.Node{ + { + Kind: yaml.ScalarNode, + Value: "$ref", + }, + { + Kind: yaml.ScalarNode, + Value: "components/schemas/thing.yaml", + }, + }, + } + + cf := index.CreateClosedAPIIndexConfig() + u, _ := url.Parse("http://smiledfdfdfdfds.com/bikes") + cf.BaseURL = u + idx := index.NewSpecIndexWithConfig(&no, cf) + n, i, e, c := LocateRefNodeWithContext(context.Background(), &no, idx) + assert.Nil(t, n) + assert.NotNil(t, i) + assert.NotNil(t, e) + assert.NotNil(t, c) +} + +func TestLocateRefNode_NoExplode_HTTP(t *testing.T) { + + no := yaml.Node{ + Kind: yaml.MappingNode, + Content: []*yaml.Node{ + { + Kind: yaml.ScalarNode, + Value: "$ref", + }, + { + Kind: yaml.ScalarNode, + Value: "components/schemas/thing.yaml", + }, + }, + } + + cf := index.CreateClosedAPIIndexConfig() + u, _ := url.Parse("http://smilfghfhfhfhfhes.com/bikes") + cf.BaseURL = u + idx := index.NewSpecIndexWithConfig(&no, cf) + ctx := context.WithValue(context.Background(), index.CurrentPathKey, "http://minty-fresh-shoes.com/nice/no.yaml") + n, i, e, c := LocateRefNodeWithContext(ctx, &no, idx) + assert.Nil(t, n) + assert.NotNil(t, i) + assert.NotNil(t, e) + assert.NotNil(t, c) +}