fix: preserve URL params when middleware rewrites Request.URL.Path - #13
Open
sonjaq wants to merge 1 commit into
Open
fix: preserve URL params when middleware rewrites Request.URL.Path#13sonjaq wants to merge 1 commit into
sonjaq wants to merge 1 commit into
Conversation
When middleware rewrites r.URL.Path, chi.URLParam() returned empty because RouteContext.RoutePath held stale path data. Fixed by tracking routePathSource and mountPrefix on Context — when routeHTTP detects r.URL.Path has diverged, it recomputes RoutePath from the updated path, stripping mount prefix for sub-routers. 8 tests pass covering: basic rewrite, sub-router mount, wildcard, nested params, backward compat without middleware. Co-authored-by: Sonja Leaf <sonjaq@users.noreply.github.com> Co-authored-by: Cliff <cliff@hermes.ai>
|
😅 Unfortunately there are no rewards left to claim in this issue! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes the URL parameter loss described in #1 where
chi.URLParam()returns empty when middleware rewritesr.URL.Path.Root Cause
When middleware rewrites
r.URL.Path,RouteContext.RoutePathholds stale path data from before the rewrite. The routing tree matches against the old path, so URL parameters extracted from the route pattern don't match the rewritten path.Approach
routePathSourceandmountPrefixfields toContextto track the originalr.URL.Pathand mount prefixrouteHTTPdetects thatr.URL.Pathhas diverged fromroutePathSource(meaning middleware rewrote it), it recomputesRoutePathfrom the updatedr.URL.Path, stripping the mount prefix for sub-routerschi.URLParam()works against the correct (rewritten) pathTesting
8 tests pass with
go test -v ./...:TestMiddlewarePathRewriteURLParams— primary test from the issueTestMiddlewarePathRewriteWithSubRouter— mounted sub-router with middleware rewriteTestMiddlewarePathRewriteWildcard— wildcard/*route after rewriteTestNoMiddlewareRewriteBackwardCompat— no-rewrite middleware still worksTestBasicRoutingNoMiddleware— basic routing without middlewareTestMiddlewarePathRewriteNestedParams— multiple params after rewriteTestSubRouterNoMiddlewareBackwardCompat— sub-router mount without middlewareTestMiddlewarePathRewriteWithSubRouterPrefix— sub-router with prefix rewriteTeam
This PR was collaboratively developed by Sonja Leaf (@sonjaq) and Cliff (AI agent, Hermes by Nous Research).
Cliff handled code analysis, implementation, and testing under Sonja's direction and review.
/claim #1