From 2e729ba4c889a50bd6e56c24d9078c77c58767b7 Mon Sep 17 00:00:00 2001 From: Naer Chang Date: Sun, 15 Aug 2021 13:23:48 -0400 Subject: [PATCH 1/3] fix issue 407, where if server URL has no path out of index exception is thrown --- routers/gorillamux/router.go | 2 +- routers/gorillamux/router_test.go | 17 ++++++++++++++++- 2 files changed, 17 insertions(+), 2 deletions(-) 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..003f04f8a 100644 --- a/routers/gorillamux/router_test.go +++ b/routers/gorillamux/router_test.go @@ -8,6 +8,7 @@ import ( "github.com/getkin/kin-openapi/openapi3" "github.com/getkin/kin-openapi/routers" + "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -30,6 +31,7 @@ func TestRouter(t *testing.T) { Title: "MyAPI", Version: "0.1", }, + Paths: openapi3.Paths{ "/hello": &openapi3.PathItem{ Connect: helloCONNECT, @@ -195,7 +197,7 @@ func TestRouter(t *testing.T) { func TestPermuteScheme(t *testing.T) { scheme0 := "{sche}{me}" - server := &openapi3.Server{URL: scheme0 + "://{d0}.{d1}.com/api/v1/", Variables: map[string]*openapi3.ServerVariable{ + server := &openapi3.Server{URL: scheme0 + "://{d0}.{d1}.com/api/v1", Variables: map[string]*openapi3.ServerVariable{ "d0": {Default: "www"}, "d1": {Default: "example", Enum: []string{"example"}}, "sche": {Default: "http"}, @@ -206,3 +208,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"}}, + }) + assert.Nil(t, err) +} From 0b17fe5dd0f0387c9a6d2ddbc3088f50f1ae18f1 Mon Sep 17 00:00:00 2001 From: Naer Chang Date: Sun, 15 Aug 2021 13:41:18 -0400 Subject: [PATCH 2/3] revert old ut to reduce diff --- routers/gorillamux/router_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/routers/gorillamux/router_test.go b/routers/gorillamux/router_test.go index 003f04f8a..fd9101590 100644 --- a/routers/gorillamux/router_test.go +++ b/routers/gorillamux/router_test.go @@ -197,7 +197,7 @@ func TestRouter(t *testing.T) { func TestPermuteScheme(t *testing.T) { scheme0 := "{sche}{me}" - server := &openapi3.Server{URL: scheme0 + "://{d0}.{d1}.com/api/v1", Variables: map[string]*openapi3.ServerVariable{ + server := &openapi3.Server{URL: scheme0 + "://{d0}.{d1}.com/api/v1/", Variables: map[string]*openapi3.ServerVariable{ "d0": {Default: "www"}, "d1": {Default: "example", Enum: []string{"example"}}, "sche": {Default: "http"}, From 1bb7fb600dd50684c50d80c1db451c2e5524a3e7 Mon Sep 17 00:00:00 2001 From: Naer Chang Date: Sun, 15 Aug 2021 13:43:40 -0400 Subject: [PATCH 3/3] further ut cleanup --- routers/gorillamux/router_test.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/routers/gorillamux/router_test.go b/routers/gorillamux/router_test.go index fd9101590..8dc0a2eb1 100644 --- a/routers/gorillamux/router_test.go +++ b/routers/gorillamux/router_test.go @@ -8,7 +8,6 @@ import ( "github.com/getkin/kin-openapi/openapi3" "github.com/getkin/kin-openapi/routers" - "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -31,7 +30,6 @@ func TestRouter(t *testing.T) { Title: "MyAPI", Version: "0.1", }, - Paths: openapi3.Paths{ "/hello": &openapi3.PathItem{ Connect: helloCONNECT, @@ -219,5 +217,5 @@ func TestServerPath(t *testing.T) { &openapi3.Server{URL: "http://example.com/"}, &openapi3.Server{URL: "http://example.com/path"}}, }) - assert.Nil(t, err) + require.NoError(t, err) }