-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Revert revert requestid contextkey #2750
Revert revert requestid contextkey #2750
Conversation
…iber#2369)" (gofiber#2742)" This reverts commit 28be17f.
Should check for `nil` since it is `any`.
Hi, we have decided against other types for the context key. String is sufficient as a type for all conceivable real cases that you might want to use in an application. |
We're obviously not on the same page. Let's try to work this out, there are two separate issues going on here: 1 - This PR only aligns the requestid middleware back to fiber The rest of fiber accepts https://pkg.go.dev/github.com/gofiber/fiber/v2#Ctx.Locals 2 - Context keys should be Please read the standard library documentation: https://pkg.go.dev/context#Context. Also check the official blog post on the matter: https://go.dev/blog/context. "Private unexported types" implies that functions that take context keys must type them as |
IMO it's probably best to follow the standard Go guidance of using |
Hello again, I'd like to further explain our decision to explicitly set the data type as While it's true that the underlying context in GoFiber allows any data type ( By specifying the data type as
For instance, consider a scenario where a user mistakenly passes a complex object as the Currently, there have been no reported downsides to this approach in terms of functionality. It doesn't render the middleware non-functional, nor does the alternative of an unspecified type ( I hope this clarifies our design choice. If you have any further questions or suggestions, feel free to share them. |
You are breaking the principle you are quoting by constraining the context keys of middlewares to be
This PR changes this to make it less astonishing:
All three points are moot:
Then go ahead an make a PR to change
Yes there is, per my description of this PR:
Additionally, a user that doesn't change the key, or changed it to a |
Hello, Thank you for your feedback. Let me briefly address our decision:
Based on these considerations, we have decided to maintain the current implementation with |
There is no such distinction.
You have clearly not read https://pkg.go.dev/context#Context nor https://go.dev/blog/context. To summarize: strings will collide, unlike unexported types, so unexported types are safe and strings are not. And the whole point of configuring the context key of the requestid middleware is because it will be read back by the app through
@ReneWerner87 who exactly is "we"? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hello @benjajaja, thank you for raising your concerns and initiating this PR. Your technical insights are valuable, and I appreciate your dedication to improving GoFiber.
Given that @ReneWerner87 is a maintainer, his perspective holds significant weight. It appears there's also a potential consistency issue with other middleware packages config.ContextKey
types, and it might be beneficial to explore a universal approach rather than proposing a PR for a single middleware package immediately following reversion PR #2742 and the #2731 discussion.
I suggest we open an issue or discussion where we can collaborate and work towards a solution that ensures consistency across the entire project. This way, we can address your concerns and maintain a uniform approach to enhance the overall user experience. Your contributions are important in achieving this goal, and I'm open to exploring your technical arguments further.
GoFiber still supports go 1.17.... What approach would you recommend? |
@sixcolors a patch for all middlewares is open here: #2751. |
-> the maintainer team + some of the contributors from the community Can you please provide a (few) short examples of real world cases so that i can understand the more precise need for the change |
Yes of course. Example A, "using unexported types as per official guidelines":This is some app that uses not only fiber but also other libraries, like for example for mqtt, job queues, whatever you have. type contextKey string
const (
UserID = contextKey("user_id")
TaskID = contextKey("task_id")
RequestId = contextKey("request_id")
// ...etc.
) The app uses the provided fiber requestid middleware. app.Use(requestid.New(requestid.Config{
ContextKey: RequestId,
})) If The requestid may later be read back with something like this: id := c.Locals(RequestId) // in a fiber handler / middleware / ...
id := ctx.Value(RequestId) // in a non-fiber "handler", e.g. a job goroutine or similar. Example B, "using
|
okay, thanks for that however, the probability of a collision is low however, the security aspect that pjebs brings up here, this could be considered when building wrapper applications or plugins, for example, where the outer applications do not have the possibility of modification we will consider these aspects again and discuss the advantages and disadvantages of the current version compared to the new version thanks for the insights and your effort |
Some middleware use multiple context keys. Maybe middleware.ContextKeys.Key would be a good standard eg |
|
Recomend we close this PR and work on #2751 as it includes this change. |
We merged #2751 |
Description
Revert the revert of #2742, fix condition check, add a unit test that asserts that the condition is correct, and add a comment about why the default value is
string
instead of the recommended private-type-style.See also the initial discussion and my latest comment in #2731 - there are several incorrect assumptions about how the context works or should work, which lead to the commit reversal.
Fixes #2356
Specific case example
context.Context
.requestid
middleware because Revert ":bug: requestid.Config.ContextKey is interface{} (#2369)" #2742 has constrained the key to be astring
.Type of change
Please delete options that are not relevant.
Checklist: