Repository navigation
Description
Summary
For std-http-server, MiddlewareFunc is currently generated as a named function type:
type MiddlewareFunc func(http.Handler) http.HandlerThis makes the generated middleware type package-specific. In projects that split one OpenAPI spec into multiple generated packages, a shared middleware slice such as:
[]func(http.Handler) http.Handlercannot be passed directly into each generated package's StdHTTPServerOptions.Middlewares.
Each generated package has its own distinct type, for example:
[]core.MiddlewareFunc
[]admin.MiddlewareFunc
[]public.MiddlewareFuncThat forces callers to write conversion helpers per generated package, even though the underlying middleware shape is the standard net/http middleware signature.
Proposal
Either emit MiddlewareFunc as a type alias:
type MiddlewareFunc = func(http.Handler) http.Handleror add a generator option that allows users to choose alias generation for middleware types.
Motivation
This would make generated std-http-server middleware easier to compose in applications that:
- generate multiple packages from one or more OpenAPI specs
- share the same
[]func(http.Handler) http.Handlermiddleware chain across those generated servers - use dependency injection or shared route registration interfaces
- want to avoid repetitive conversion helpers like:
func toMiddlewares(mws []func(http.Handler) http.Handler) []gen.MiddlewareFunc {
out := make([]gen.MiddlewareFunc, len(mws))
for i, mw := range mws {
out[i] = mw
}
return out
}With an alias, callers can pass the shared middleware slice directly while preserving the existing generated API shape.
Benefits
This reduces boilerplate and makes the generated std-http-server middleware API easier to use with normal Go net/http middleware.
Today, when an application has multiple generated server packages, each package gets its own distinct MiddlewareFunc type. Even though every type has the same underlying signature, Go does not allow a []func(http.Handler) http.Handler slice to be used as a []gen.MiddlewareFunc slice. It also does not allow one generated package's middleware slice to be reused as another generated package's middleware slice.
Using an alias would let applications define middleware once:
middlewares := []func(http.Handler) http.Handler{
requestIDMiddleware,
authMiddleware,
loggingMiddleware,
}and pass that same slice to every generated std-http-server package without conversion.
That matters most for larger services that split APIs into multiple generated packages but still need one consistent middleware chain for validation, auth, logging, tracing, and metrics. The alias keeps the generated API ergonomic while still documenting the expected middleware shape through the MiddlewareFunc name.
Compatibility
Changing the type to an alias may be source-compatible for most users, but it is technically observable for users relying on MiddlewareFunc as a distinct named type.
If that compatibility risk is too high, a config option would preserve current behavior by default while allowing projects that need shared middleware composition to opt in.
Activity
jay-babu commentedon May 2, 2026
If this is accepted, I will create a PR with the change. It should only be one line
mromaszewicz commentedon May 2, 2026
Yeah, this is a one liner in all relevant templates.
I'll merge that.
I made that change, which was already breaking in v2.7.0, and I'll push a v2.7.1 with this change. I think it's a better this way.
jay-babu commentedon May 2, 2026
Thank you so much for replying on both comments I had! Perfect, this will help quite a bit
kefniark commentedon May 5, 2026
Yeah that would we wonderful, I have a project with like 10+ services and multiple reusable middlewares and that httpnet change caused me some typing headaches with helpers to add extra wrap/cast.
Some type alias could definitely help there, thanks for the work 👍