Skip to content

fix: synchronize RoutePath with mutated URL.Path to prevent parameter loss - #9

Open
jamboriu wants to merge 1 commit into
madalynerlge2:mainfrom
jamboriu:fix/path-rewrite-params
Open

fix: synchronize RoutePath with mutated URL.Path to prevent parameter loss#9
jamboriu wants to merge 1 commit into
madalynerlge2:mainfrom
jamboriu:fix/path-rewrite-params

Conversation

@jamboriu

@jamboriu jamboriu commented Aug 3, 2026

Copy link
Copy Markdown

Description

This PR fixes the URL parameter loss issue that occurs when middlewares rewrite r.URL.Path (e.g., path rewriting, strip prefixes, URL normalization) before routing to sub-routers or handlers (resolving issue #1).

Cause

In the go-chi/chi routing cycle, sub-routing depends on rctx.RoutePath (which tracks the remaining path to match). However, standard middlewares modifying r.URL.Path do not update rctx.RoutePath, leading to out-of-sync routing contexts where chi.URLParam queries fail.

Solution

  1. Context Sincronization: Added an internal URLPath tracking field to chi.Context (with proper cleanup in Reset()) to detect when r.URL.Path has been mutated.
  2. Depth Heuristics: If the path has diverged, the router calculates the consumed routing depth (slash count) of the parent context and extracts the remaining path from the new r.URL.Path accordingly. This handles both full path rewrites and partial/versioned prefix updates (e.g., /api/v1 -> /api/v2).
  3. Integration Tests: Added TestMiddlewarePathRewriteURLParams, TestSubrouterMiddlewarePathRewriteURLParams, and TestSubrouterPartialPathRewriteURLParams verifying parameter extraction under various rewrite depths.

Testing and Verification

  • Full suite tests executed with the Go race detector enabled (go test -race ./...): All PASS (no regressions, no races).
  • Internal structural audit completed successfully (10/10 integrity score).

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