Repository navigation
Emit strict handler and middleware types as aliases - #2660
Conversation
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]>
[Medium impact] The PR appears safe to merge; the earlier request for a middleware-sharing test remains a non-blocking improvement. Findings
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.SummaryChanges
Reviews (3) · Last reviewed commit: "Emit strict handler and middleware types..." · Reviewed by Greptile |
| type StrictHandlerFunc = func(ctx context.Context, w http.ResponseWriter, r *http.Request, request any) (any, error) | ||
| type StrictMiddlewareFunc = func(f StrictHandlerFunc, operationID string) StrictHandlerFunc |
There was a problem hiding this 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.
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!
6654147 to
cea7e40
Compare
Closes: #2364
Generated strict servers declared
StrictHandlerFuncandStrictMiddlewareFuncas 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.