Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 13 additions & 10 deletions openapi3/swagger_loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -876,16 +876,19 @@ func unescapeRefString(ref string) string {
}

func referencedDocumentPath(documentPath *url.URL, ref string) (*url.URL, error) {
newDocumentPath := documentPath
if documentPath != nil {
refDirectory, err := url.Parse(path.Dir(ref))
if err != nil {
return nil, err
}
joinedDirectory := path.Join(path.Dir(documentPath.String()), refDirectory.String())
if newDocumentPath, err = url.Parse(joinedDirectory + "/"); err != nil {
return nil, err
}
if documentPath == nil {
return nil, nil
}

newDocumentPath, err := copyURL(documentPath)
if err != nil {
return nil, err
}
refPath, err := url.Parse(ref)
if err != nil {
return nil, err
}
newDocumentPath.Path = path.Join(path.Dir(newDocumentPath.Path), path.Dir(refPath.Path)) + "/"

return newDocumentPath, nil
}
60 changes: 60 additions & 0 deletions openapi3/swagger_loader_referenced_document_path_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package openapi3

import (
"net/url"
"testing"

"github.com/stretchr/testify/require"
)

func TestReferencedDocumentPath(t *testing.T) {
httpURL, err := url.Parse("http://example.com/path/to/schemas/test1.yaml")
require.NoError(t, err)

fileURL, err := url.Parse("path/to/schemas/test1.yaml")
require.NoError(t, err)

refEmpty := ""
refNoComponent := "moreschemas/test2.yaml"
refWithComponent := "moreschemas/test2.yaml#/components/schemas/someobject"

for _, test := range []struct {
path *url.URL
ref, expected string
}{
{
path: httpURL,
ref: refEmpty,
expected: "http://example.com/path/to/schemas/",
},
{
path: httpURL,
ref: refNoComponent,
expected: "http://example.com/path/to/schemas/moreschemas/",
},
{
path: httpURL,
ref: refWithComponent,
expected: "http://example.com/path/to/schemas/moreschemas/",
},
{
path: fileURL,
ref: refEmpty,
expected: "path/to/schemas/",
},
{
path: fileURL,
ref: refNoComponent,
expected: "path/to/schemas/moreschemas/",
},
{
path: fileURL,
ref: refWithComponent,
expected: "path/to/schemas/moreschemas/",
},
} {
result, err := referencedDocumentPath(test.path, test.ref)
require.NoError(t, err)
require.Equal(t, test.expected, result.String())
}
}