Skip to content

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

Open
imsugeno wants to merge 2 commits intooapi-codegen:mainfrom
imsugeno:feat/issue-659-http-method-constants
Open

feat: use http.MethodXXX constants instead of string literals in generated code#2294
imsugeno wants to merge 2 commits intooapi-codegen:mainfrom
imsugeno:feat/issue-659-http-method-constants

Conversation

@imsugeno
Copy link

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.
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 http.MethodXXX constants instead of string literals

1 participant