Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
daa7dc9
Initial plan
Claude Apr 7, 2026
db31cdf
Add URL() method to Route for generating URLs with parameters
Claude Apr 7, 2026
26faa1d
fix: address review feedback on Route.URL() - deterministic lookup, c…
Copilot Apr 7, 2026
6b37d8d
fix: nil guard, deterministic case-insensitive fallback, mounted rout…
Copilot Apr 7, 2026
434c6f2
refactor: extract shared buildRouteURL helper, add deterministic fall…
Copilot Apr 7, 2026
9fef978
fix: refine Route.URL greedy fallback and docs
Copilot Apr 7, 2026
be74496
test: cover default greedy fallback path
Copilot Apr 7, 2026
6d6740e
perf: avoid greedy fallback slice allocations
Copilot Apr 7, 2026
6e56f99
docs: clarify greedy fallback helper behavior
Copilot Apr 7, 2026
2e241ed
docs: explain Route.URL fallback edge cases
Copilot Apr 7, 2026
a7f3f1f
perf: precompute greedy fallback map keys
Copilot Apr 8, 2026
9b47da0
refactor: rename default greedy fallback keys
Copilot Apr 8, 2026
389a0f6
🐛 fix: make Route.URL callable on GetRoute values
Copilot Apr 8, 2026
3384a5b
🧹 chore: clarify Route.URL nolint rationale
Copilot Apr 8, 2026
afc8c17
🧹 chore: fix Route.URL comment casing
Copilot Apr 8, 2026
5ac4c3f
Merge branch 'main' into claude/fix-getroute-path-param
gaby Apr 10, 2026
3c20541
Merge branch 'main' into claude/fix-getroute-path-param
ReneWerner87 Apr 11, 2026
a44230b
Merge branch 'main' into claude/fix-getroute-path-param
gaby Apr 11, 2026
55afb07
Update res.go
gaby Apr 11, 2026
9f828e7
Update router.go
gaby Apr 11, 2026
77044e2
🐛 fix deterministic case-insensitive Route.URL key selection
Copilot Apr 11, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions docs/api/app.md
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,8 @@ func main() {

This method retrieves a route by its name.

The returned `Route` can be inspected or used to generate a URL directly with `route.URL(params)`.

```go title="Signature"
func (app *App) GetRoute(name string) Route
```
Expand All @@ -554,12 +556,17 @@ func main() {
app := fiber.New()

app.Get("/", handler).Name("index")
app.Get("/user/:name/:id", handler).Name("user")

route := app.GetRoute("index")

data, _ := json.MarshalIndent(route, "", " ")
fmt.Println(string(data))

userRoute := app.GetRoute("user")
location, _ := userRoute.URL(fiber.Map{"name": "john", "id": 1})
fmt.Println(location) // /user/john/1

log.Fatal(app.Listen(":3000"))
}
```
Expand Down
2 changes: 2 additions & 0 deletions docs/whats_new.md
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,8 @@ app.RouteChain("/api").RouteChain("/user/:id?")

You can find more information about `app.RouteChain` and `app.Route` in the API documentation ([RouteChain](./api/app#routechain), [Route](./api/app#route)).

Named routes retrieved with `app.GetRoute(name)` also support `route.URL(params)` for generating relative URLs directly from the route definition, including parameter substitution for named, wildcard (`*`), and plus (`+`) segments.

### Domain routing

`Domain` creates a router scoped to a specific hostname pattern. Routes registered through the returned `Router` only match requests whose hostname (from `c.Hostname()`) matches the pattern. When `TrustProxy` is enabled and the proxy is trusted (as defined by [`TrustProxyConfig`](./api/app#trustproxyconfig)), the hostname may be derived from the `X-Forwarded-Host` header. Be sure to configure `TrustProxyConfig` to restrict which proxies are trusted and prevent header spoofing when enabling `TrustProxy`.
Expand Down
2 changes: 0 additions & 2 deletions path.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,6 @@ const (
var (
// slash has a special role, unlike the other parameters it must not be interpreted as a parameter
routeDelimiter = []byte{slashDelimiter, '-', '.'}
// list of greedy parameters
greedyParameters = []byte{wildcardParam, plusParam}
// list of chars for the parameter recognizing
parameterStartChars = [256]bool{
wildcardParam: true,
Expand Down
33 changes: 4 additions & 29 deletions res.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package fiber

import (
"bufio"
"bytes"
"fmt"
"html/template"
"io"
Expand Down Expand Up @@ -646,38 +645,14 @@ func (r *DefaultRes) ViewBind(vars Map) error {
return r.c.ViewBind(vars)
}

// getLocationFromRoute get URL location from route using parameters
// getLocationFromRoute gets the URL location from a route using parameters.
// Nil receivers and missing routes return ErrNotFound to match Route.URL semantics.
func (r *DefaultRes) getLocationFromRoute(route *Route, params Map) (string, error) {
if route == nil || route.Path == "" {
if r == nil || route == nil || route.Path == "" {
return "", ErrNotFound
}

app := r.c.app
buf := bytebufferpool.Get()
for _, segment := range route.routeParser.segs {
if !segment.IsParam {
_, err := buf.WriteString(segment.Const)
if err != nil {
return "", fmt.Errorf("failed to write string: %w", err)
}
continue
}

for key, val := range params {
isSame := key == segment.ParamName || (!app.config.CaseSensitive && utils.EqualFold(key, segment.ParamName))
isGreedy := segment.IsGreedy && len(key) == 1 && bytes.IndexByte(greedyParameters, key[0]) >= 0
if isSame || isGreedy {
_, err := buf.WriteString(utils.ToString(val))
if err != nil {
return "", fmt.Errorf("failed to write string: %w", err)
}
}
}
}
location := buf.String()
// release buffer
bytebufferpool.Put(buf)
return location, nil
return buildRouteURL(route, params)
}

// GetRouteURL generates URLs to named routes, with parameters. URLs are relative, for example: "/user/1831"
Expand Down
143 changes: 129 additions & 14 deletions router.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/gofiber/utils/v2"
utilsstrings "github.com/gofiber/utils/v2/strings"
"github.com/valyala/bytebufferpool"
"github.com/valyala/fasthttp"
)

Expand Down Expand Up @@ -58,11 +59,122 @@ type Route struct {
routeParser routeParser // Parameter parser

// Data for routing
use bool // USE matches path prefixes
mount bool // Indicated a mounted app on a specific route
star bool // Path equals '*'
root bool // Path equals '/'
autoHead bool // Automatically generated HEAD route
use bool // USE matches path prefixes
mount bool // Indicated a mounted app on a specific route
star bool // Path equals '*'
root bool // Path equals '/'
autoHead bool // Automatically generated HEAD route
caseSensitive bool // Whether parameter matching is case-sensitive
}

var (
defaultGreedyParameterKeys = []string{"*", "+"}
preferredWildcardGreedyParameters = []string{"*", "+"}
preferredPlusGreedyParameters = []string{"+", "*"}
)

// URL generates a URL from the route path and parameters.
// This method fills in the route parameters with the provided values.
// Parameter matching respects the app's CaseSensitive configuration:
// case-insensitive by default, case-sensitive when CaseSensitive is true.
//
// Example:
//
// app.Get("/user/:name/:id", handler).Name("user")
// url, err := app.GetRoute("user").URL(Map{"name": "john", "id": "123"})
// // Returns: "/user/john/123"
//
//nolint:gocritic // hugeParam: app.GetRoute returns a value, so URL must be callable on that value directly.
func (r Route) URL(params Map) (string, error) {
if r.Path == "" {
return "", ErrNotFound
}

return buildRouteURL(&r, params)
}

// buildRouteURL generates a URL from route segments and parameters.
// This shared helper is used by both Route.URL() and DefaultRes.getLocationFromRoute()
// to ensure consistent URL generation behavior across APIs.
//
// Parameter resolution uses a deterministic three-step lookup:
// 1. Exact key match on segment.ParamName
// 2. Case-insensitive fallback picking the lexicographically-smallest matching key (when !caseSensitive)
// 3. Greedy parameter fallback for wildcard (*) and plus (+) parameters
func buildRouteURL(route *Route, params Map) (string, error) {
if len(route.routeParser.segs) == 0 {
return route.Path, nil
}

buf := bytebufferpool.Get()
defer bytebufferpool.Put(buf)

for _, segment := range route.routeParser.segs {
if !segment.IsParam {
_, err := buf.WriteString(segment.Const)
if err != nil {
return "", fmt.Errorf("failed to write string: %w", err)
}
continue
}

var (
val any
found bool
)

// Prefer an exact parameter name match
if val, found = params[segment.ParamName]; !found && !route.caseSensitive {
// Fall back to a case-insensitive match using a deterministic winner
var matchedKey string
foundMatch := false
for key := range params {
if utils.EqualFold(key, segment.ParamName) && (!foundMatch || key < matchedKey) {
matchedKey = key
foundMatch = true
}
}
if foundMatch {
val = params[matchedKey]
found = true
}
Comment on lines +128 to +140
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

There's a bug in the case-insensitive parameter matching logic. The found variable from the outer scope is updated within the loop, which prevents the code from correctly finding the lexicographically smallest matching key as intended. Instead, it picks the first key that matches case-insensitively based on map iteration order, leading to non-deterministic behavior.

To fix this, you should use a local boolean flag within this block to track if a match has been found.

			// Fall back to a case-insensitive match using a deterministic winner
			var matchedKey string
			foundMatch := false
			for key := range params {
				if utils.EqualFold(key, segment.ParamName) && (!foundMatch || key < matchedKey) {
					matchedKey = key
					foundMatch = true
				}
			}
			if foundMatch {
				val = params[matchedKey]
				found = true
			}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot Analyse and apply this fix if it makes sense.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}

// For greedy parameters, fall back to generic greedy keys
if !found && segment.IsGreedy {
for _, greedyKey := range preferredGreedyParameters(segment.ParamName) {
if val, found = params[greedyKey]; found {
break
}
}
}

if found {
_, err := buf.WriteString(utils.ToString(val))
if err != nil {
return "", fmt.Errorf("failed to write string: %w", err)
}
}
}

return buf.String(), nil
}

// preferredGreedyParameters returns the generic greedy fallback lookup order
// for a route parameter name.
// Parameter names starting with '+' prefer '+' before '*', names starting with
// '*' prefer '*' before '+', and all other names fall back to the default order.
func preferredGreedyParameters(paramName string) []string {
if paramName != "" {
switch paramName[0] {
case plusParam:
return preferredPlusGreedyParameters
case wildcardParam:
return defaultGreedyParameterKeys
}
}

return defaultGreedyParameterKeys
}

func (r *Route) match(detectionPath, path string, params *[maxParams]string) bool {
Expand Down Expand Up @@ -369,18 +481,20 @@ func (app *App) addPrefixToRoute(prefix string, route *Route) *Route {
route.routeParser = parseRoute(prettyPath, app.customConstraints...)
route.root = false
route.star = false
route.caseSensitive = app.config.CaseSensitive

return route
}

func (*App) copyRoute(route *Route) *Route {
return &Route{
// Router booleans
use: route.use,
mount: route.mount,
star: route.star,
root: route.root,
autoHead: route.autoHead,
use: route.use,
mount: route.mount,
star: route.star,
root: route.root,
autoHead: route.autoHead,
caseSensitive: route.caseSensitive,

// Path data
path: route.path,
Expand Down Expand Up @@ -554,10 +668,11 @@ func (app *App) register(methods []string, pathRaw string, group *Group, handler
isRoot := pathClean == "/"

route := Route{
use: isUse,
mount: isMount,
star: isStar,
root: isRoot,
use: isUse,
mount: isMount,
star: isStar,
root: isRoot,
caseSensitive: app.config.CaseSensitive,

path: pathClean,
routeParser: parsedPretty,
Expand Down
Loading