Repository navigation
fix(tree): preserve trailing slash redirect when wildcard and common prefix routes coexist (#4890) - #4891
Open
rajpreetcodes wants to merge 1 commit into
Open
fix(tree): preserve trailing slash redirect when wildcard and common prefix routes coexist (#4890)#4891rajpreetcodes wants to merge 1 commit into
rajpreetcodes wants to merge 1 commit into
Conversation
…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 Report✅ All modified and coverable lines are covered by tests. 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
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
DVLens
approved these changes
Oct 14, 2026
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. |
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.
Pull Request Checklist
masterbranch.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 checkedn.handlers == niland 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
tree.go, whenpath == prefix, inspectn.indicesfor'/'to check if a valid trailing slash child exists before backtracking.value.tsracross wildcard backtracking passes while ensuring that exact handler matches clearvalue.tsr = false.tree_test.goandroutes_test.gocovering common prefix static routes with wildcard routes.