diff --git a/routers/gorillamux/router.go b/routers/gorillamux/router.go index 31d8dc8fe..eec1f0122 100644 --- a/routers/gorillamux/router.go +++ b/routers/gorillamux/router.go @@ -35,10 +35,16 @@ func NewRouter(doc *openapi3.T) (routers.Router, error) { servers := make([]srv, 0, len(doc.Servers)) for _, server := range doc.Servers { serverURL := server.URL - scheme0 := strings.Split(serverURL, "://")[0] - schemes := permutePart(scheme0, server) - - u, err := url.Parse(bEncode(strings.Replace(serverURL, scheme0+"://", schemes[0]+"://", 1))) + var schemes []string + var u *url.URL + var err error + if strings.Contains(serverURL, "://") { + scheme0 := strings.Split(serverURL, "://")[0] + schemes = permutePart(scheme0, server) + u, err = url.Parse(bEncode(strings.Replace(serverURL, scheme0+"://", schemes[0]+"://", 1))) + } else { + u, err = url.Parse(bEncode(serverURL)) + } if err != nil { return nil, err } diff --git a/routers/gorillamux/router_test.go b/routers/gorillamux/router_test.go index 8dc0a2eb1..6c660187e 100644 --- a/routers/gorillamux/router_test.go +++ b/routers/gorillamux/router_test.go @@ -219,3 +219,26 @@ func TestServerPath(t *testing.T) { }) require.NoError(t, err) } + +func TestRelativeURL(t *testing.T) { + helloGET := &openapi3.Operation{Responses: openapi3.NewResponses()} + doc := &openapi3.T{ + Servers: openapi3.Servers{ + &openapi3.Server{ + URL: "/api/v1", + }, + }, + Paths: openapi3.Paths{ + "/hello": &openapi3.PathItem{ + Get: helloGET, + }, + }, + } + router, err := NewRouter(doc) + require.NoError(t, err) + req, err := http.NewRequest(http.MethodGet, "https://example.com/api/v1/hello", nil) + require.NoError(t, err) + route, _, err := router.FindRoute(req) + require.NoError(t, err) + require.Equal(t, "/hello", route.Path) +}