gin icon indicating copy to clipboard operation
gin copied to clipboard

Use the Gin context when doing WrapF or WrapH

Open bvisness opened this issue 3 years ago • 1 comments

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.

bvisness avatar Oct 18 '22 23:10 bvisness

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%) :arrow_down:
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.

codecov[bot] avatar Oct 19 '22 16:10 codecov[bot]