Skip to content

std-http-server: allow MiddlewareFunc to be emitted as a type alias #2363

Open

Description

@jay-babu

Summary

For std-http-server, MiddlewareFunc is currently generated as a named function type:

type MiddlewareFunc func(http.Handler) http.Handler

This 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.Handler

cannot 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.MiddlewareFunc

That 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.Handler

or 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.Handler middleware 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

  1. jay-babu commented on May 2, 2026

    @jay-babu
    Author

    If this is accepted, I will create a PR with the change. It should only be one line

  2. mromaszewicz commented on May 2, 2026

    @mromaszewicz
    Member

    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.

  3. jay-babu commented on May 2, 2026

    @jay-babu
    Author

    Thank you so much for replying on both comments I had! Perfect, this will help quite a bit

  4. kefniark commented on May 5, 2026

    @kefniark

    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 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions