fix: clean up and test parameter binding#2307
fix: clean up and test parameter binding#2307mromaszewicz wants to merge 1 commit intooapi-codegen:mainfrom
Conversation
078cc36 to
5f4f888
Compare
Greptile SummaryThis PR performs a thorough audit and correctness fix of parameter binding across all 8 server code generation templates, fixing a range of genuine bugs in how path, query, header, and cookie parameters are extracted from HTTP requests. It also adds a comprehensive multi-router roundtrip test suite that validates all OpenAPI parameter styles and types end-to-end.\n\nKey fixes applied:\n- Gin, Fiber, and Iris path param handlers were incorrectly calling query-param getters ( Confidence Score: 5/5Safe to merge — all template fixes are correct and backed by new roundtrip tests covering every router and parameter style. Every fix addresses a genuine, reproducible bug (wrong getter method, missing ParamLocation, incorrect required-param pre-check). The changes are surgical and match the stated intent. The new multi-router roundtrip test suite directly validates the fixed paths, and the two skipped tests have clear issue-tracker references. The only remaining items are cosmetic P2s (wrong error message wording, err shadowing in IsJson cookie blocks) that are pre-existing and don't affect correctness. No files require special attention; all core template changes are correct. Verify that the CI matrix runs at least one Go >= 1.25 job so the echov5 tests are not silently skipped.
|
| Filename | Overview |
|---|---|
| pkg/codegen/templates/fiber/fiber-middleware.tmpl | Fixed path params to use c.Params() + url.PathUnescape() instead of c.Query(), added block-scoped cookie variable to prevent loop redeclaration, added ParamLocation to path and cookie styled bindings, fixed required deepObject query param pre-check. |
| pkg/codegen/templates/gin/gin-wrappers.tmpl | Fixed path params to use c.Param() instead of c.Query(), added ParamLocation: runtime.ParamLocationPath and runtime.ParamLocationCookie, fixed required deepObject query param pre-check. |
| pkg/codegen/templates/iris/iris-middleware.tmpl | Fixed path params from ctx.URLParam() to ctx.Params().Get(), changed cookie handler from nonexistent ctx.Cookie() to ctx.GetCookie(), changed query params from nonexistent ctx.QueryParam() to ctx.URLParam() — all critical API correctness fixes. |
| pkg/codegen/templates/chi/chi-middleware.tmpl | Added ParamLocation: runtime.ParamLocationCookie to cookie styled binding, added _ = err to suppress unused-variable compilation errors when only passthrough path params exist, fixed required deepObject query param pre-check. |
| pkg/codegen/templates/gorilla/gorilla-middleware.tmpl | Added ParamLocation for both path and cookie styled bindings, added _ = err, fixed required deepObject query param pre-check. |
| pkg/codegen/templates/stdhttp/std-http-middleware.tmpl | Added ParamLocation: runtime.ParamLocationCookie to cookie styled binding, added _ = err, fixed required deepObject query param pre-check. The corresponding roundtrip test is intentionally skipped pending #2306. |
| pkg/codegen/templates/echo/v5/echo-wrappers.tmpl | Added Type/Format fields to path, header, and cookie styled bindings, and upgraded query parameter binding to BindQueryParameterWithOptions for consistency with other templates. |
| internal/test/parameters/param_roundtrip_test.go | New comprehensive multi-router roundtrip test file covering path/query/header/cookie parameters for Echo, Chi, Gin, Gorilla, Iris, Fiber; stdhttp skipped with documented issue reference. |
| internal/test/parameters/echov5/echov5_param_test.go | New Echo v5 roundtrip test in a separate Go 1.25 module; the version-guarded Makefile silently skips tests on Go < 1.25, so CI coverage depends on the toolchain version used. |
| internal/test/parameters/echov5/go.mod | New separate module for echov5 tests with a replace directive pointing to the workspace root; requires Go 1.25 to build. |
Reviews (1): Last reviewed commit: "fix: clean up and test parameter binding" | Re-trigger Greptile
5f4f888 to
8788701
Compare
Audit and fix parameter binding in all 8 server code generation templates (Echo v4, Echo v5, Chi, Gin, Gorilla, Iris, Fiber, stdhttp) to ensure consistent, correct handling of path, query, header, and cookie parameters across all OpenAPI styles and types. Template fixes: - Add missing ParamLocation to path params (Gin, Fiber, Gorilla) and cookie params (Chi, Gin, Fiber, Gorilla, stdhttp) so the runtime applies correct URL escaping per location - Add missing Type/Format fields to Echo v5 path, header, and cookie params, and upgrade its query binding to BindQueryParameterWithOptions - Fix required deepObject query params rejected by a spurious pre-check in Chi, Gin, Gorilla, Fiber, and stdhttp templates - Fix Gin, Iris, and Fiber using query param getters instead of path param getters for content-based (passthrough/JSON) path parameters - Fix Iris cookie params calling nonexistent ctx.Cookie() (now ctx.GetCookie()) and query params calling ctx.QueryParam() (now ctx.URLParam()) - Fix Fiber cookie params redeclaring var in a loop (now block-scoped) - Fix Fiber not URL-decoding path param values for passthrough/JSON - Add _ = err to suppress unused variable when only passthrough path params exist (Chi, Gin, Gorilla, Iris, Fiber, stdhttp) Multi-router parameter roundtrip test: - New test infrastructure under internal/test/parameters/ with per-router subdirectories (echo, chi, gin, gorilla, iris, fiber, stdhttp) plus a shared client package - Each router's server echoes received parameters back as JSON; the shared testImpl sends requests via the generated client and verifies the JSON response matches the original values - Generated files isolated in gen/ subdirectories so go generate works even when generated code has compilation errors - Echo v5 in its own module (Go 1.25+) with version-guarded Makefile - Covers all OpenAPI parameter styles (simple, label, matrix, form, deepObject) x types (primitive, array, object) x explode flags across path, query, header, and cookie locations Skipped tests pending external changes: - spaceDelimited and pipeDelimited query styles: runtime does not yet support binding these styles (oapi-codegen/runtime#116) - stdhttp roundtrip: stdlib ServeMux cannot register path wildcards starting with a digit like {1param} (oapi-codegen#2306) Closes: oapi-codegen#777, oapi-codegen#1752 Obsoletes: oapi-codegen#1751, oapi-codegen#2062 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
8788701 to
a369da5
Compare
Audit and fix parameter binding in all 8 server code generation templates (Echo v4, Echo v5, Chi, Gin, Gorilla, Iris, Fiber, stdhttp) to ensure consistent, correct handling of path, query, header, and cookie parameters across all OpenAPI styles and types.
Template fixes:
Multi-router parameter roundtrip test:
Skipped tests pending external changes:
Closes: #777, #1752
Obsoletes: #1751, #2062