Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

fix for issue #429 #431

Merged
merged 1 commit into from
Dec 28, 2018
Merged

fix for issue #429 #431

merged 1 commit into from
Dec 28, 2018

Conversation

tomare
Copy link
Contributor

@tomare tomare commented Dec 20, 2018

fix for issue #429

@elithrar
Copy link
Contributor

elithrar commented Dec 20, 2018 via email

@tomare
Copy link
Contributor Author

tomare commented Dec 20, 2018

Condition:

  1. one route has more than 2 subrouters.
  2. bind middleware for each subrouter.
  3. the request matches the second subrouter.

Issue and reason:
In function func (r *Router) Match(req *http.Request, match *RouteMatch) bool
a. search for all route (r.routes)
b. share the match
c. after calling match for the first subrouter, the match.MatchErr will be set to no matching route was found, and then goes to the second subrouter.
d. if second subrouter was matched, then in function func (r *Route) Match(...), the match.MatchErr is not nil, and will not be set to nil. and Match return true. but the mat.MatchErr is not nil
e. then the middleware of second subrouter will not be applied.

Fix:
reset the match.MatchErr before check the match. because share the match object for route, if we have multi subrouters in one route, the MatchErr will be contaminated by the previous subrouter.

@maplebed maplebed mentioned this pull request Dec 20, 2018
@maplebed
Copy link

💚 thank you for this patch.

@elithrar elithrar requested review from kisielk and elithrar December 20, 2018 23:03
middleware_test.go Show resolved Hide resolved

router.ServeHTTP(rw, req)
if rw.Body.String() != first {
t.Fatal("First Middleware doesn't work")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

t.Failf("Middleware did not run: expected %s middleware to write a response (got %s)", first, body)


router.ServeHTTP(rw, req)
if rw.Body.String() != second {
t.Fatal("Second Middleware doesn't work")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

t.Failf("Middleware did not run: expected %s middleware to write a response (got %s)", second, body)

route.go Outdated
@@ -43,6 +43,8 @@ func (r *Route) Match(req *http.Request, match *RouteMatch) bool {
return false
}

//reset MatchErr
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tell a story / why - it's obvious that we reset it :)

Instead, say: "Set MatchErr to nil to prevent subsequent matching subrouters from failing to run middleware. If not reset, the middleware would see a non-nil MatchErr and be skipped, even when there was a matching route"

@tomare tomare force-pushed the master branch 2 times, most recently from 446e65c to e0ab5d0 Compare December 21, 2018 16:24
add middlewareOnMultiSubrouter test

remove blank line
@maplebed
Copy link

::bump:: I'd really love to see this merged. Is the only thing we're waiting on a small addition to the test (to add a third non-matching route)?

@elithrar
Copy link
Contributor

Yep - the tests need to be comprehensive. It’s clear that our already large test suite doesn’t cover all behaviours, especially with complex subrouter + middleware interactions.

The burden on PRs to provide more complete test cases will only increase 😉

@elithrar
Copy link
Contributor

Looks like the comment was addressed, just didn’t see a reply comment. Let me review in full at a computer later today before merging.

@elithrar elithrar merged commit ef912dd into gorilla:master Dec 28, 2018
@elithrar
Copy link
Contributor

Thanks @tomare!

@bminer
Copy link

bminer commented Feb 11, 2019

I don't see this in master branch. When will this be released? How do I go get it?

@tomare
Copy link
Contributor Author

tomare commented Feb 11, 2019

I don't see this in master branch. When will this be released? How do I go get it?

It was released and changed on later commit.

@bminer
Copy link

bminer commented Feb 15, 2019

@tomare - Sorry, can you provide more info? Why was it changed? Is there an associated issue, so I can read up on it?

@tomare
Copy link
Contributor Author

tomare commented Feb 15, 2019

@tomare - Sorry, can you provide more info? Why was it changed? Is there an associated issue, so I can read up on it?

you can check out on this commit.
08e7f80

# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants