Skip to content

feat: use http.MethodXXX constants instead of string literals in generated code#2294

Merged
mromaszewicz merged 3 commits into
oapi-codegen:mainfrom
imsugeno:feat/issue-659-http-method-constants
Mar 23, 2026
Merged

feat: use http.MethodXXX constants instead of string literals in generated code#2294
mromaszewicz merged 3 commits into
oapi-codegen:mainfrom
imsugeno:feat/issue-659-http-method-constants

Conversation

@imsugeno

Copy link
Copy Markdown
Contributor

Replace HTTP method string literals (e.g. "GET", "POST") with net/http constants (e.g. http.MethodGet, http.MethodPost) in generated code.

Changes

  • Add httpMethodConstant template helper function that maps HTTP method strings to their http.MethodXXX equivalents
  • Update 3 templates that emit method string literals:
    • client.tmplhttp.NewRequest("GET", ...)http.NewRequest(http.MethodGet, ...)
    • gorilla/gorilla-register.tmpl.Methods("GET").Methods(http.MethodGet)
    • stdhttp/std-http-handler.tmpl"GET "+patternhttp.MethodGet+" "+pattern

Other server templates (Chi, Echo, Gin, Fiber, Iris) use .Method as a router method name (e.g. router.Get(...)) rather than a string literal, so no changes are needed there.

This is a cosmetic change only — http.MethodGet == "GET" — so there is no behavioral difference and no breaking change to consumers.

Fixes #659

Add httpMethodConstant template helper that converts HTTP method strings
(e.g. "GET") to net/http constants (e.g. http.MethodGet) and apply it
to the client, gorilla, and stdhttp templates.

Refs oapi-codegen#659
Run make generate to update all generated files with the new
http.MethodXXX constants in place of string literals.
@mromaszewicz mromaszewicz enabled auto-merge (squash) March 23, 2026 19:28
@mromaszewicz mromaszewicz merged commit fc65e09 into oapi-codegen:main Mar 23, 2026
17 checks passed
@jamietanna jamietanna added the enhancement New feature or request label May 1, 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.

Use http.MethodXXX constants instead of string literals

3 participants