Skip to content

Routing bug with empty path component #609

@yurivish

Description

@yurivish

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

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):
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.")
	}
}

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions