Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix slash removing in documentPathForRecursiveRef method #478

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 29 additions & 2 deletions openapi3/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -422,8 +422,35 @@ func (loader *Loader) documentPathForRecursiveRef(current *url.URL, resolvedRef
if loader.rootDir == "" {
return current
}
return &url.URL{Path: path.Join(loader.rootDir, resolvedRef)}

if resolvedRef == "" {
return current
}
var fragment string
relPath := resolvedRef
if idx := strings.IndexByte(relPath, '#'); idx >= 0 {
fragment = relPath[idx+1:]
relPath = relPath[:idx]
}
if relPath == "" {
return &url.URL{
Path: current.Path,
Fragment: fragment,
}
}
respolvedPath := path.Join(loader.rootDir, relPath)
// Unfortunately `path.Join` remove last slash from result file.
// For example:
// ```
// path.Join("foo/bar/", "") == "foo/bar"
// ```
// In this workaround we try to restore last slash if needed.
if (resolvedRef == "" || strings.HasSuffix(resolvedRef, "/")) && !strings.HasSuffix(respolvedPath, "/") {
respolvedPath += "/"
}
return &url.URL{
Path: respolvedPath,
Fragment: fragment,
}
}

func (loader *Loader) resolveRef(doc *T, ref string, path *url.URL) (*T, string, *url.URL, error) {
Expand Down