Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix openapi3.referencedDocumentPath #248

Merged
merged 3 commits into from
Sep 4, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
}
63 changes: 63 additions & 0 deletions openapi3/swagger_loader_referenced_document_path_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
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")
if err != nil {
kshlm marked this conversation as resolved.
Show resolved Hide resolved
panic(err)
}
fileURL, err := url.Parse("path/to/schemas/test1.yaml")
kshlm marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
panic(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.NotNil(t, result)
kshlm marked this conversation as resolved.
Show resolved Hide resolved
require.Nil(t, err)
kshlm marked this conversation as resolved.
Show resolved Hide resolved
require.Equal(t, test.expected, result.String())
}
}