From fe18256a527b5b1f976b8ab009ce25ea18c973e9 Mon Sep 17 00:00:00 2001 From: Andrey Dyatlov Date: Mon, 22 Nov 2021 04:05:26 +0100 Subject: [PATCH 1/3] Fix issue https://github.com/getkin/kin-openapi/issues/410: do not specify scheme for relative paths. --- routers/gorillamux/router.go | 21 +++++++++++++++------ routers/gorillamux/router_test.go | 22 ++++++++++++++++++++++ 2 files changed, 37 insertions(+), 6 deletions(-) diff --git a/routers/gorillamux/router.go b/routers/gorillamux/router.go index 31d8dc8fe..3c3658bf3 100644 --- a/routers/gorillamux/router.go +++ b/routers/gorillamux/router.go @@ -35,12 +35,21 @@ 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))) - if err != nil { - return nil, err + 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))) + if err != nil { + return nil, err + } + } else { + u, err = url.Parse(bEncode(serverURL)) + if err != nil { + return nil, err + } } path := bDecode(u.EscapedPath()) if len(path) > 0 && path[len(path)-1] == '/' { diff --git a/routers/gorillamux/router_test.go b/routers/gorillamux/router_test.go index 8dc0a2eb1..5bce15a6b 100644 --- a/routers/gorillamux/router_test.go +++ b/routers/gorillamux/router_test.go @@ -219,3 +219,25 @@ 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, _ := http.NewRequest(http.MethodGet, "https://example.com/api/v1/hello", nil) + route, _, err := router.FindRoute(req) + require.NoError(t, err) + require.Equal(t, "/hello", route.Path) +} From b5c98070bbdf3e675a25ab585af567ca16ace9d8 Mon Sep 17 00:00:00 2001 From: Andrey Dyatlov Date: Mon, 22 Nov 2021 12:06:00 +0100 Subject: [PATCH 2/3] Apply suggestions from code review Thanks! Co-authored-by: Pierre Fenoll --- routers/gorillamux/router.go | 9 +++------ routers/gorillamux/router_test.go | 5 +++-- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/routers/gorillamux/router.go b/routers/gorillamux/router.go index 3c3658bf3..64ac645fc 100644 --- a/routers/gorillamux/router.go +++ b/routers/gorillamux/router.go @@ -42,15 +42,12 @@ func NewRouter(doc *openapi3.T) (routers.Router, error) { scheme0 := strings.Split(serverURL, "://")[0] schemes = permutePart(scheme0, server) u, err = url.Parse(bEncode(strings.Replace(serverURL, scheme0+"://", schemes[0]+"://", 1))) - if err != nil { - return nil, err - } } else { u, err = url.Parse(bEncode(serverURL)) - if err != nil { - return nil, err - } } + if err != nil { + return nil, err + } path := bDecode(u.EscapedPath()) if len(path) > 0 && path[len(path)-1] == '/' { path = path[:len(path)-1] diff --git a/routers/gorillamux/router_test.go b/routers/gorillamux/router_test.go index 5bce15a6b..6c660187e 100644 --- a/routers/gorillamux/router_test.go +++ b/routers/gorillamux/router_test.go @@ -230,13 +230,14 @@ func TestRelativeURL(t *testing.T) { }, Paths: openapi3.Paths{ "/hello": &openapi3.PathItem{ - Get: helloGET, + Get: helloGET, }, }, } router, err := NewRouter(doc) require.NoError(t, err) - req, _ := http.NewRequest(http.MethodGet, "https://example.com/api/v1/hello", nil) + 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) From 3ad289823011c4714a9e17846b85dbeda9c8984d Mon Sep 17 00:00:00 2001 From: Andrey Dyatlov Date: Mon, 22 Nov 2021 12:08:49 +0100 Subject: [PATCH 3/3] Fix indentation. --- routers/gorillamux/router.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/routers/gorillamux/router.go b/routers/gorillamux/router.go index 64ac645fc..eec1f0122 100644 --- a/routers/gorillamux/router.go +++ b/routers/gorillamux/router.go @@ -45,9 +45,9 @@ func NewRouter(doc *openapi3.T) (routers.Router, error) { } else { u, err = url.Parse(bEncode(serverURL)) } - if err != nil { - return nil, err - } + if err != nil { + return nil, err + } path := bDecode(u.EscapedPath()) if len(path) > 0 && path[len(path)-1] == '/' { path = path[:len(path)-1]