Skip to content

Commit

Permalink
Fix openapi3.referencedDocumentPath (#248)
Browse files Browse the repository at this point in the history
  • Loading branch information
kshlm authored Sep 4, 2020
1 parent 2392e46 commit ceae068
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 10 deletions.
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())
}
}

0 comments on commit ceae068

Please sign in to comment.