danielgtaylor / danielgtaylor/huma
proposal: change Middlewares to split per-request ctx
- Dominant language
- Go
- Stars
- 4.4k
- Forks
- 285
- Avg merge
- 40m
- Merged PRs (30d)
- 1
Description
> [!WARNING]
> This is (in its current form) a breaking proposal; so it should be targeted at v3.
> Though there may be a way to introduce this _without_ breakage.
The current middleware definition `func(ctx huma.Context, next func(huma.Context))` receives the per-request `ctx` as well as the basically constant `next` parameter for each request. This makes it cumbersome to use middlewares that require "singleton" initialization (e.g. something using groupcache), because there's no way to access `next` on a per-registration basis.
I'd propose changing the definition of `Middleware` to:
```go
type Middleware func(next func(Context)) func(Context)
```
This would allow stuff like:
```go
huma.Register(api, huma.Operation{
// ...
Middlewares: huma.Middlewares{
func(next func(huma.Context)) func(huma.Context) {
// initialize per-operation:
m := func(ctx huma.Context) {
// ...
}
return func(ctx huma.Context) {
return m(ctx)
}
},
},
}, someHandler)
```
WDYT?
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.