Skip to content

Commit 0db2d4c

Browse files
adyatlovfenollp
andauthored
Fix issue #410 (#450)
Co-authored-by: Pierre Fenoll <[email protected]>
1 parent 41c2da7 commit 0db2d4c

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

routers/gorillamux/router.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,16 @@ func NewRouter(doc *openapi3.T) (routers.Router, error) {
3535
servers := make([]srv, 0, len(doc.Servers))
3636
for _, server := range doc.Servers {
3737
serverURL := server.URL
38-
scheme0 := strings.Split(serverURL, "://")[0]
39-
schemes := permutePart(scheme0, server)
40-
41-
u, err := url.Parse(bEncode(strings.Replace(serverURL, scheme0+"://", schemes[0]+"://", 1)))
38+
var schemes []string
39+
var u *url.URL
40+
var err error
41+
if strings.Contains(serverURL, "://") {
42+
scheme0 := strings.Split(serverURL, "://")[0]
43+
schemes = permutePart(scheme0, server)
44+
u, err = url.Parse(bEncode(strings.Replace(serverURL, scheme0+"://", schemes[0]+"://", 1)))
45+
} else {
46+
u, err = url.Parse(bEncode(serverURL))
47+
}
4248
if err != nil {
4349
return nil, err
4450
}

routers/gorillamux/router_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,3 +219,26 @@ func TestServerPath(t *testing.T) {
219219
})
220220
require.NoError(t, err)
221221
}
222+
223+
func TestRelativeURL(t *testing.T) {
224+
helloGET := &openapi3.Operation{Responses: openapi3.NewResponses()}
225+
doc := &openapi3.T{
226+
Servers: openapi3.Servers{
227+
&openapi3.Server{
228+
URL: "/api/v1",
229+
},
230+
},
231+
Paths: openapi3.Paths{
232+
"/hello": &openapi3.PathItem{
233+
Get: helloGET,
234+
},
235+
},
236+
}
237+
router, err := NewRouter(doc)
238+
require.NoError(t, err)
239+
req, err := http.NewRequest(http.MethodGet, "https://example.com/api/v1/hello", nil)
240+
require.NoError(t, err)
241+
route, _, err := router.FindRoute(req)
242+
require.NoError(t, err)
243+
require.Equal(t, "/hello", route.Path)
244+
}

0 commit comments

Comments
 (0)