diff --git a/routers/gorillamux/router.go b/routers/gorillamux/router.go index a47c762e7..31d8dc8fe 100644 --- a/routers/gorillamux/router.go +++ b/routers/gorillamux/router.go @@ -43,7 +43,7 @@ func NewRouter(doc *openapi3.T) (routers.Router, error) { return nil, err } path := bDecode(u.EscapedPath()) - if path[len(path)-1] == '/' { + if len(path) > 0 && path[len(path)-1] == '/' { path = path[:len(path)-1] } servers = append(servers, srv{ diff --git a/routers/gorillamux/router_test.go b/routers/gorillamux/router_test.go index c42d7f835..8dc0a2eb1 100644 --- a/routers/gorillamux/router_test.go +++ b/routers/gorillamux/router_test.go @@ -206,3 +206,16 @@ func TestPermuteScheme(t *testing.T) { perms := permutePart(scheme0, server) require.Equal(t, []string{"http", "https"}, perms) } + +func TestServerPath(t *testing.T) { + server := &openapi3.Server{URL: "http://example.com"} + err := server.Validate(context.Background()) + require.NoError(t, err) + + _, err = NewRouter(&openapi3.T{Servers: openapi3.Servers{ + server, + &openapi3.Server{URL: "http://example.com/"}, + &openapi3.Server{URL: "http://example.com/path"}}, + }) + require.NoError(t, err) +}