go-chi / go-chi/chi

Superfluous wildcard "*" keys in `URLParams` struct

Open
#827 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
Go
Stars
22.8k
Forks
1.2k
Avg merge
5h 17m
Merged PRs (30d)
10

Description

Hi!

I want to get all path params for a given request having a router that is mounted onto another one:

```go
func main() {
r := chi.NewRouter()
r.Get("/{id}", myHandler)

baseRouter := chi.NewRouter()
baseRouter.Mount("/base", r)

go http.ListenAndServe(":8080", baseRouter)

http.DefaultClient.Get("http://localhost:8080/base/123")
}

func myHandler(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
urlParams := chi.RouteContext(ctx).URLParams

for i, key := range urlParams.Keys {
fmt.Printf("key: \"%s\", value: \"%s\"\n", key, urlParams.Values[i])
}
}
```
The output is:
```bash
key: "*", value: ""
key: "id", value: "123"
```

I'm expecting to get `id` parameter only, but what I get additionally is this empty wildcard "*" parameter. This doesn't happen when using router without mounting.

Is there a way to get only defined path parameters other than manually filtering out wildcard ones? I'm not sure if this is a bug or not, but shouldn't this be handled in the internal `urlParams` struct field and not in the exported one at least?

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the mounted-router example in main and inspect how chi.RouteContext(ctx).URLParams is populated for the request handled by myHandler. Trace the mounted route's parameter handling and verify the resulting Keys and Values. Done means the request reports only the defined "id" parameter, without an empty wildcard entry.

Written by the indexing model from the issue text.

Assessment

Tech stack
go
Domain
api, backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.