Skip to content

fix(tree): preserve trailing slash redirect when wildcard and common prefix routes coexist (#4890) - #4891

Open
rajpreetcodes wants to merge 1 commit into
gin-gonic:masterfrom
rajpreetcodes:fix/tsr-wildcard-common-prefix
Open

rajpreetcodes wants to merge 1 commit into
gin-gonic:masterfrom
rajpreetcodes:fix/tsr-wildcard-common-prefix

Conversation

@rajpreetcodes

@rajpreetcodes rajpreetcodes commented Oct 14, 2026 •

Copy link
Copy Markdown

Pull Request Checklist

  • Open your pull request against the master branch.
  • All tests pass in available continuous integration systems (e.g., GitHub Actions).
  • Tests are added or modified as needed to cover code changes.
  • If the pull request introduces a new feature, the feature is documented in the docs/doc.md.

Description

Fixes #4890

Problem

When static routes share a common prefix (such as /aa/ and /aa_id) and a wildcard route is also registered (such as /:c/bb), the radix tree creates an intermediate prefix node (aa) without registered handlers.

When matching a request path equal to the intermediate prefix (such as /aa), the router previously checked n.handlers == nil and immediately rolled back to skipped wildcard nodes without verifying if the intermediate node has a trailing slash child with a valid handler. When the wildcard route failed to match, the trailing slash redirect (TSR) recommendation was overwritten or lost, resulting in 404 Not Found instead of 301 Moved Permanently.

Solution

  1. In tree.go, when path == prefix, inspect n.indices for '/' to check if a valid trailing slash child exists before backtracking.
  2. Preserve value.tsr across wildcard backtracking passes while ensuring that exact handler matches clear value.tsr = false.
  3. Add unit tests in both tree_test.go and routes_test.go covering common prefix static routes with wildcard routes.

…prefix routes coexist

When multiple static routes share a common prefix and a wildcard route is also registered, the radix tree creates an intermediate prefix node without handlers. During path matching where the requested path equals the intermediate prefix, the search previously rolled back to skipped wildcard nodes without checking if the intermediate node contains a trailing slash child with a valid handler. If the backtracked wildcard route did not match, the trailing slash redirect recommendation was lost and returned 404 instead of 301.

This commit checks and preserves trailing slash recommendations on intermediate prefix nodes before backtracking to wildcard nodes, ensuring requests redirect correctly when static trailing slash routes exist while preserving exact matches for wildcard routes.

Fixes gin-gonic#4890
@codecov

codecov Bot commented Oct 14, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.71%. Comparing base (0f09c3a) to head (8980f84).

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #4891      +/-   ##
==========================================
+ Coverage   98.65%   98.71%   +0.06%     
==========================================
  Files          48       48              
  Lines        3188     3192       +4     
==========================================
+ Hits         3145     3151       +6     
+ Misses         43       41       -2     
Flag Coverage Δ
--ldflags="-checklinkname=0" -tags sonic 98.70% <100.00%> (+0.06%) ⬆️
-tags go_json 98.57% <100.00%> (+<0.01%) ⬆️
-tags nomsgpack 98.63% <100.00%> (+<0.01%) ⬆️
go-1.26 98.40% <100.00%> (+0.06%) ⬆️
go-1.27 98.67% <100.00%> (+<0.01%) ⬆️
macos-latest 98.65% <100.00%> (+<0.01%) ⬆️
ubuntu-latest 98.71% <100.00%> (+0.06%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@rajpreetcodes

Copy link
Copy Markdown
Author

@appleboy All 26 test suites (unit tests, race detection, benchmarks, sonic/go_json tags across Go 1.26 and Go 1.27 on macOS/Ubuntu) are passing with 100% patch coverage. The Trivy scan failure is from the upstream golang.org/x/net CVE present on master. Ready for review and merge.

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.

RedirectTrailingSlash returns 404 instead of 301 when a wildcard route and multiple static routes with the same prefix are registered

2 participants