Title: std-http-server: allow MiddlewareFunc to be emitted as a type alias · Issue #2363 · oapi-codegen/oapi-codegen · GitHub
Open Graph Title: std-http-server: allow MiddlewareFunc to be emitted as a type alias · Issue #2363 · oapi-codegen/oapi-codegen
X Title: std-http-server: allow MiddlewareFunc to be emitted as a type alias · Issue #2363 · oapi-codegen/oapi-codegen
Description: 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 OpenAP...
Open Graph Description: 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-spe...
X Description: 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-spe...
Opengraph URL: https://github.com/oapi-codegen/oapi-codegen/issues/2363
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"std-http-server: allow MiddlewareFunc to be emitted as a type alias","articleBody":"## Summary\n\nFor `std-http-server`, `MiddlewareFunc` is currently generated as a named function type:\n\n```go\ntype MiddlewareFunc func(http.Handler) http.Handler\n```\n\nThis makes the generated middleware type package-specific. In projects that split one OpenAPI spec into multiple generated packages, a shared middleware slice such as:\n\n```go\n[]func(http.Handler) http.Handler\n```\n\ncannot be passed directly into each generated package's `StdHTTPServerOptions.Middlewares`.\n\nEach generated package has its own distinct type, for example:\n\n```go\n[]core.MiddlewareFunc\n[]admin.MiddlewareFunc\n[]public.MiddlewareFunc\n```\n\nThat forces callers to write conversion helpers per generated package, even though the underlying middleware shape is the standard `net/http` middleware signature.\n\n## Proposal\n\nEither emit `MiddlewareFunc` as a type alias:\n\n```go\ntype MiddlewareFunc = func(http.Handler) http.Handler\n```\n\nor add a generator option that allows users to choose alias generation for middleware types.\n\n## Motivation\n\nThis would make generated `std-http-server` middleware easier to compose in applications that:\n\n- generate multiple packages from one or more OpenAPI specs\n- share the same `[]func(http.Handler) http.Handler` middleware chain across those generated servers\n- use dependency injection or shared route registration interfaces\n- want to avoid repetitive conversion helpers like:\n\n```go\nfunc toMiddlewares(mws []func(http.Handler) http.Handler) []gen.MiddlewareFunc {\n out := make([]gen.MiddlewareFunc, len(mws))\n for i, mw := range mws {\n out[i] = mw\n }\n return out\n}\n```\n\nWith an alias, callers can pass the shared middleware slice directly while preserving the existing generated API shape.\n\n## Benefits\n\nThis reduces boilerplate and makes the generated `std-http-server` middleware API easier to use with normal Go `net/http` middleware.\n\nToday, 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.\n\nUsing an alias would let applications define middleware once:\n\n```go\nmiddlewares := []func(http.Handler) http.Handler{\n requestIDMiddleware,\n authMiddleware,\n loggingMiddleware,\n}\n```\n\nand pass that same slice to every generated `std-http-server` package without conversion.\n\nThat 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.\n\n## Compatibility\n\nChanging 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.\n\nIf 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.","author":{"url":"https://github.com/jay-babu","@type":"Person","name":"jay-babu"},"datePublished":"2026-05-02T20:29:57Z","url":"https://github.com/oapi-codegen/oapi-codegen/issues/2363"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react-reconciler,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@d5750093facf |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:aba17ae3-85c7-ba78-a4a5-5a470b3811ee |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | E84A:151D9E:1B0DF:2380C:6AF9BABD |
| html-safe-nonce | ab4db2aba4a05844d21530c310b2ab936ca29766a1ff79e42330f612af780bf9 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFODRBOjE1MUQ5RToxQjBERjoyMzgwQzo2QUY5QkFCRCIsInZpc2l0b3JfaWQiOiI3NTE1NTc1MjM1MTAxOTY0OTkwIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | cae8c7bdbd28e4ec3662d97ff0505fc3bffaabd0c388d1f1db98054c5ef32778 |
| hovercard-subject-tag | issue:4369946453 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/oapi-codegen/oapi-codegen/2363/issue_layout |
| twitter:image | https://opengraph.githubassets.com/63070e86de6688a531b13972dc8be9c7714a9e17d6db81bfbc9705e268aae0be/oapi-codegen/oapi-codegen/issues/2363 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/63070e86de6688a531b13972dc8be9c7714a9e17d6db81bfbc9705e268aae0be/oapi-codegen/oapi-codegen/issues/2363 |
| og:image:alt | 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-spe... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | jay-babu |
| hostname | github.com |
| expected-hostname | github.com |
| None | 612d3e6ffd693e7ea8d9fcf452370995872baef31d825b1fedef704cd3d24ac7 |
| turbo-cache-control | no-preview |
| go-import | github.com/oapi-codegen/oapi-codegen git https://github.com/oapi-codegen/oapi-codegen.git |
| octolytics-dimension-user_id | 142752710 |
| octolytics-dimension-user_login | oapi-codegen |
| octolytics-dimension-repository_id | 173009358 |
| octolytics-dimension-repository_nwo | oapi-codegen/oapi-codegen |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 173009358 |
| octolytics-dimension-repository_network_root_nwo | oapi-codegen/oapi-codegen |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 072e34035c13c31543fba9f025cbbbda4210dae4 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width