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

Use the Gin context when doing WrapF or WrapH #3368

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

bvisness
Copy link

@bvisness bvisness commented Oct 18, 2022

Because the signature of an http.Handler does not include a *gin.Context, an http.Handler can only access req.Context(). However, this always refers to the request's original context instead of the Gin context. This means that any values set with Gin's c.Set are not accessible in those handlers. This means that Gin middleware that sets context values actually can't pass values to third-party handlers!

This PR updates WrapF and WrapH to make req.Context() return the Gin context itself. This makes calls like c.Value work as expected.

c.Request is unaffected by this change, meaning that the original request (with its original context) can still be accessed via req.Context().(*gin.Context).Request if absolutely necessary. This also means that ContextWithFallback still works. As far as I can tell, the only instance of breakage here would be:

  • The user does not use ContextWithFallback, and
  • code in a third-party handler depends on the stdlib context to work correctly (e.g. for cancellation)

And in this case, their problems could likely be fixed simply by opting into ContextWithFallback, rendering the breakage trivial.

@codecov
Copy link

codecov bot commented Oct 19, 2022

Codecov Report

Merging #3368 (fc2d22c) into master (33ab0fc) will not change coverage.
The diff coverage is 100.00%.

@@           Coverage Diff           @@
##           master    #3368   +/-   ##
=======================================
  Coverage   98.27%   98.27%           
=======================================
  Files          42       42           
  Lines        3127     3127           
=======================================
  Hits         3073     3073           
  Misses         40       40           
  Partials       14       14           
Flag Coverage Δ
98.27% <100.00%> (ø)
go-1.17 98.17% <100.00%> (ø)
go-1.18 98.17% <100.00%> (ø)
go-1.19 98.27% <100.00%> (ø)
macos-latest 98.17% <100.00%> (-0.10%) ⬇️
ubuntu-latest 98.27% <100.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
utils.go 97.00% <100.00%> (ø)

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant