fix(router): walk parent tree for RouteNotFound handler in groups#2917
fix(router): walk parent tree for RouteNotFound handler in groups#2917lyydsheep wants to merge 1 commit intolabstack:masterfrom
Conversation
|
Tests are missing. Please add at least one testcase with example routing tree we can review this. |
|
Thanks for the feedback! I have added
The test tree registers a |
aldas
left a comment
There was a problem hiding this comment.
When I run these tests on unpatched Echo (v5.0.4) they all pass - meaning they do not test the feature in this PR. I would expect at least some of them to fails in unpatched version.
=== RUN TestRouterRouteNotFoundHandlerInGroup
=== RUN TestRouterRouteNotFoundHandlerInGroup/root_RouteNotFound_handler_fires_for_group_sub-path_with_wrong_method
--- PASS: TestRouterRouteNotFoundHandlerInGroup/root_RouteNotFound_handler_fires_for_group_sub-path_with_wrong_method (0.00s)
=== RUN TestRouterRouteNotFoundHandlerInGroup/group_RouteNotFound_handler_fires_for_group_sub-path_with_wrong_method
--- PASS: TestRouterRouteNotFoundHandlerInGroup/group_RouteNotFound_handler_fires_for_group_sub-path_with_wrong_method (0.00s)
=== RUN TestRouterRouteNotFoundHandlerInGroup/root_RouteNotFound_fires_when_no_group-level_handler_defined
--- PASS: TestRouterRouteNotFoundHandlerInGroup/root_RouteNotFound_fires_when_no_group-level_handler_defined (0.00s)
=== RUN TestRouterRouteNotFoundHandlerInGroup/fallback_to_methodNotAllowed_when_no_RouteNotFound_handler_defined
--- PASS: TestRouterRouteNotFoundHandlerInGroup/fallback_to_methodNotAllowed_when_no_RouteNotFound_handler_defined (0.00s)
--- PASS: TestRouterRouteNotFoundHandlerInGroup (0.00s)
PASS
|
Thank you for the thorough review, @aldas! You are absolutely right — the previous tests used The fix is only exercised when I have updated the tests accordingly:
These revised tests should fail on unpatched |
This test verifies the fix for issue labstack#2485 where RouteNotFound handlers registered at parent levels (root or groups) were not being found when a route exists but with wrong HTTP method. The test should: - FAIL on unpatched code (falls back to methodNotAllowedHandler) - PASS with the fix (finds parent's RouteNotFound handler) This demonstrates that the parent-walk logic correctly traverses up the node tree to find RouteNotFound handlers defined at higher levels. Signed-off-by: lyydsheep <2230561977@qq.com>
7cf56a4 to
f4f7d88
Compare
|
Where this requirement comes from
In your examples you are setting up is not this behavior expected:
NB: please do not use LLM and think this though by yourself. |
|
After thorough testing, I found that Echo v5 does not exhibit the bug described in issue #2485. The original issue was reported against I apologize for the noise. Closing this PR as it is not needed for v5. |
Summary
Fixes #2485
When a route exists in the router but the HTTP method is not allowed, the router previously always fell back to
methodNotAllowedHandler. However, this bypassed anyRouteNotFoundhandler registered at a parent or root group level.Root Cause
In
DefaultRouter.Route(), whencurrentNode.isHandleris true (path matched but method not), the code immediately setrInfo = methodNotAllowedRouteInfowithout checking if any parent node had anotFoundHandlerregistered.Fix
Traverse the parent node chain to look for a
notFoundHandler. If found, use it; otherwise fall back to the existingmethodNotAllowedHandlerbehavior.Test
Existing route tests pass. The fix ensures
RouteNotFoundhandlers registered at a group level are properly invoked for sub-paths.Signed-off-by: lyydsheep 2230561977@qq.com