Repository navigation
feat(client): improve perfomance of generating path for client - #2046
Conversation
Kusari Analysis ResultsAnalysis for commit: b690045, performed at: 2025-08-02T11:55:19Z • • Recommendation✅ PROCEED with this Pull Request Summary✅ No Flagged Issues Detected All values appear to be within acceptable risk parameters. No pinned version dependency changes, code issues or exposed secrets detected! Found this helpful? Give it a 👍 or 👎 reaction! |
501b237 to
ea62ed3
Compare
|
Kusari PR Analysis rerun based on - ea62ed3 performed at: 2025-08-02T05:09:15Z - link to updated analysis |
ea62ed3 to
fab1963
Compare
|
Kusari PR Analysis rerun based on - fab1963 performed at: 2025-08-02T05:11:59Z - link to updated analysis |
fab1963 to
a3cd398
Compare
|
Kusari PR Analysis rerun based on - a3cd398 performed at: 2025-08-02T06:16:38Z - link to updated analysis |
a3cd398 to
1511442
Compare
|
Kusari PR Analysis rerun based on - 1511442 performed at: 2025-08-02T07:22:05Z - link to updated analysis |
|
Thanks, will try and review at some point. Is it worth committing the benchmark code for future testing? |
|
Kusari PR Analysis rerun based on - 3acf76d performed at: 2025-08-02T11:53:02Z - link to updated analysis |
I added a commit with a benchmark test |
3acf76d to
b690045
Compare
|
Kusari PR Analysis rerun based on - b690045 performed at: 2025-08-02T11:55:19Z - link to updated analysis |
|
Thanks. From what I can see: Before: After: Is that right? The performance improvement seems fairly low |
# Conflicts: # examples/generate/serverurls/gen.go # internal/test/any_of/codegen/ref_schema/openapi.gen.go # internal/test/any_of/param/param.gen.go # internal/test/issues/issue-1180/issue.gen.go # internal/test/issues/issue-1298/issue1298.gen.go # internal/test/issues/issue-1397/issue1397.gen.go # internal/test/issues/issue-grab_import_names/issue.gen.go # internal/test/options/response_getters/enabled/response_getters.gen.go # internal/test/references/external/pathalias/client.gen.go # internal/test/references/external/response_refs/pkg2/pkg2.gen.go # internal/test/references/external/x_go_name_qualification/gen/spec_base/issue.gen.go # internal/test/schemas/schemas.gen.go # internal/test/strict-server/client/client.gen.go
Regenerates all generated code so the new string-concatenation path builder in client.tmpl is applied to the test and example files that moved during the "refactor all tests by category" reorganization.
GenPathString built a format string and ran it through fmt.Sprintf at
generation time, so any percent sign in a spec path was interpreted as a
format verb. Percent-encoded paths such as /search/%20/{id} are legal
OpenAPI and produced corrupt output like
"/search/%!/(string= " + pathParam0 + ")%!s(MISSING)"
Deriving the parameter count from strings.Count(tmpl, "%s") compounded
this: a path containing a literal "%s" was counted as a parameter, so
/literal%s/{id} emitted a reference to an undeclared pathParam1.
Literal segments were also pasted into the generated source unquoted,
so a path containing a double quote or a backslash produced Go code
that did not compile or escaped incorrectly.
Build the concatenation from the regexp match offsets instead, quoting
each literal segment with strconv.Quote. Percent signs, quotes and
backslashes are then correct by construction, and the parameter count
comes from the number of matches rather than from counting needles in
an intermediate string.
Output is unchanged for every spec in the repository; `make generate`
produces no diff.
The surrounding template helpers are genParamArgs, genParamTypes and genParamNames, and the Go function backing this one is GenPathString, so getPathString reads as a typo. Rename it before it settles. Generated output is unchanged.
|
@greptileai review |
Greptile SummaryThe PR replaces
Confidence Score: 5/5The PR appears safe to merge. No blocking failure remains; the previously reported repeated-parameter compilation issue is addressed by reusing the first variable index for duplicate placeholders, and the other prior compatibility concern was explicitly accepted as intentional.
|
| Filename | Overview |
|---|---|
| pkg/codegen/utils.go | Adds path-expression generation whose placeholder normalization and deduplication align with the existing path-parameter ordering logic. |
| pkg/codegen/templates/client.tmpl | Uses the new helper to emit string concatenation instead of runtime formatting for generated client paths. |
| pkg/codegen/template_helpers.go | Registers the new path-generation helper; the removed legacy helper was already reviewed and explicitly accepted as an intentional compatibility change. |
| pkg/codegen/utils_test.go | Adds focused coverage for literals, escaping, parameter ordering, repeated parameters, and supported placeholder forms. |
Reviews (2): Last reviewed commit: "fix(codegen): reuse the same variable fo..." | Re-trigger Greptile
A path parameter may appear more than once in one path, as in the
Keycloak-style /admin/realms/{realm}/clients/{client-uuid}/roles/{role-name}/composites/clients/{client-uuid}
that motivated oapi-codegen#2220. SortParamsByPath deduplicates such parameters and
the client template declares one variable per deduplicated parameter,
but GenPathString numbered variables by occurrence, so the second
{client-uuid} referred to a pathParam that was never declared and the
generated client failed to compile.
Key the variable index by parameter name so repeats resolve back to the
first occurrence, and treat the prefixed and exploded forms {.param} and
{;param*} as the same parameter, matching OrderedParamsFromUri.
This also corrects the output rather than only restoring compilation.
The previous fmt.Sprintf form emitted one %s per occurrence but only one
argument per unique parameter, so the generated code failed go vet and
put a literal %!s(MISSING) in the request path at runtime.
|
I found a few real bugs (and then so did Greptile) when reviewing your PR. I've pushed fixes and I'll merge this, so you get PR credit. Thanks for doing this. It's always good to avoid unnecessary function calls, even if the gain is minimal when you look at the big picture, vs a synthetic benchmark. |
In this pull request, I made a small performance improvement to the client by using string concatenation instead of fmt.Sprintf. Below, I’ll include the benchmark results.