🧹 chore: Add FullPath() helper to context#3837
Conversation
|
Note Other AI code review bot(s) detectedCodeRabbit has detected other AI code review bot(s) in this pull request and will avoid duplicating their findings in the review comments. This may lead to a less comprehensive review. WalkthroughAdded a new public accessor Changes
Sequence Diagram(s)sequenceDiagram
participant Req as HTTP Request
participant Router as App Router
participant Ctx as DefaultCtx
participant Route as Route
Note over Router,Route: incoming request matched to a Route (may include group prefix)
Req ->> Router: HTTP request
Router ->> Route: match route (including group/mount)
Router ->> Ctx: create DefaultCtx (attach Route)
Ctx ->> Route: access Route.Path
Route -->> Ctx: "/v1/test" %% example composed path
Note right of Ctx: handlers/middleware can call FullPath()\nbefore and after Next()
Ctx ->> Ctx: FullPath() returns matched route path
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes
Possibly related PRs
Suggested reviewers
Poem
Pre-merge checks and finishing touches❌ Failed checks (1 inconclusive)
✅ Passed checks (3 passed)
✨ Finishing touches🧪 Generate unit tests (beta)
📜 Recent review detailsConfiguration used: CodeRabbit UI Review profile: CHILL Plan: Pro 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (6)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Summary of ChangesHello @gaby, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request introduces a significant enhancement by providing a Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request introduces a FullPath helper to the context, which is a useful addition for getting the complete matched route path, including group prefixes. The implementation is straightforward and correct, and it's well-covered by tests and documentation. I have one suggestion to refactor the new tests for better maintainability.
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (4)
ctx_interface_gen.go (1)
113-114: Doc tweak for unmatched routes (optional)Consider clarifying behavior when no route matched, e.g., “If no route matched, returns the original request path.” This mirrors the implementation suggestion in ctx.go.
- // FullPath returns the matched route path, including any group prefixes. + // FullPath returns the matched route path, including any group prefixes. + // If no route matched, returns the original request path.ctx.go (2)
320-324: Avoid allocation when no route matched; clarify docRoute() allocates a new Route when c.route is nil. Return directly from fields to avoid that and document the unmatched case.
-// FullPath returns the matched route path, including any group prefixes. -func (c *DefaultCtx) FullPath() string { - return c.Route().Path -} +// FullPath returns the matched route path, including any group prefixes. +// If no route matched, returns the original request path. +func (c *DefaultCtx) FullPath() string { + if c.route != nil { + return c.route.Path + } + return c.pathOriginal +}
320-324: Formatting/build hygienePlease run the repo’s Go targets to satisfy project guidelines: gofumpt formatting, betteralign, and gopls modernize (e.g.,
make format betteralign modernize).ctx_test.go (1)
3656-3690: Good coverage; add param/unmatched cases (optional)Current tests validate static and grouped routes. Recommend adding:
- Param route returns pattern: e.g., GET /users/:id → “/users/:id”.
- Unmatched path returns original path (if adopting the doc/impl suggestion).
Example additions:
func Test_Ctx_FullPath_Params(t *testing.T) { t.Parallel() app := New() app.Get("/users/:id", func(c Ctx) error { require.Equal(t, "/users/:id", c.FullPath()) return c.SendStatus(StatusOK) }) resp, err := app.Test(httptest.NewRequest(MethodGet, "/users/42", nil)) require.NoError(t, err) require.Equal(t, StatusOK, resp.StatusCode) } func Test_Ctx_FullPath_Unmatched(t *testing.T) { t.Parallel() app := New() // no routes resp, err := app.Test(httptest.NewRequest(MethodGet, "/missing", nil)) require.NoError(t, err) require.Equal(t, StatusNotFound, resp.StatusCode) // Acquire to inspect FullPath() behavior on unmatched c := app.AcquireCtx(&fasthttp.RequestCtx{}) defer app.ReleaseCtx(c) c.Request().SetRequestURI("/missing") require.Equal(t, "/missing", c.FullPath()) }
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (4)
ctx.go(1 hunks)ctx_interface_gen.go(1 hunks)ctx_test.go(1 hunks)docs/api/ctx.md(1 hunks)
🧰 Additional context used
📓 Path-based instructions (2)
**/*.go
📄 CodeRabbit inference engine (AGENTS.md)
**/*.go: Apply formatting using gofumpt (Make target: format)
Optimize struct field alignment using betteralign (Make target: betteralign)
Modernize code using gopls modernize (Make target: modernize)
Files:
ctx_interface_gen.goctx.goctx_test.go
docs/**
📄 CodeRabbit inference engine (.github/copilot-instructions.md)
Review and update the contents of the
docsfolder if necessary when modifying code
Files:
docs/api/ctx.md
🧠 Learnings (2)
📚 Learning: 2024-11-10T23:44:13.704Z
Learnt from: gaby
Repo: gofiber/fiber PR: 3193
File: middleware/adaptor/adaptor.go:111-111
Timestamp: 2024-11-10T23:44:13.704Z
Learning: In the `middleware/adaptor/adaptor.go` file of the Fiber framework, when updating context handling, replacing `c.Context()` with `c.RequestCtx()` is appropriate to access the `fasthttp.RequestCtx`.
Applied to files:
ctx.godocs/api/ctx.md
📚 Learning: 2024-11-08T04:10:42.990Z
Learnt from: gaby
Repo: gofiber/fiber PR: 3193
File: middleware/cache/cache_test.go:897-897
Timestamp: 2024-11-08T04:10:42.990Z
Learning: In the Fiber framework, `Context()` is being renamed to `RequestCtx()`, and `UserContext()` to `Context()` to improve clarity and align with Go's context conventions.
Applied to files:
docs/api/ctx.md
🧬 Code graph analysis (2)
ctx.go (1)
router.go (1)
Route(43-64)
ctx_test.go (4)
app.go (1)
New(521-634)ctx_interface_gen.go (1)
Ctx(18-428)constants.go (2)
StatusOK(52-52)MethodGet(5-5)group.go (1)
Group(14-21)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (5)
- GitHub Check: unit (1.25.x, macos-latest)
- GitHub Check: unit (1.25.x, windows-latest)
- GitHub Check: Compare
- GitHub Check: repeated
- GitHub Check: lint
🔇 Additional comments (2)
ctx_interface_gen.go (1)
113-114: Public interface addition: confirm breakage is acceptable and document itAdding FullPath() to Ctx is a source-breaking change for any external types implementing Ctx without embedding DefaultCtx. Please confirm this is intended for v3 and ensure release notes/docs call it out. Also regenerate the interface via the existing go:generate flow so the generated file remains authoritative.
docs/api/ctx.md (1)
423-438: Documentation for FullPath() is well-structured and clear.The new section is properly formatted, includes a clear description explaining that the method returns the full matched route path including group/mount prefixes, and provides a concrete example demonstrating this with a grouped route. The placement between
RouteandMatchedsections is logical, and the documentation follows established patterns in the file.
|
Let me report the above to CodeRabbit |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #3837 +/- ##
=======================================
Coverage 92.26% 92.26%
=======================================
Files 115 115
Lines 9742 9744 +2
=======================================
+ Hits 8988 8990 +2
Misses 480 480
Partials 274 274
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Pull Request Overview
This PR adds a new FullPath() method to the Fiber context that returns the full matched route path including any group prefixes. This provides a convenient way to access the complete route pattern without needing to call Route().Path directly.
- Adds
FullPath()method to the Ctx interface and DefaultCtx implementation - Includes comprehensive test coverage for basic routes, grouped routes, and middleware behavior
- Documents the new method with examples showing usage in routes and middleware
Reviewed Changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| ctx.go | Implements the FullPath() method that delegates to Route().Path |
| ctx_interface_gen.go | Adds FullPath() to the Ctx interface definition |
| ctx_test.go | Adds three test cases covering basic usage, grouped routes, and middleware scenarios |
| docs/api/ctx.md | Documents the new method with signature, description, and usage examples |
Summary
gin, add a FullPath() helper to Ctx that exposes the matched route path.Fixes #3832