Codegen: add fiber v3 support#2431
Conversation
Important change: the GenerateImports now accepts whole configuration, so it can use the information to know which version of package to import. Currently it's only used for fiber but in the future can be used for any other import with major version increment
The upstream PR targeted fiber v3.0.0-beta.4 and an older oapi-codegen template base. Bring it up to date so it compiles and works against fiber v3.3.0 (stable) and current main: Template fixes (fiber v3 API changes since beta.4): - middleware/strict: c.Context() now returns context.Context, not *fasthttp.RequestCtx; use c.RequestCtx().SetUserValue for scopes. - strict: ctx.BodyParser was removed; use ctx.Bind().Body. ctx.UserContext was removed; use ctx.Context(). - regenerate the v3 non-strict templates (handler/interface/middleware) and the strict interface template from their current v2 counterparts, substituting *fiber.Ctx -> fiber.Ctx (v3 passes Ctx by value). This pulls in fixes the v3 copies were missing: GetReqHeaders() returns map[string][]string (header binding was passing []string where a string was wanted, a compile error), inverted cookie-presence check, missing Type/Format binding options, BindQueryParameterWithOptions, IsAlias guards, per-handler middleware, SummaryAsComment(prefix) signature. Codegen wiring: - imports.tmpl/codegen.go: drop the PR's hardcoded FiberV3 import branch; route fiber/v3 through the new GenerateOptions.RouterImports() mechanism on main. Add FiberV3Server to RouterImports() and AnyOperationGenerator(). Examples / deps: - examples/go.mod: require gofiber/fiber/v3 v3.3.0 and the (still unreleased upstream) fiber-middleware/v2, via a replace to the author's v3 fork until oapi-codegen/fiber-middleware ships v2. - regenerate example .gen.go; use api.GetSpec() (GetSwagger is deprecated). Docs/cmd: - README: the Fiber v3 row used the v2 `fiber-server` option; fix to `fiber-v3-server`. Fix anchor typo. - add fiber-v3 to the -generate help text. Verified: root + examples + internal/test build, test, and lint clean; petstore-expanded/fiberv3 round-trip test passes against fiber v3.3.0; strict + security generated servers compile against fiber v3.3.0. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
|
Review the following changes in direct dependencies. Learn more about Socket for GitHub.
|
Greptile SummaryThis PR adds Fiber v3 (
Confidence Score: 5/5Safe to merge — the change is additive (new opt-in backend, no existing API altered) and the generated output compiles cleanly against Fiber v3.3.0. The codegen wiring, templates, and generated files are all consistent with how other backends are implemented. No existing generated API surface is changed, and the new backend is purely opt-in via fiber-v3-server: true. The only notable gap is the absence of internal/test/servers/fiber-v3/ integration tests, which every other backend has, but that does not affect correctness of the merged output. internal/test/servers/ — a fiber-v3 router test directory analogous to internal/test/servers/fiber/ would close the parity gap with other backends.
|
| Filename | Overview |
|---|---|
| pkg/codegen/configuration.go | Adds FiberV3Server to GenerateOptions, RouterImports switch, AnyOperationGenerator, and Validate nServers counter — all correctly wired following existing patterns. |
| configuration-schema.json | Adds the required fiber-v3-server boolean entry; schema stays in sync with the configuration.go struct change. |
| pkg/codegen/codegen.go | Adds FiberV3Server generation call and write-string block following the same pattern as every other backend. |
| pkg/codegen/operations.go | Adds GenerateFiberV3Server function and wires fiber-v3 strict templates into GenerateStrictServer, mirroring the fiber-v2 approach. |
| pkg/codegen/templates/fiber-v3/fiber-middleware.tmpl | Core parameter-binding template with correct v3 API fixes. Per-handler middleware chaining uses safe closure capture. |
| pkg/codegen/templates/strict/strict-fiber-v3.tmpl | Strict handler wrapper; uses ctx.Context() correctly for v3 and Bind().Body() for bodies. All errors mapped to 400, consistent with v2 template. |
| pkg/codegen/templates/strict/strict-fiber-v3-interface.tmpl | Strict interface and response-visitor template; Visit*Response methods use fiber v3 API correctly. |
| examples/petstore-expanded/fiberv3/api/petstore-server.gen.go | Generated file matches expected template output with correct fiber v3 patterns. |
| examples/go.mod | Adds fiber/v3 and the kerak19 fork replace directive (documented known limitation). |
| docs/fiber-v3-server.md | New documentation page correctly documents the fiber-v3-server generate option and usage. |
Reviews (3): Last reviewed commit: "fiber-v3: restore GenerateImports signat..." | Re-trigger Greptile
- middleware: fix cookie IsPassThrough branch emitting `}&cookie` (a
compile error for optional pass-through cookie params). Bug was carried
over verbatim from the v2 template; corrected here. Verified by
generating a server with an optional text/plain cookie param.
- configuration: fix copy-paste comment on FiberV3Server field.
- strict: use `any` instead of `interface{}` in StrictHandlerFunc to match
the v2 strict-fiber template.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
|
Thanks for the review. Addressed in 7adc99f:
Root + examples + |
# Conflicts: # README.md
…lidation Two simplifications to keep the fiber v3 support purely additive: - Restore the exported `GenerateImports` signature (`packageName string, versionOverride *string`) instead of changing it to take a `Configuration`. The fiber v3 import is emitted via `GenerateOptions.RouterImports()`, which `GenerateImports` already reads from the global state, so the signature change bought nothing and would have broken downstream consumers of `pkg/codegen` that call the function directly. - Count `FiberV3Server` in `Configuration.Validate`'s single-server check, which otherwise allowed `fiber-v3-server` to be combined with another server type in one generation run.
Now that github.com/oapi-codegen/fiber-v3-middleware v0.1.0 is published, drop the temporary `replace` directive that pointed the fiber v3 petstore example at a fork's `fiber-middleware/v2` branch, and import the released module instead. Aligns `github.com/gofiber/fiber/v3` to v3.4.0 to match the middleware release.
|
I've fixed up the PR to be consistent with our other routers, and also released a new fiber V3 middleware, so we don't need to use a replace directive to a personal fork of fiber middleware. |
This continues #1937 (by @kerak19), bringing Fiber v3 support up to date with current
mainand stable Fiber v3 (v3.3.0). The original PR targetedv3.0.0-beta.4and an older template base, so the templates no longer matched either currentmainor stable Fiber v3.The original commits are preserved; a final commit adapts them.
Fiber v3 API changes since beta.4 (verified against the v3.3.0 source)
c.Context()now returnscontext.Context(not*fasthttp.RequestCtx), so it has noSetUserValue. The security-scopes path was a compile error →c.RequestCtx().SetUserValue(...).ctx.BodyParserwas removed (replaced byBind()) →ctx.Bind().Body().ctx.UserContext()was removed →ctx.Context().GetReqHeaders()returnsmap[string][]string; the v3 header binding passed a[]stringwhere astringwas required (compile error).Template drift fixed
The v3 templates were copied from a beta.4-era v2 base and had since diverged. The v3 non-strict templates and the strict-interface template are regenerated from their current v2 counterparts (only difference: Fiber v3 passes
Ctxby value,*fiber.Ctx→fiber.Ctx). This also corrects an inverted cookie-presence check, missingType/Formatbinding options, a staleBindQueryParameter, missingIsAliasguards, per-handler middleware support, and theSummaryAsComment(prefix)signature.Codegen wiring
Fiber v3 imports now route through
GenerateOptions.RouterImports()(the mechanism introduced onmain) rather than the original hardcoded import branch. AddedFiberV3ServertoRouterImports()andAnyOperationGenerator().Docs / cmd
fiber-serveroption →fiber-v3-server; fixed an anchor typo.fiber-v3to the-generatehelp text.Verification
internal/testbuild, test, and lint clean.petstore-expanded/fiberv3round-trip test passes against Fiber v3.3.0.go generateproduces zero drift from the committed.gen.gofiles.Known limitations (carried over from #1937)
oapi-codegen/fiber-middlewarestill has no released v3//v2module, so the petstore example uses areplaceto @kerak19's fork. This should switch to an officialgithub.com/oapi-codegen/fiber-middleware/v2once published.Bind().Body()returns a parse error (notio.EOF) on an empty body, so the v2 empty-optional-body tolerance is not ported.