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

Fix : invalid params when using ignore trailing slash #34

Merged
merged 5 commits into from
Jul 4, 2024

Conversation

tigerwill90
Copy link
Owner

@tigerwill90 tigerwill90 commented Jul 4, 2024

Description

This PR fixes an issue related to the new "ignore trailing slash" feature. Unlike the "redirect trailing slash" option, when "ignore trailing slash" is enabled, the correct route handler is expected to be called with the appropriate parameters for the matched route. However, this wasn't always the case due to the evaluation of different paths with wildcard segments.

Problem

Given the following routes:

  • /api/{a}
  • /api/foo/{b}

When a request is made to /api/foo/bar/ with "ignore trailing slash" enabled, the expected match is /api/foo/{b}, but the parameters were not always correctly assigned. The evaluation process was as follows (also see priority rules):

  1. /api/foo/{b}: Records "bar" as the parameter for {b}, and marks it for a trailing slash recommendation (tsr).
  2. /api/{a}: Matches /api/foo, drops {b} parameter, and records "foo" as the parameter for {a}.
  3. Since no previous match, falls back to the first tsr, /api/foo/{b}, and calls the handler.

During step 2, the parameters recorded in step 1 were lost, resulting in the correct handler being called but with incorrect parameters.

Solution

The fix ensures that parameters are preserved when encountering the first trailing slash recommendation (tsr). This way, when falling back to the initially matched route, the correct parameters are retained and passed to the handler.

Benchmark Differential

Expect no impact on performance (small optimisation have been achieved on the lookup function.).

goos: linux
goarch: amd64
pkg: github.com/tigerwill90/fox
cpu: Intel(R) Core(TM) i9-9900K CPU @ 3.60GHz
                    │   old.txt   │              new.txt               │
                    │   sec/op    │   sec/op     vs base               │
StaticAll-16          11.36µ ± 2%   11.01µ ± 1%  -3.01% (p=0.000 n=10)
GithubParamsAll-16    84.29n ± 1%   82.11n ± 1%  -2.58% (p=0.000 n=10)
OverlappingRoute-16   96.45n ± 1%   98.48n ± 2%  +2.10% (p=0.007 n=10)
StaticParallel-16     9.398n ± 4%   9.521n ± 1%       ~ (p=0.239 n=10)
CatchAll-16           34.06n ± 6%   31.56n ± 2%  -7.34% (p=0.000 n=10)
CatchAllParallel-16   5.732n ± 7%   5.803n ± 6%       ~ (p=0.529 n=10)
CloneWith-16          65.94n ± 1%   66.19n ± 2%       ~ (p=0.684 n=10)
geomean               73.11n        72.25n       -1.18%

@tigerwill90 tigerwill90 added the wip label Jul 4, 2024
Copy link

codecov bot commented Jul 4, 2024

Codecov Report

Attention: Patch coverage is 83.87097% with 10 lines in your changes missing coverage. Please review.

Project coverage is 87.15%. Comparing base (74b75c6) to head (5a8c0e4).

Files Patch % Lines
tree.go 84.00% 4 Missing and 4 partials ⚠️
context.go 60.00% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master      #34      +/-   ##
==========================================
- Coverage   87.38%   87.15%   -0.24%     
==========================================
  Files          15       15              
  Lines        1911     1939      +28     
==========================================
+ Hits         1670     1690      +20     
- Misses        189      193       +4     
- Partials       52       56       +4     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@tigerwill90 tigerwill90 merged commit 05ec42f into master Jul 4, 2024
6 of 8 checks passed
@tigerwill90 tigerwill90 deleted the feat/ingore-ts-invalid-params branch July 4, 2024 14:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant