oapi-codegen
oapi-codegen copied to clipboard
Strict Middleware Cannot Access Response Status Code
Problem
When using oapi-codegen generated strict middleware (StrictMiddlewareFunc), the response status code is not available during middleware execution.
Expected Behavior
func Telemetry(f v1.StrictHandlerFunc, operationName string) v1.StrictHandlerFunc {
return func(ctx *gin.Context, request interface{}) (interface{}, error) {
result, err := f(ctx, request)
statusCode := ctx.Writer.Status() // Should return actual status (200, 404, etc.)
// ... telemetry collection
return result, err
}
}
Actual Behavior
ctx.Writer.Status() always returns 0 because the response hasn't been written yet when strict middleware executes.
Root Cause
The oapi-codegen generated handlers write the response after all middleware has executed:
// Generated handler pattern
for _, middleware := range sh.middlewares {
handler = middleware(handler, "OperationName") // Middleware runs here
}
response, err := handler(ctx, request)
// Response writing happens AFTER middleware execution
if err != nil {
ctx.Status(http.StatusInternalServerError) // Status set here
}
Request
Could oapi-codegen provide a way for strict middleware to access the final response status code?
I've just encountered the same problem. It would be super convenient to have access to the status code from the strict middlewares