Skip to content

Emit strict handler and middleware types as aliases - #2660

Merged
mromaszewicz merged 1 commit into
oapi-codegen:mainfrom
mromaszewicz:fix/issue-2364
Nov 9, 2026
Merged

mromaszewicz merged 1 commit into
oapi-codegen:mainfrom
mromaszewicz:fix/issue-2364

Conversation

@mromaszewicz

Copy link
Copy Markdown
Member

Closes: #2364

Generated strict servers declared StrictHandlerFunc and StrictMiddlewareFunc as distinct named types, so a middleware written once could not be placed in the middleware slice of two different generated packages even though the signatures were identical. They are now type aliases of the underlying function types, which makes them interchangeable across every generated package for the same framework.

Observable only to code that relied on these being distinct named types (e.g. methods declared on them); such code needs to use the underlying function type instead.

@mromaszewicz
mromaszewicz requested a review from a team as a code owner November 9, 2026 17:18
@mromaszewicz mromaszewicz added the notable changes Used for release notes to highlight these more highly label Nov 9, 2026
Closes: oapi-codegen#2364

Generated strict servers declared `StrictHandlerFunc` and
`StrictMiddlewareFunc` as distinct named types, so a middleware written once
could not be placed in the middleware slice of two different generated
packages even though the signatures were identical. They are now type aliases
of the underlying function types, which makes them interchangeable across
every generated package for the same framework.

Observable only to code that relied on these being distinct named types (e.g.
methods declared on them); such code needs to use the underlying function
type instead.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
@greptile-apps

greptile-apps Bot commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge; the earlier request for a middleware-sharing test remains a non-blocking improvement.

Findings

  1. P2 Middleware sharing has no test ▶
Fix with agent prompt
### Issue 1
pkg/codegen/templates/strict/strict-http.tmpl:4-5
Existing strict-server tests pass `nil` middleware, and no `*_test.go` references `StrictHandlerFunc` or `StrictMiddlewareFunc`. A later change could restore the named types and break sharing without a test catching it.

Add a compile-time test in `internal/test/servers/strict/` that assigns a handler, middleware, and middleware slice between two generated packages with matching signatures. It should fail with the old named declarations and pass with these aliases.

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Summary

Changes StrictHandlerFunc and StrictMiddlewareFunc to function type aliases so matching generated packages can share middleware.

  • Strict server handler and middleware names now resolve to aliases, letting callers reuse middleware across generated packages with matching framework signatures.

Reviews (3) · Last reviewed commit: "Emit strict handler and middleware types..." · Reviewed by Greptile

Comment on lines +4 to +5
type StrictHandlerFunc = func(ctx context.Context, w http.ResponseWriter, r *http.Request, request any) (any, error)
type StrictMiddlewareFunc = func(f StrictHandlerFunc, operationID string) StrictHandlerFunc

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Middleware sharing has no test

Existing strict-server tests pass nil middleware, and no *_test.go references StrictHandlerFunc or StrictMiddlewareFunc. A later change could restore the named types and break sharing without a test catching it.

Add a compile-time test in internal/test/servers/strict/ that assigns a handler, middleware, and middleware slice between two generated packages with matching signatures. It should fail with the old named declarations and pass with these aliases.

Prompt To Fix With AI
This is a comment left during a code review.
Path: pkg/codegen/templates/strict/strict-http.tmpl
Line: 4-5

Comment:
**Middleware sharing has no test**

Existing strict-server tests pass `nil` middleware, and no `*_test.go` references `StrictHandlerFunc` or `StrictMiddlewareFunc`. A later change could restore the named types and break sharing without a test catching it.

Add a compile-time test in `internal/test/servers/strict/` that assigns a handler, middleware, and middleware slice between two generated packages with matching signatures. It should fail with the old named declarations and pass with these aliases.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

@mromaszewicz
mromaszewicz merged commit 3abe03a into oapi-codegen:main Nov 9, 2026
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

notable changes Used for release notes to highlight these more highly

Projects

None yet

Development

Successfully merging this pull request may close these issues.

strict std-http server: allow StrictMiddlewareFunc to remain assignable from runtime middleware

1 participant