π₯ feat: Add Support for Removing Routes
Description
- Add method to Remove Routes
- Updated register method to prevent duplicate routes.
Fixes #3098
Changes introduced
Type of change
- [x] New feature (non-breaking change which adds functionality)
- [x] Enhancement (improvement to existing features and functionality)
- [x] Documentation update (changes to documentation)
Thanks for opening this pull request! π Please check out our contributing guidelines. If you need help or want to chat with us, join us on Discord https://gofiber.io/discord
Walkthrough
The changes add dynamic route removal methods to the Fiber app, enabling route removal by path, name, or custom predicate. The router ensures normalized path handling and prevents duplicate route registrations. Extensive tests validate route removal, path normalization, concurrency, and handler counts. Documentation and migration notes are updated with usage examples.
Changes
| File(s) | Change Summary |
|---|---|
| router.go | Added normalizePath, RemoveRoute, RemoveRouteByName, RemoveRouteFunc, and internal deleteRoute methods. Updated route addition to prevent duplicates and handle middleware routes properly. Imported slices for slice comparisons. |
| router_test.go | Added helper functions and comprehensive tests for route removal by path, name, and function, path normalization, concurrency, duplicate prevention, and handler count verification. Imported new packages for testing support. |
| docs/api/app.md | Documented new route removal methods with API signatures, usage examples, and instructions to call RebuildTree() after removals. |
| docs/whats_new.md | Added "RemoveRoute" section to migration guide describing new removal methods with references to documentation. |
| app_test.go | Modified middleware handler in Test_App_Order to explicitly send HTTP 200 OK status instead of returning nil after writing response. |
Sequence Diagram(s)
sequenceDiagram
participant User
participant App
participant Router
User->>App: RemoveRoute(path, methods...)
App->>Router: deleteRoute(methods, matchFunc)
Router->>Router: Lock and iterate routes
Router->>Router: Remove matching route(s)
Router-->>App: Mark routes refreshed
User->>App: RebuildTree()
App->>Router: Rebuild routing tree
sequenceDiagram
participant User
participant App
User->>App: AddRoute(path, handler)
App->>App: Check for duplicate route
alt Duplicate exists
App->>App: Merge or replace handler
else No duplicate
App->>App: Add new route
end
Assessment against linked issues
| Objective | Addressed | Explanation |
|---|---|---|
Add RemoveRoute method to allow unregistering specific routes by path and HTTP methods (#3098) |
β | |
| Ensure removal process is straightforward and does not interfere with other routes (#3098) | β | |
| Prevent duplicate route registration and enable handler replacement (#3098) | β |
Suggested labels
π Documentation, π§Ή Updates
Suggested reviewers
- efectn
- ReneWerner87
- sixcolors
Poem
A rabbit hopped through routes anew,
Removing paths, both old and few.
With helpers, tests, and docs in tow,
The router now can ebb and flow.
No duplicate shall sneak or hideβ
Dynamic routes, with pride, reside!
πβ¨
[!NOTE]
β‘οΈ AI Code Reviews for VS Code, Cursor, Windsurf
CodeRabbit now has a plugin for VS Code, Cursor and Windsurf. This brings AI code reviews directly in the code editor. Each commit is reviewed immediately, finding bugs before the PR is raised. Seamless context handoff to your AI code agent ensures that you can easily incorporate review feedback. Learn more here.
[!NOTE]
β‘οΈ Faster reviews with caching
CodeRabbit now supports caching for code and dependencies, helping speed up reviews. This means quicker feedback, reduced wait times, and a smoother review experience overall. Cached data is encrypted and stored securely. This feature will be automatically enabled for all accounts on May 30th. To opt out, configure
Review - Disable Cacheat either the organization or repository level. If you prefer to disable all data retention across your organization, simply turn off theData Retentionsetting under your Organization Settings. Enjoy the performance boostβyour workflow just got faster.
π Recent review details
Configuration used: CodeRabbit UI Review profile: CHILL Plan: Pro
π₯ Commits
Reviewing files that changed from the base of the PR and between 7244633f6695dd1e15913a18262db9abbf5e9676 and b23eb6eae0202aac5a7a9ad82899868775a17b01.
π Files selected for processing (1)
-
docs/api/app.md(2 hunks)
π§ Files skipped from review as they are similar to previous changes (1)
- docs/api/app.md
πͺ§ Tips
Chat
There are 3 ways to chat with CodeRabbit:
- Review comments: Directly reply to a review comment made by CodeRabbit. Example:
-
I pushed a fix in commit <commit_id>, please review it. -
Explain this complex logic. -
Open a follow-up GitHub issue for this discussion.
-
- Files and specific lines of code (under the "Files changed" tab): Tag
@coderabbitaiin a new review comment at the desired location with your query. Examples:-
@coderabbitai explain this code block. -
@coderabbitai modularize this function.
-
- PR comments: Tag
@coderabbitaiin a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:-
@coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase. -
@coderabbitai read src/utils.ts and explain its main purpose. -
@coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format. -
@coderabbitai help me debug CodeRabbit configuration file.
-
Support
Need help? Create a ticket on our support page for assistance with any issues or questions.
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.
CodeRabbit Commands (Invoked using PR comments)
-
@coderabbitai pauseto pause the reviews on a PR. -
@coderabbitai resumeto resume the paused reviews. -
@coderabbitai reviewto trigger an incremental review. This is useful when automatic reviews are disabled for the repository. -
@coderabbitai full reviewto do a full review from scratch and review all the files again. -
@coderabbitai summaryto regenerate the summary of the PR. -
@coderabbitai generate docstringsto generate docstrings for this PR. -
@coderabbitai generate sequence diagramto generate a sequence diagram of the changes in this PR. -
@coderabbitai resolveresolve all the CodeRabbit review comments. -
@coderabbitai configurationto show the current CodeRabbit configuration for the repository. -
@coderabbitai helpto get help.
Other keywords and placeholders
- Add
@coderabbitai ignoreanywhere in the PR description to prevent this PR from being reviewed. - Add
@coderabbitai summaryto generate the high-level summary at a specific location in the PR description. - Add
@coderabbitaianywhere in the PR title to generate the title automatically.
CodeRabbit Configuration File (.coderabbit.yaml)
- You can programmatically configure CodeRabbit by adding a
.coderabbit.yamlfile to the root of your repository. - Please see the configuration documentation for more information.
- If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation:
# yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json
Documentation and Community
- Visit our Documentation for detailed information on how to use CodeRabbit.
- Join our Discord Community to get help, request features, and share feedback.
- Follow us on X/Twitter for updates and announcements.
@ckoch786 The method has to be added to the docs under docs/. Also mention this new method in the whats_new.md file. You can add it after the RebuildTree section introduced in #3074
@ckoch786 In the root of the repo we have a Makefile to ease local development. See here: https://github.com/gofiber/fiber#-development
You can run the tests locally and also the lint tests.
@gaby I cleaned up my branch and just pushed the docs changes.
@ckoch786 Test and lint workflows are failing. You can test them locally using the Makefile.
@gaby @coderabbitai I am using the the Makefile lint and test. The lint is now passing locally however the tests are really hit or miss for me locally not sure what is going on. Even when running on a fresh checkout of gofiber/fiber on main three of the tests fail for me.
Also some of the tests that fail when running with make test pass when I run them with go test -run Test_ so not really sure how to figure out why they are failing. Any thoughts?
Also I did get to the point where all the test passed after running a few with go test -run and then rerunning make test but now I am back to 651 of them failing, not sure what I am doing wrong.
[!TIP] For best results, initiate chat on the files or code changes.
@ckoch786, sorry to hear you're experiencing issues running the tests locally. Here are some steps that might help:
-
Verify Go Version: Ensure you're using a compatible Go version. Fiber requires Go 1.18 or higher. Check your version with:
go version -
Clean Module Cache and Vendor Directory: Stale dependencies might cause tests to fail. Run:
go clean -modcache rm -rf vendor/ go mod tidy go mod vendor -
Run Tests Verbosely: Running tests with verbose output can help identify issues:
make testverboseOr directly:
go test -v ./... -
Check for Environment Variables: Ensure there are no environment variables affecting the tests. Some tests might rely on specific settings.
-
Port Conflicts: Ensure no other processes are using the ports required by the tests. Conflicts can cause tests to fail unexpectedly.
-
Update Dependencies: Make sure all dependencies are up to date:
go get -u ./... -
Review Failing Tests: Identify which tests are failing:
-
Run individual tests to isolate issues:
go test -v -run Test_SpecificFunction ./... -
Check the error messages for clues.
-
If the problems persist, please share the specific tests failing and any error messages you receive. This information can help diagnose the issue more effectively.
@gaby I am looking at the failing tests from the actions that ran against my branch. I think I found the the changes that I made that is probably causing them to fail. Though it is weird that they do not always fail for me locally, not sure what that is about yet.
@gaby I am looking at the failing tests from the actions that ran against my branch. I think I found the the changes that I made that is probably causing them to fail. Though it is weird that they do not always fail for me locally, not sure what that is about yet.
Are you running just the new tests or the full suite?
Use this make test. We use -shuffle to run them in random orders, it helps catch weird edge cases.
@ckoch786 Any progress on this?
@ReneWerner87 @efectn I vote we close this. Spent 1hr and can't find the issue, all the tests are failing. Author is not responding
@ckoch786 Any progress on this?
@gaby No, sorry. Having to set up a dev environment where I could get the test to run properly set me back. That, and after seeing the number of tests that were failing, I was thinking that I just need to rip out the duplicate route detection logic because getting all the tests to pass is unfortunately going to take more time than I have available at the moment. If that is fine, then I can commit some time over the next few days to get the tests that are for the new RemoveRoute methods, since those do not touch any existing code. I will also update the docs. Also, I see that you have made some changes to my PR. Thank you for not letting it get super stale!
@ckoch786 Any progress on this?
@gaby No, sorry. Having to set up a dev environment where I could get the test to run properly set me back. That, and after seeing the number of tests that were failing, I was thinking that I just need to rip out the duplicate route detection logic because getting all the tests to pass is unfortunately going to take more time than I have available at the moment. If that is fine, then I can commit some time over the next few days to get the tests that are for the new RemoveRoute methods, since those do not touch any existing code. I will also update the docs. Also, I see that you have made some changes to my PR. Thank you for not letting it get super stale!
I believe that @efectn has taken over the changes to this after some internal discussions. So no worries πͺ
@ReneWerner87 @efectn I vote we close this. Spent 1hr and can't find the issue, all the tests are failing. Author is not responding
@gaby I appreciate you trying to find the issue. Now I'm even more wary of messing with the duplicate route detection, as I don't know enough about Fiber at the moment to safely make this change. I was hoping that eliminating the ability to duplicate routes would not conflict with anything. It's a good thing that the tests caught it because my manual testing, did not turn up any issues.
Codecov Report
All modified and coverable lines are covered by tests :white_check_mark:
Project coverage is 84.36%. Comparing base (
59b9b10) to head (b23eb6e). Report is 9 commits behind head on main.
Additional details and impacted files
@@ Coverage Diff @@
## main #3230 +/- ##
==========================================
+ Coverage 84.17% 84.36% +0.19%
==========================================
Files 119 120 +1
Lines 11942 12140 +198
==========================================
+ Hits 10052 10242 +190
- Misses 1462 1468 +6
- Partials 428 430 +2
| Flag | Coverage Ξ | |
|---|---|---|
| unittests | 84.36% <100.00%> (+0.19%) |
:arrow_up: |
Flags with carried forward coverage won't be shown. Click here to find out more.
:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.
:rocket: New features to boost your workflow:
- :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
@coderabbitai full review
β Actions performed
Full review triggered.
Congrats on merging your first pull request! π We here at Fiber are proud of you! If you need help or want to chat with us, join us on Discord https://gofiber.io/discord