go-chi / go-chi/chi

Wildcard matching with regex pattern is not correctly parsing the path values

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

Description

Combining two related issues here. Please let me know if that is not fine, will create separate issues.

## Issue 1: Path values are not correct

*Endpoint*: `/payments/{id: [0-9]{4}}*`

*Request Path*: `/payments/4765_payID/status`

Expected Path Keys and Values: `{ "id" : "4765", "*" : "_payID/status" }`

Actual Path Keys and Values: `{ "id": "", "*": "4765_payID/status" }

I am sure there are other ways to register the same route like `/payments/{id: [0-9]{4}}{local_wildcard}/*` to handle the above cases but this doesn't allow `/payments/1234_payID` work around is to define two routes in that case. We are facing this issue as we are migrating from another router ( lua based ) to Chi and we want to keep the routes backward compatible. Does it make sense to support this usecase ? If so then i can take a initial look at this and raise a PR for the same.

## Issue 2: Extension to the Issue 1, Wildcard endpoint is ignored

- *Endpoint 1* : `/payments/{id: [0-9]{4}}*`
- *Endpoint 2*: `/payments/{id: [0-9]{4}}/status`

*Request Path*: `/payments/4765_payID/not_status`

*Expected Status Code* : `200` ( Path Matched )

*Actual Status Code*: `404` ( No route matched )

When there are multiple childs along with wildcard, it follows the path without wildcard in this case. I was thinking why not add `*` also in the sort function [here](https://github.com/go-chi/chi/blob/67be7d9cafdaeb4e04e887ff78d09e030ee43b00/tree.go#L814) to push the wildcards to the last ( tried locally, works fine). Please share your thoughts, i can raise the PR for the same

Contributor guide

Open the contributing guide

Research direction

Start by reproducing both endpoint examples in the router and reading tree.go around the sort function at line 814. Trace how the regex path parameter and wildcard are parsed and ordered. Done means the reported path keys, values, and route status match the expected results without breaking the related route behavior.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.