Skip to content
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