Skip to content

Commit 2326c8e

Browse files
authored
fix handling no match access selected path #467 (#468)
* fix handling no match access selected path * remove obsolete field
1 parent 4d03210 commit 2326c8e

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

request.go

+13-5
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,10 @@ var defaultRequestContentType string
1313

1414
// Request is a wrapper for a http Request that provides convenience methods
1515
type Request struct {
16-
Request *http.Request
17-
pathParameters map[string]string
18-
attributes map[string]interface{} // for storing request-scoped values
19-
selectedRoutePath string // root path + route path that matched the request, e.g. /meetings/{id}/attendees
20-
selectedRoute *Route
16+
Request *http.Request
17+
pathParameters map[string]string
18+
attributes map[string]interface{} // for storing request-scoped values
19+
selectedRoute *Route // is nil when no route was matched
2120
}
2221

2322
func NewRequest(httpRequest *http.Request) *Request {
@@ -114,11 +113,20 @@ func (r Request) Attribute(name string) interface{} {
114113
}
115114

116115
// SelectedRoutePath root path + route path that matched the request, e.g. /meetings/{id}/attendees
116+
// If no route was matched then return an empty string.
117117
func (r Request) SelectedRoutePath() string {
118+
if r.selectedRoute == nil {
119+
return ""
120+
}
121+
// skip creating an accessor
118122
return r.selectedRoute.Path
119123
}
120124

121125
// SelectedRoute returns a reader to access the selected Route by the container
126+
// Returns nil if no route was matched.
122127
func (r Request) SelectedRoute() RouteReader {
128+
if r.selectedRoute == nil {
129+
return nil
130+
}
123131
return routeAccessor{route: r.selectedRoute}
124132
}

web_service_test.go

+4
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,10 @@ func newSelectedRouteTestingService() *WebService {
383383
}
384384

385385
func selectedRouteChecker(req *Request, resp *Response) {
386+
if req.SelectedRoute() == nil {
387+
resp.InternalServerError()
388+
return
389+
}
386390
if req.SelectedRoutePath() != pathGetFriends {
387391
resp.InternalServerError()
388392
}

0 commit comments

Comments
 (0)