go-chi / go-chi/chi

Routing bug with empty path component

Open
#609 7 comments 5 reactions 1 assignee Claimed by @helios741 View on GitHub
routing
Dominant language
Go
Stars
22.8k
Forks
1.2k
Avg merge
5h 17m
Merged PRs (30d)
10

Description

Hi, I found a surprising (to me) edge case regarding routes with empty path components:

```go
package main

import (
"fmt"
"net/http"

"github.com/go-chi/chi"
)

func empty(w http.ResponseWriter, r *http.Request) {}

func main() {
r := chi.NewRouter()
r.Get(`/{:[a-z]+}/`, empty)
ctx := chi.NewRouteContext()
if r.Match(ctx, "GET", "//") {
fmt.Println("The route matched the URL.")
}
}
```

This prints `The route matched the URL.` even though there was clearly no match for the regular expression. My understanding of the semantics of a regex filter is that the route will match if and only if the regex filter matches the path component, but that's not what seems to be happening here.

I'd be curious if this is behaving as it should, and if so, if there's a good alternative to filtering out empty path components, since requirning a match on `.+` doesn't work.

Thank you!

P. S. I think this a bug, since omitting the trailing slash from both the pattern and URL should not change the behavior but doing so makes the route no longer match (click to expand):

```go
package main

import (
"fmt"
"net/http"

"github.com/go-chi/chi"
)

func empty(w http.ResponseWriter, r *http.Request) {}

func main() {
r := chi.NewRouter()
r.Get(`/{:[a-z]+}`, empty)
ctx := chi.NewRouteContext()
if r.Match(ctx, "GET", "/") {
fmt.Println("The route matched the URL.")
}
}
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.