Skip to content

Commit 77044e2

Browse files
Copilotgaby
andauthored
🐛 fix deterministic case-insensitive Route.URL key selection
Agent-Logs-Url: https://github.com/gofiber/fiber/sessions/6b2ae8da-f708-42b5-9e9b-7e3795de0ec5 Co-authored-by: gaby <835733+gaby@users.noreply.github.com>
1 parent 9f828e7 commit 77044e2

2 files changed

Lines changed: 13 additions & 3 deletions

File tree

router.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,14 +127,16 @@ func buildRouteURL(route *Route, params Map) (string, error) {
127127
if val, found = params[segment.ParamName]; !found && !route.caseSensitive {
128128
// Fall back to a case-insensitive match using a deterministic winner
129129
var matchedKey string
130+
foundMatch := false
130131
for key := range params {
131-
if utils.EqualFold(key, segment.ParamName) && (!found || key < matchedKey) {
132+
if utils.EqualFold(key, segment.ParamName) && (!foundMatch || key < matchedKey) {
132133
matchedKey = key
133-
found = true
134+
foundMatch = true
134135
}
135136
}
136-
if found {
137+
if foundMatch {
137138
val = params[matchedKey]
139+
found = true
138140
}
139141
}
140142

router_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2449,6 +2449,14 @@ func Test_Route_URL(t *testing.T) {
24492449
require.NoError(t, err)
24502450
require.Equal(t, "/user/fiber", url)
24512451

2452+
// When multiple keys case-fold to the same param name and no exact key
2453+
// exists, the lexicographically-smallest key wins deterministically.
2454+
for range 50 {
2455+
url, err = route.URL(Map{"nAme": "second", "Name": "first"})
2456+
require.NoError(t, err)
2457+
require.Equal(t, "/user/first", url)
2458+
}
2459+
24522460
// When multiple keys case-fold to the same param name, prefer the exact match.
24532461
url, err = route.URL(Map{"name": "exact", "Name": "fallback"})
24542462
require.NoError(t, err)

0 commit comments

Comments
 (0)