Skip to content
Merged
Show file tree
Hide file tree
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
10 changes: 7 additions & 3 deletions client/internal/request/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -533,14 +533,18 @@ func (r *Request) resolveURLPath(basePath string) (string, url.Values, error) {
}
}

reinstateSlash := pathPatternURL.Path != "" && pathPatternURL.Path != "/" &&
pathPatternURL.Path[len(pathPatternURL.Path)-1] == '/'
// path.Join strips trailing slashes; reinstate one whenever the
// pathPattern carried it, including the bare-root case ("/" under a
// non-empty basePath, which path.Join would collapse to "/basepath").
// The HasSuffix check on urlPath keeps the rewrite idempotent and
// avoids producing "//" when basePath is "/" or empty.
reinstateSlash := strings.HasSuffix(pathPatternURL.Path, "/")

urlPath := path.Join(basePathURL.Path, pathPatternURL.Path)
for k, v := range r.pathParams {
urlPath = strings.ReplaceAll(urlPath, "{"+k+"}", url.PathEscape(v))
}
if reinstateSlash {
if reinstateSlash && !strings.HasSuffix(urlPath, "/") {
urlPath += "/"
}

Expand Down
36 changes: 36 additions & 0 deletions client/internal/request/request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,42 @@ func TestBuildRequest_BuildHTTP_EscapedPath(t *testing.T) {
assert.EqualT(t, req.URL.RawPath, req.URL.EscapedPath())
}

// TestBuildRequest_BuildHTTP_RootPathTrailingSlash locks in the fix for
// issue #101: the bare-root pattern "/" under a non-empty basePath must
// keep its trailing slash, and the bare-root cases that the pre-fix
// formula avoided ("" / "/" basePath) must still not produce "//".
func TestBuildRequest_BuildHTTP_RootPathTrailingSlash(t *testing.T) {
const bp = "/basepath"
cases := []struct {
name string
basePath string
pathPattern string
wantPath string
}{
{"root pattern under non-empty basePath keeps slash (#101)", bp, "/", bp + "/"},
{"root pattern under '/' basePath stays '/'", "/", "/", "/"},
{"root pattern under empty basePath stays '/'", "", "/", "/"},
{"non-root trailing slash still preserved", bp, "/users/", bp + "/users/"},
{"no trailing slash on pattern produces no trailing slash", bp, "/users", bp + "/users"},
}

for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
reqWrtr := runtime.ClientRequestWriterFunc(func(_ runtime.ClientRequest, _ strfmt.Registry) error {
return nil
})
r := New(http.MethodGet, tc.pathPattern, reqWrtr)

req, cancel, err := r.BuildHTTPContext(t.Context(), runtime.JSONMime, tc.basePath, testProducers, nil, nil)
require.NoError(t, err)
t.Cleanup(cancel)
require.NotNil(t, req)

assert.EqualT(t, tc.wantPath, req.URL.Path)
})
}
}

func TestBuildRequest_BuildHTTP_BasePathWithQueryParameters(t *testing.T) {
reqWrtr := runtime.ClientRequestWriterFunc(func(req runtime.ClientRequest, _ strfmt.Registry) error {
_ = req.SetBodyParam(nil)
Expand Down
Loading