Skip to content

Support handler-specific middleware for gofiber#2302

Merged
mromaszewicz merged 3 commits into
oapi-codegen:mainfrom
courtyard-nft:handler-middleware
Mar 27, 2026
Merged

Support handler-specific middleware for gofiber#2302
mromaszewicz merged 3 commits into
oapi-codegen:mainfrom
courtyard-nft:handler-middleware

Conversation

@jpetrich

Copy link
Copy Markdown
Contributor

This PR adds support for handler-specific middleware for gofiber, as mentioned in #518. This is useful when different endpoints require different middleware to be added, e.g. for custom auth.

@jpetrich jpetrich requested a review from a team as a code owner March 24, 2026 02:19
@jpetrich jpetrich marked this pull request as draft March 24, 2026 02:19
@jpetrich jpetrich marked this pull request as ready for review March 24, 2026 02:22
@mromaszewicz

Copy link
Copy Markdown
Member

@greptileai

@greptile-apps

greptile-apps Bot commented Mar 27, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR adds support for handler-specific middleware in the gofiber code generation path, mirroring a capability that already exists for chi/echo. It introduces a new HandlerMiddlewareFunc type (func(c *fiber.Ctx, next fiber.Handler) error) and a HandlerMiddlewares []HandlerMiddlewareFunc field on FiberServerOptions, allowing users to inject middleware that runs after path/query parameters are parsed and security scopes are set in context — but before the handler itself executes.

Key changes:

  • fiber-middleware.tmpl: Each generated handler now wraps its call in a middleware chain (iterated in reverse so index 0 runs first). Also fixes a pre-existing issue where sanitizeGoIdentity was missing from the security scope key construction, which could produce invalid Go identifiers for non-standard scheme names.
  • fiber-handler.tmpl: Threads options.HandlerMiddlewares through to the ServerInterfaceWrapper.
  • internal/test/issues/issue518/: A new end-to-end test covering both basic registration and the auth middleware use-case (differentiating secured vs. unsecured endpoints via security scopes set in context).

Confidence Score: 5/5

This PR is safe to merge — it is a purely additive change and is well-covered by tests.

The change is additive: existing callers that don't set HandlerMiddlewares get the same generated code path (an empty slice causes the loop to no-op). The middleware chaining closure correctly captures loop variables (m and next) to avoid the classic Go closure-in-loop issue. The execution order (first element runs outermost) is intuitive and matches chi's ApplyChiMiddlewareFirstToLast behavior. Tests cover registration, middleware interop, and the secured/unsecured endpoint distinction.

No files require special attention.

Important Files Changed

Filename Overview
pkg/codegen/templates/fiber/fiber-middleware.tmpl Adds HandlerMiddlewareFunc type and wraps each handler in the middleware chain; also fixes security scope key generation with sanitizeGoIdentity.
pkg/codegen/templates/fiber/fiber-handler.tmpl Adds HandlerMiddlewares field to FiberServerOptions and propagates it to ServerInterfaceWrapper.
internal/test/issues/issue518/main_test.go New integration test covering basic registration and auth middleware differentiation based on security scopes; tests both secured and unsecured endpoints.
internal/test/issues/issue518/spec.yaml New OpenAPI spec defining two endpoints: /auth-check (with bearerAuth security scheme) and /test (no auth), used for issue518 tests.
internal/test/issues/issue518/config.yaml Standard oapi-codegen config enabling fiber-server and models generation for the issue518 test package.
internal/test/issues/issue518/doc.go Standard go:generate declaration for the issue518 test package.

Reviews (1): Last reviewed commit: "chore(fiber): regenerate existing fiber ..." | Re-trigger Greptile

@mromaszewicz

Copy link
Copy Markdown
Member

Thank you!

@mromaszewicz mromaszewicz merged commit a3a04d4 into oapi-codegen:main Mar 27, 2026
18 checks passed
@mromaszewicz mromaszewicz added the enhancement New feature or request label Apr 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants