Skip to content

Commit 9f31860

Browse files
Add support of query string to swagger UI
1 parent c7b1da0 commit 9f31860

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

swagger.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ func Handler(configFns ...func(*Config)) http.HandlerFunc {
191191
return
192192
}
193193

194-
matches := re.FindStringSubmatch(r.RequestURI)
194+
matches := re.FindStringSubmatch(r.URL.Path)
195195

196196
path := matches[2]
197197

@@ -222,7 +222,11 @@ func Handler(configFns ...func(*Config)) http.HandlerFunc {
222222

223223
_, _ = w.Write([]byte(doc))
224224
case "":
225-
http.Redirect(w, r, matches[1]+"/"+"index.html", http.StatusMovedPermanently)
225+
query := r.URL.RawQuery
226+
if query != "" {
227+
query = "?" + query
228+
}
229+
http.Redirect(w, r, matches[1]+"/"+"index.html"+query, http.StatusMovedPermanently)
226230
default:
227231
var err error
228232
r.URL, err = url.Parse(matches[2])

swagger_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,12 @@ func TestWrapHandler(t *testing.T) {
107107
assert.Equal(t, http.StatusMethodNotAllowed, performRequest(http.MethodPost, test.RootFolder+"index.html", router).Code)
108108

109109
assert.Equal(t, http.StatusMethodNotAllowed, performRequest(http.MethodPut, test.RootFolder+"index.html", router).Code)
110+
111+
w7 := performRequest(http.MethodGet, test.RootFolder+"index.html?param=value&another=value", router)
112+
assert.Equal(t, http.StatusOK, w7.Code)
113+
assert.Equal(t, w7.Header()["Content-Type"][0], "text/html; charset=utf-8")
114+
115+
assert.Equal(t, 301, performRequest(http.MethodGet, test.RootFolder+"?param=value&another=value", router).Code)
110116
}
111117
}
112118

0 commit comments

Comments
 (0)