Skip to content

fix: clean up and test parameter binding#2307

Open
mromaszewicz wants to merge 1 commit intooapi-codegen:mainfrom
mromaszewicz:feat/param-cleanup
Open

fix: clean up and test parameter binding#2307
mromaszewicz wants to merge 1 commit intooapi-codegen:mainfrom
mromaszewicz:feat/param-cleanup

Conversation

@mromaszewicz
Copy link
Copy Markdown
Member

@mromaszewicz mromaszewicz commented Mar 27, 2026

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:

Closes: #777, #1752
Obsoletes: #1751, #2062

@mromaszewicz mromaszewicz requested a review from a team as a code owner March 27, 2026 00:31
@mromaszewicz
Copy link
Copy Markdown
Member Author

@greptileai

@mromaszewicz mromaszewicz changed the title fix: parameter binding consistency across all server templates and ro… fix: clean up and test parameter binding Mar 27, 2026
@greptile-apps
Copy link
Copy Markdown

greptile-apps bot commented Mar 27, 2026

Greptile Summary

This 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 (c.Query(), ctx.URLParam()) instead of path-param getters — now corrected to c.Param(), c.Params(), and ctx.Params().Get()\n- Fiber path params with IsPassThrough / IsJson were missing url.PathUnescape(), which could silently corrupt percent-encoded path segments\n- Required deepObject-style query params were rejected by a spurious pre-check in Chi, Gin, Gorilla, Fiber, and stdhttp — fixed by removing .Required from the passthrough/JSON gate\n- ParamLocation field was missing for path params (Gin, Fiber, Gorilla) and cookie params (Chi, Gin, Fiber, Gorilla, stdhttp)\n- Echo v5 was missing Type/Format fields and was not using BindQueryParameterWithOptions for query params\n- Iris was calling the nonexistent ctx.Cookie() and ctx.QueryParam() APIs (now ctx.GetCookie() and ctx.URLParam())\n- Fiber cookie params redeclared cookie in a loop body; now fixed with block scoping\n- _ = err added to prevent "declared but not used" compile errors in handlers that have only passthrough path params

Confidence Score: 5/5

Safe 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.

Important Files Changed

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

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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Use BindStyledParameterWithLocation for correct path param parsing

1 participant