gin.Context vs context.Context when handles are called
In the strict Gin server, the handlers are called like this:
handler := func(ctx *gin.Context, request interface{}) (interface{}, error) {
return sh.ssi.JSONExample(ctx, request.(JSONExampleRequestObject))
}
The signature of the handler is:
func (StrictServerInterface).JSONExample(ctx context.Context, request JSONExampleRequestObject) (JSONExampleResponseObject, error)
So the gin.Context is passed as a regular context.Context. Using it like this makes it harder to set context values throughout middleware functions. I have custom packages that add stuff to the context that does not work with gin.Context. I can set theses values like this:
ctx := context.WithValue(c.Request.Context(), key, value)
c.Request = c.Request.WithContext(ctx)
c.Next()
I can't however retrieve this value in my handler because the interface is context.Context so I can't use c.Request.Context()
Is there a reason that the signature of the handler is not:
func (StrictServerInterface).JSONExample(ctx gin.Context, request JSONExampleRequestObject) (JSONExampleResponseObject, error)
Changing this would be breaking change so would you consider changing the template generator to actually pass the request context instead of the gin context like this:
handler := func(ctx *gin.Context, request interface{}) (interface{}, error) {
return sh.ssi.{{.OperationId}}(ctx.Request.Context(), request.({{$opid | ucFirst}}RequestObject))
}
You can use ctx.Value(middleware.GinContextKey).(*gin.Context) to get the gin context in your middlewares.
Found this issue, because it's breaking OTEL span propagation, not the best of times to find out there's no answer.
edit:
r.ContextWithFallback = true seems to have worked.