Skip to content

Commit

Permalink
some refacto
Browse files Browse the repository at this point in the history
Signed-off-by: Pierre Fenoll <[email protected]>
  • Loading branch information
fenollp committed Nov 25, 2023
1 parent 243c14f commit bc648b0
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 14 deletions.
2 changes: 1 addition & 1 deletion openapi3/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 8 additions & 9 deletions openapi3/loader_relative_refs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}
}

Expand Down Expand Up @@ -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
Expand Down
11 changes: 7 additions & 4 deletions openapi3/loader_uri_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit bc648b0

Please sign in to comment.