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()) + } +}