Skip to content

fix: preserve URL params when middleware rewrites Request.URL.Path - #13

Open
sonjaq wants to merge 1 commit into
madalynerlge2:mainfrom
sonjaq:fix/url-param-loss-on-path-rewrite
Open

fix: preserve URL params when middleware rewrites Request.URL.Path#13
sonjaq wants to merge 1 commit into
madalynerlge2:mainfrom
sonjaq:fix/url-param-loss-on-path-rewrite

Conversation

@sonjaq

@sonjaq sonjaq commented Aug 7, 2026

Copy link
Copy Markdown

Summary

Fixes the URL parameter loss described in #1 where chi.URLParam() returns empty when middleware rewrites r.URL.Path.

Root Cause

When middleware rewrites r.URL.Path, RouteContext.RoutePath holds 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

  • Added routePathSource and mountPrefix fields to Context to track the original r.URL.Path and mount prefix
  • When routeHTTP detects that r.URL.Path has diverged from routePathSource (meaning middleware rewrote it), it recomputes RoutePath from the updated r.URL.Path, stripping the mount prefix for sub-routers
  • This ensures chi.URLParam() works against the correct (rewritten) path

Testing

8 tests pass with go test -v ./...:

  • TestMiddlewarePathRewriteURLParams — primary test from the issue
  • TestMiddlewarePathRewriteWithSubRouter — mounted sub-router with middleware rewrite
  • TestMiddlewarePathRewriteWildcard — wildcard /* route after rewrite
  • TestNoMiddlewareRewriteBackwardCompat — no-rewrite middleware still works
  • TestBasicRoutingNoMiddleware — basic routing without middleware
  • TestMiddlewarePathRewriteNestedParams — multiple params after rewrite
  • TestSubRouterNoMiddlewareBackwardCompat — sub-router mount without middleware
  • TestMiddlewarePathRewriteWithSubRouterPrefix — sub-router with prefix rewrite

Team

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

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>
@opirebot

opirebot Bot commented Aug 7, 2026

Copy link
Copy Markdown

😅 Unfortunately there are no rewards left to claim in this issue!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant