Title: feat(client): 160,000 -> 85,000 LOC improvement for query-param heavy openapi specs · Issue #1744 · oapi-codegen/oapi-codegen · GitHub
Open Graph Title: feat(client): 160,000 -> 85,000 LOC improvement for query-param heavy openapi specs · Issue #1744 · oapi-codegen/oapi-codegen
X Title: feat(client): 160,000 -> 85,000 LOC improvement for query-param heavy openapi specs · Issue #1744 · oapi-codegen/oapi-codegen
Description: Unable at the moment to submit a PR, but we've pulled in client.tmpl locally as of commit d232e9e, and have applied the following patch of changes to the client.tmpl template. diff --git a/templates/client.tmpl b/templates/client.tmpl in...
Open Graph Description: Unable at the moment to submit a PR, but we've pulled in client.tmpl locally as of commit d232e9e, and have applied the following patch of changes to the client.tmpl template. diff --git a/template...
X Description: Unable at the moment to submit a PR, but we've pulled in client.tmpl locally as of commit d232e9e, and have applied the following patch of changes to the client.tmpl template. diff --git a/temp...
Opengraph URL: https://github.com/oapi-codegen/oapi-codegen/issues/1744
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"feat(client): 160,000 -> 85,000 LOC improvement for query-param heavy openapi specs","articleBody":"Unable at the moment to submit a PR, but we've pulled in `client.tmpl` locally as of commit d232e9efa9f54ee20ca60df885595bd23dc86759, and have applied the following patch of changes to the `client.tmpl` template.\r\n\r\n```patch\r\ndiff --git a/templates/client.tmpl b/templates/client.tmpl\r\nindex 10ee564..54da21f 100644\r\n--- a/templates/client.tmpl\r\n+++ b/templates/client.tmpl\r\n@@ -123,6 +123,26 @@ func (c *{{ $clientTypeName }}) {{$opid}}{{.Suffix}}(ctx context.Context{{genPar\r\n {{end}}{{/* range .Bodies */}}\r\n {{end}}\r\n\r\n+func addParamToQuery(values url.Values, style string, explode bool, paramName string, value any) error {\r\n+\tqueryFrag, err := runtime.StyleParamWithLocation(style, explode, paramName, runtime.ParamLocationQuery, value)\r\n+\tif err != nil {\r\n+\t\treturn err\r\n+\t}\r\n+\r\n+\tparsed, err := url.ParseQuery(queryFrag)\r\n+\tif err != nil {\r\n+\t\treturn err\r\n+\t}\r\n+\r\n+\tfor k, v := range parsed {\r\n+\t\tfor _, v2 := range v {\r\n+\t\t\tvalues.Add(k, v2)\r\n+\t\t}\r\n+\t}\r\n+\r\n+\treturn nil\r\n+}\r\n+\r\n {{/* Generate request builders */}}\r\n {{range .}}\r\n {{$hasParams := .RequiresParamObject -}}\r\n@@ -196,32 +216,23 @@ func New{{$opid}}Request{{if .HasBody}}WithBody{{end}}(server string{{genParamAr\r\n {{if .QueryParams}}\r\n if params != nil {\r\n queryValues := queryURL.Query()\r\n- {{range $paramIdx, $param := .QueryParams}}\r\n+ {{range $paramIdx, $param := .QueryParams -}}\r\n {{if not .Required}} if params.{{.GoName}} != nil { {{end}}\r\n- {{if .IsPassThrough}}\r\n+ {{- if .IsPassThrough}}\r\n queryValues.Add(\"{{.ParamName}}\", {{if not .Required}}*{{end}}params.{{.GoName}})\r\n- {{end}}\r\n- {{if .IsJson}}\r\n+ {{- end}}\r\n+ {{- if .IsJson}}\r\n if queryParamBuf, err := json.Marshal({{if not .Required}}*{{end}}params.{{.GoName}}); err != nil {\r\n return nil, err\r\n } else {\r\n queryValues.Add(\"{{.ParamName}}\", string(queryParamBuf))\r\n }\r\n-\r\n- {{end}}\r\n- {{if .IsStyled}}\r\n- if queryFrag, err := runtime.StyleParamWithLocation(\"{{.Style}}\", {{.Explode}}, \"{{.ParamName}}\", runtime.ParamLocationQuery, {{if not .Required}}*{{end}}params.{{.GoName}}); err != nil {\r\n+ {{- end}}\r\n+ {{- if .IsStyled}}\r\n+ if err := addParamToQuery(queryValues, \"{{.Style}}\", {{.Explode}}, \"{{.ParamName}}\", {{if not .Required}}*{{end}}params.{{.GoName}}); err != nil {\r\n return nil, err\r\n- } else if parsed, err := url.ParseQuery(queryFrag); err != nil {\r\n- return nil, err\r\n- } else {\r\n- for k, v := range parsed {\r\n- for _, v2 := range v {\r\n- queryValues.Add(k, v2)\r\n- }\r\n- }\r\n }\r\n- {{end}}\r\n+ {{- end}}\r\n {{if not .Required}}}{{end}}\r\n {{end}}\r\n queryURL.RawQuery = queryValues.Encode()\r\n```\r\n\r\nCould likely be further improved if this were further extracted into runtime.\r\n\r\nIn the case of our OpenAPI spec, which is very query-param heavy, it cuts the LOC for the generated code from **~160,000** to **~85,000**. Without this, it would take Go over 10 seconds to even parse the AST/compile due to the amount of generated code. With this change, it's near instant again.\r\n\r\nWe generate query params to add various forms of filtering to our paginated endpoints, though most requests don't actually use most query parameters.","author":{"url":"https://github.com/lrstanley","@type":"Person","name":"lrstanley"},"datePublished":"2024-08-30T03:16:14Z","url":"https://github.com/oapi-codegen/oapi-codegen/issues/1744"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react-reconciler,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@d5750093facf |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:cd571037-336d-f44b-2630-c7194e377741 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | BBF0:333850:2AF3E67:38A6898:6AF9C43F |
| html-safe-nonce | a18b8cb02f3cbfd8ae608710af4ba99c10161e30e406ca9e74cc553e7b96fd86 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCQkYwOjMzMzg1MDoyQUYzRTY3OjM4QTY4OTg6NkFGOUM0M0YiLCJ2aXNpdG9yX2lkIjoiMjY3NDExMDAyMTExNjc0MDY3MSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 223443bb74918001de06575b178a3e74bcdd0b0f456c1412aa4e2fbfe6fb0273 |
| hovercard-subject-tag | issue:2496098909 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/oapi-codegen/oapi-codegen/1744/issue_layout |
| twitter:image | https://opengraph.githubassets.com/67525d9ff75677cb05d6a0162c3b7f6cf8f1e2895ea194aea4a51854d44192d3/oapi-codegen/oapi-codegen/issues/1744 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/67525d9ff75677cb05d6a0162c3b7f6cf8f1e2895ea194aea4a51854d44192d3/oapi-codegen/oapi-codegen/issues/1744 |
| og:image:alt | Unable at the moment to submit a PR, but we've pulled in client.tmpl locally as of commit d232e9e, and have applied the following patch of changes to the client.tmpl template. diff --git a/template... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | lrstanley |
| hostname | github.com |
| expected-hostname | github.com |
| None | 612d3e6ffd693e7ea8d9fcf452370995872baef31d825b1fedef704cd3d24ac7 |
| turbo-cache-control | no-preview |
| go-import | github.com/oapi-codegen/oapi-codegen git https://github.com/oapi-codegen/oapi-codegen.git |
| octolytics-dimension-user_id | 142752710 |
| octolytics-dimension-user_login | oapi-codegen |
| octolytics-dimension-repository_id | 173009358 |
| octolytics-dimension-repository_nwo | oapi-codegen/oapi-codegen |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 173009358 |
| octolytics-dimension-repository_network_root_nwo | oapi-codegen/oapi-codegen |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 072e34035c13c31543fba9f025cbbbda4210dae4 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width