-
-
Notifications
You must be signed in to change notification settings - Fork 234
Closed
Description
I'm using humago.
I have a chain of middleware, and one of the later ones needs to use humago.Unwrap() to access the underlying http.Request to log some attributes that are not available in huma.Context (e.g. useragent).
One of the earlier middleware is injecting a key into the request context (context.Context), which naturally creates a new context. The new context is then reattached to the huma context using huma.WithContext().
However, after that point, humago.Unwrap(ctx) panics with the error: not a humago context.
If I move the panicking middleware up the chain so that it runs before the middleware that modifies the context, it works fine.
Here is a minimal example to reproduce the problem (TestMiddleware is the one that panics):
package main
import (
"context"
"net/http"
"github.com/danielgtaylor/huma/v2"
"github.com/danielgtaylor/huma/v2/adapters/humago"
)
func main() {
router := http.NewServeMux()
api := humago.New(router, huma.DefaultConfig("Campaigns", "1"))
api.UseMiddleware(UpdateCtx, TestMiddleware)
huma.Get(api, "/", handler)
http.ListenAndServe(":8080", router)
}
type key struct{}
func UpdateCtx(ctx huma.Context, next func(huma.Context)) {
// Inject key/value into context.
ridCtx := context.WithValue(ctx.Context(), key{}, "value")
ctx = huma.WithContext(ctx, ridCtx)
next(ctx)
}
func TestMiddleware(ctx huma.Context, next func(huma.Context)) {
req, _ := humago.Unwrap(ctx)
println("Useragent:", req.UserAgent())
next(ctx)
}
type Output struct {
Body struct {
Message string
}
}
func handler(ctx context.Context, i *struct{}) (*Output, error) {
output := Output{}
output.Body.Message = "Hello"
return &output, nil
}Metadata
Metadata
Assignees
Labels
No labels