Skip to content

Commit

Permalink
Fix issue #410 (#450)
Browse files Browse the repository at this point in the history
Co-authored-by: Pierre Fenoll <[email protected]>
  • Loading branch information
adyatlov and fenollp authored Nov 22, 2021
1 parent 41c2da7 commit 0db2d4c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
14 changes: 10 additions & 4 deletions routers/gorillamux/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
23 changes: 23 additions & 0 deletions routers/gorillamux/router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit 0db2d4c

Please sign in to comment.