From 14157e8cfa998fc50b902339ea6b3f7c10448b39 Mon Sep 17 00:00:00 2001 From: "Artem V. Navrotskiy" Date: Fri, 21 Jan 2022 10:38:46 +0300 Subject: [PATCH 1/3] Fix slash removing in `documentPathForRecursiveRef` method --- openapi3/loader.go | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/openapi3/loader.go b/openapi3/loader.go index 0b8d0e1cc..2a60f08ec 100644 --- a/openapi3/loader.go +++ b/openapi3/loader.go @@ -422,8 +422,17 @@ func (loader *Loader) documentPathForRecursiveRef(current *url.URL, resolvedRef if loader.rootDir == "" { return current } - return &url.URL{Path: path.Join(loader.rootDir, resolvedRef)} - + joinedPath := path.Join(loader.rootDir, resolvedRef) + // 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(joinedPath, "/") { + joinedPath += "/" + } + return &url.URL{Path: joinedPath} } func (loader *Loader) resolveRef(doc *T, ref string, path *url.URL) (*T, string, *url.URL, error) { From 799e754e3646d0aaa95db052f33cae2b3d42d48a Mon Sep 17 00:00:00 2001 From: "Artem V. Navrotskiy" Date: Fri, 21 Jan 2022 17:06:27 +0300 Subject: [PATCH 2/3] Fix `documentPathForRecursiveRef` fragment support --- openapi3/loader.go | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/openapi3/loader.go b/openapi3/loader.go index 2a60f08ec..40bd5ef26 100644 --- a/openapi3/loader.go +++ b/openapi3/loader.go @@ -422,17 +422,26 @@ func (loader *Loader) documentPathForRecursiveRef(current *url.URL, resolvedRef if loader.rootDir == "" { return current } - joinedPath := path.Join(loader.rootDir, resolvedRef) + var fragment string + relPath := resolvedRef + if idx := strings.IndexByte(relPath, '#'); idx >= 0 { + fragment = relPath[idx+1:] + relPath = relPath[:idx] + } + 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(joinedPath, "/") { - joinedPath += "/" + if (resolvedRef == "" || strings.HasSuffix(resolvedRef, "/")) && !strings.HasSuffix(respolvedPath, "/") { + respolvedPath += "/" + } + return &url.URL{ + Path: respolvedPath, + Fragment: fragment, } - return &url.URL{Path: joinedPath} } func (loader *Loader) resolveRef(doc *T, ref string, path *url.URL) (*T, string, *url.URL, error) { From 20ba5305e0adcc31fa0f113117539dfd6c855e8d Mon Sep 17 00:00:00 2001 From: "Artem V. Navrotskiy" Date: Fri, 21 Jan 2022 23:27:42 +0300 Subject: [PATCH 3/3] Fix `documentPathForRecursiveRef` empty `resolvedRef` path --- openapi3/loader.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/openapi3/loader.go b/openapi3/loader.go index 40bd5ef26..7a9e324d0 100644 --- a/openapi3/loader.go +++ b/openapi3/loader.go @@ -422,12 +422,21 @@ func (loader *Loader) documentPathForRecursiveRef(current *url.URL, resolvedRef if loader.rootDir == "" { return current } + 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: