From 673b8bf0a9d12a615e81142ac797217c6fcfad0c Mon Sep 17 00:00:00 2001 From: Kaushal M Date: Wed, 2 Sep 2020 21:03:33 +0530 Subject: [PATCH 1/3] Fix openapi3.referencedDocumentPath `openapi3.referencedDocument` tried to `path.Join` two URLs, and then generate a new `url.URL` by parsing the obtained path. The URL obtained at the end would be invalid when the base path is HTTP URL. Given base path as 'http://example.com/schemas' and ref as '/schema1', the obtained end URL would be 'http:/example.com/schemas'. This happens because `path.Join` cleans the path it generates and replaces repeated '//' with a single '/'. This is fixed by doing a `path.Join` on the base URL.Path and the ref, and setting this result as the URL.Path of the new URL. --- openapi3/swagger_loader.go | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/openapi3/swagger_loader.go b/openapi3/swagger_loader.go index 71216a28a..aae028065 100644 --- a/openapi3/swagger_loader.go +++ b/openapi3/swagger_loader.go @@ -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 } From 33ca746dce4b6c6322e03d0161352dec8fd6228a Mon Sep 17 00:00:00 2001 From: Kaushal M Date: Thu, 3 Sep 2020 18:47:56 +0530 Subject: [PATCH 2/3] Add tests for referencedDocumentPath --- ...er_loader_referenced_document_path_test.go | 63 +++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 openapi3/swagger_loader_referenced_document_path_test.go diff --git a/openapi3/swagger_loader_referenced_document_path_test.go b/openapi3/swagger_loader_referenced_document_path_test.go new file mode 100644 index 000000000..3e6158fcc --- /dev/null +++ b/openapi3/swagger_loader_referenced_document_path_test.go @@ -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 { + panic(err) + } + fileURL, err := url.Parse("path/to/schemas/test1.yaml") + 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) + require.Nil(t, err) + require.Equal(t, test.expected, result.String()) + } +} From fab3df18da7228eeb7316a05fb65a939073fa854 Mon Sep 17 00:00:00 2001 From: Kaushal M Date: Fri, 4 Sep 2020 11:59:50 +0530 Subject: [PATCH 3/3] Use the right `require` functions --- .../swagger_loader_referenced_document_path_test.go | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/openapi3/swagger_loader_referenced_document_path_test.go b/openapi3/swagger_loader_referenced_document_path_test.go index 3e6158fcc..de219c369 100644 --- a/openapi3/swagger_loader_referenced_document_path_test.go +++ b/openapi3/swagger_loader_referenced_document_path_test.go @@ -9,13 +9,11 @@ import ( func TestReferencedDocumentPath(t *testing.T) { httpURL, err := url.Parse("http://example.com/path/to/schemas/test1.yaml") - if err != nil { - panic(err) - } + require.NoError(t, err) + fileURL, err := url.Parse("path/to/schemas/test1.yaml") - if err != nil { - panic(err) - } + require.NoError(t, err) + refEmpty := "" refNoComponent := "moreschemas/test2.yaml" refWithComponent := "moreschemas/test2.yaml#/components/schemas/someobject" @@ -56,8 +54,7 @@ func TestReferencedDocumentPath(t *testing.T) { }, } { result, err := referencedDocumentPath(test.path, test.ref) - require.NotNil(t, result) - require.Nil(t, err) + require.NoError(t, err) require.Equal(t, test.expected, result.String()) } }