Skip to content

Commit

Permalink
Use use OS repsecting file seperator
Browse files Browse the repository at this point in the history
  • Loading branch information
percivalalb authored and Alex Barter committed Jun 27, 2024
1 parent 43f4e25 commit 8091a0a
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 7 deletions.
8 changes: 4 additions & 4 deletions openapi3/internalize_refs.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,14 @@ func DefaultRefNameResolver(doc *T, ref componentRef) string {
var internalisedName string

if filePath != "" {
internalisedName = strings.TrimLeft(filePath, "."+string(filepath.Separator))
internalisedName = strings.TrimLeft(filePath, "./"+string(filepath.Separator))
}

if componentPath != "" {
if internalisedName != "" {
internalisedName += "_"
}
internalisedName += strings.TrimLeft(componentPath, "."+string(filepath.Separator))
internalisedName += strings.TrimLeft(componentPath, "./"+string(filepath.Separator))
}

// Replace invalid characters in component fixed field names.
Expand All @@ -91,8 +91,8 @@ func cutDirectories(p, dirs string) (string, bool) {
return p, false
}

p = strings.TrimRight(p, string(filepath.Separator))
dirs = strings.TrimRight(dirs, string(filepath.Separator))
p = strings.TrimRight(p, "/"+string(filepath.Separator))
dirs = strings.TrimRight(dirs, "/"+string(filepath.Separator))

var sb strings.Builder
sb.Grow(len(ParameterInHeader))
Expand Down
3 changes: 2 additions & 1 deletion openapi3/load_cicular_ref_with_external_file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"embed"
"encoding/json"
"net/url"
"path/filepath"
"testing"

"github.com/stretchr/testify/require"
Expand All @@ -21,7 +22,7 @@ func TestLoadCircularRefFromFile(t *testing.T) {
loader := openapi3.NewLoader()
loader.IsExternalRefsAllowed = true
loader.ReadFromURIFunc = func(loader *openapi3.Loader, uri *url.URL) ([]byte, error) {
return circularResSpecs.ReadFile(uri.Path)
return circularResSpecs.ReadFile(filepath.ToSlash(uri.Path))
}

got, err := loader.LoadFromFile("testdata/circularRef/base.yml")
Expand Down
3 changes: 2 additions & 1 deletion openapi3/load_with_go_embed_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"embed"
"fmt"
"net/url"
"path/filepath"

"github.com/getkin/kin-openapi/openapi3"
)
Expand All @@ -18,7 +19,7 @@ func Example() {
loader := openapi3.NewLoader()
loader.IsExternalRefsAllowed = true
loader.ReadFromURIFunc = func(loader *openapi3.Loader, uri *url.URL) ([]byte, error) {
return fs.ReadFile(uri.Path)
return fs.ReadFile(filepath.ToSlash(uri.Path))
}

doc, err := loader.LoadFromFile("testdata/recursiveRef/openapi.yml")
Expand Down
3 changes: 2 additions & 1 deletion openapi3/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ func join(basePath *url.URL, relativePath *url.URL) *url.URL {
return relativePath
}
newPath := *basePath
newPath.Path = path.Join(path.Dir(newPath.Path), relativePath.Path)
newPath.Path = filepath.Join(filepath.Dir(newPath.Path), relativePath.Path)
return &newPath
}

Expand All @@ -269,6 +269,7 @@ func resolvePath(basePath *url.URL, componentPath *url.URL) *url.URL {
if componentPath.Path[0] == '/' {
return componentPath
}
componentPath.Path = filepath.FromSlash(componentPath.Path)
return join(basePath, componentPath)
}
return componentPath
Expand Down

0 comments on commit 8091a0a

Please sign in to comment.