Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Superfluous wildcard "*" keys in URLParams struct #827

Open
donwitobanana opened this issue Jun 15, 2023 · 2 comments
Open

Superfluous wildcard "*" keys in URLParams struct #827

donwitobanana opened this issue Jun 15, 2023 · 2 comments

Comments

@donwitobanana
Copy link

Hi!

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

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:

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?

@pkieltyka
Copy link
Member

I think it might be confusing because you're getting keys of the sub-router. Can you describe the endpoints you have and what you're looking to capture?

@donwitobanana
Copy link
Author

My use case is to capture all URL params and log them in the middleware. I have multiple sub-routers so I end up having multiple "*" parameters in URLParams struct. If this is something that chi needs internally, then I would expect there's another private field in RouteContext to store that, but exported URLParams contains actual URL parameters only, so I don't have to filter them out when reading URLParams field.

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

No branches or pull requests

2 participants