diff --git a/openapi3/loader.go b/openapi3/loader.go index 89b95eea4..f72a7b0ed 100644 --- a/openapi3/loader.go +++ b/openapi3/loader.go @@ -280,7 +280,7 @@ func join(basePath *url.URL, relativePath *url.URL) (*url.URL, error) { } func resolvePath(basePath *url.URL, componentPath *url.URL) (*url.URL, error) { - if componentPath.Scheme == "" && componentPath.Host == "" { + if is_file(componentPath) { // support absolute paths if componentPath.Path[0] == '/' { return componentPath, nil diff --git a/openapi3/loader_relative_refs_test.go b/openapi3/loader_relative_refs_test.go index 7c553672d..2efb0c7e8 100644 --- a/openapi3/loader_relative_refs_test.go +++ b/openapi3/loader_relative_refs_test.go @@ -801,14 +801,14 @@ var relativeDocRefsTestDataEntries = []refTestDataEntry{ func TestLoadSpecWithRelativeDocumentRefs(t *testing.T) { for _, td := range relativeDocRefsTestDataEntries { - t.Logf("testcase %q", td.name) - - spec := []byte(td.contentTemplate) - loader := NewLoader() - loader.IsExternalRefsAllowed = true - doc, err := loader.LoadFromDataWithPath(spec, &url.URL{Path: "testdata/"}) - require.NoError(t, err) - td.testFunc(t, doc) + t.Run(td.name, func(t *testing.T) { + spec := []byte(td.contentTemplate) + loader := NewLoader() + loader.IsExternalRefsAllowed = true + doc, err := loader.LoadFromDataWithPath(spec, &url.URL{Path: "testdata/"}) + require.NoError(t, err) + td.testFunc(t, doc) + }) } } @@ -909,7 +909,6 @@ func TestLoadSpecWithRelativeDocumentRefs2(t *testing.T) { loader := NewLoader() loader.IsExternalRefsAllowed = true doc, err := loader.LoadFromFile("testdata/relativeDocsUseDocumentPath/openapi/openapi.yml") - require.NoError(t, err) // path in nested directory diff --git a/openapi3/loader_uri_reader.go b/openapi3/loader_uri_reader.go index ee6452f94..ba7b5f24a 100644 --- a/openapi3/loader_uri_reader.go +++ b/openapi3/loader_uri_reader.go @@ -68,12 +68,15 @@ func ReadFromHTTP(cl *http.Client) ReadFromURIFunc { } } +func is_file(location *url.URL) bool { + return location.Path != "" && + location.Host == "" && + (location.Scheme == "" || location.Scheme == "file") +} + // ReadFromFile is a ReadFromURIFunc which reads local file URIs. func ReadFromFile(loader *Loader, location *url.URL) ([]byte, error) { - if location.Host != "" { - return nil, ErrURINotSupported - } - if location.Scheme != "" && location.Scheme != "file" { + if !is_file(location) { return nil, ErrURINotSupported } return os.ReadFile(location.Path)