From 1511442c45cd6e2f68ed84db670b83de2f438913 Mon Sep 17 00:00:00 2001 From: Sterligov Denis Date: Sat, 2 Aug 2025 09:53:44 +0500 Subject: [PATCH 1/2] feat(client): improve perfomance of generating path for client --- .../authenticated-api/echo/api/api.gen.go | 4 +- .../authenticated-api/stdhttp/api/api.gen.go | 4 +- examples/client/client.gen.go | 5 +- examples/clienttypenameclash/client.gen.go | 3 +- .../custom-client-type.gen.go | 3 +- examples/generate/serverurls/gen.go | 1 + .../petstore-expanded/petstore-client.gen.go | 9 ++-- .../test/any_of/codegen/inline/openapi.gen.go | 3 +- .../any_of/codegen/ref_schema/openapi.gen.go | 3 +- internal/test/any_of/param/param.gen.go | 3 +- internal/test/client/client.gen.go | 17 +++---- internal/test/issues/issue-1039/client.gen.go | 3 +- internal/test/issues/issue-1087/api.gen.go | 2 +- internal/test/issues/issue-1180/issue.gen.go | 2 +- .../test/issues/issue-1182/pkg1/pkg1.gen.go | 2 +- .../test/issues/issue-1189/issue1189.gen.go | 2 +- .../issue-1208-1209/issue-multi-json.gen.go | 2 +- .../test/issues/issue-1212/pkg1/pkg1.gen.go | 2 +- .../test/issues/issue-1298/issue1298.gen.go | 2 +- .../test/issues/issue-1397/issue1397.gen.go | 2 +- internal/test/issues/issue-312/issue.gen.go | 4 +- internal/test/issues/issue-52/issue.gen.go | 2 +- .../issue-grab_import_names/issue.gen.go | 2 +- .../issue-illegal_enum_names/issue.gen.go | 2 +- internal/test/issues/issue240/client.gen.go | 5 +- .../name_normalizer.gen.go | 2 +- .../name_normalizer.gen.go | 2 +- .../name_normalizer.gen.go | 2 +- .../to-camel-case/name_normalizer.gen.go | 2 +- .../unset/name_normalizer.gen.go | 2 +- internal/test/parameters/parameters.gen.go | 42 ++++++++-------- internal/test/schemas/schemas.gen.go | 20 ++++---- .../test/strict-server/client/client.gen.go | 25 +++++----- pkg/codegen/template_helpers.go | 2 +- pkg/codegen/templates/client.tmpl | 2 +- pkg/codegen/utils.go | 27 ++++++++-- pkg/codegen/utils_test.go | 50 +++++++++++++++++-- 37 files changed, 161 insertions(+), 106 deletions(-) diff --git a/examples/authenticated-api/echo/api/api.gen.go b/examples/authenticated-api/echo/api/api.gen.go index 383ab5dc25..5f511b1576 100644 --- a/examples/authenticated-api/echo/api/api.gen.go +++ b/examples/authenticated-api/echo/api/api.gen.go @@ -174,7 +174,7 @@ func NewListThingsRequest(server string) (*http.Request, error) { return nil, err } - operationPath := fmt.Sprintf("/things") + operationPath := "/things" if operationPath[0] == '/' { operationPath = "." + operationPath } @@ -212,7 +212,7 @@ func NewAddThingRequestWithBody(server string, contentType string, body io.Reade return nil, err } - operationPath := fmt.Sprintf("/things") + operationPath := "/things" if operationPath[0] == '/' { operationPath = "." + operationPath } diff --git a/examples/authenticated-api/stdhttp/api/api.gen.go b/examples/authenticated-api/stdhttp/api/api.gen.go index 91ced48c3d..28c712c40d 100644 --- a/examples/authenticated-api/stdhttp/api/api.gen.go +++ b/examples/authenticated-api/stdhttp/api/api.gen.go @@ -175,7 +175,7 @@ func NewListThingsRequest(server string) (*http.Request, error) { return nil, err } - operationPath := fmt.Sprintf("/things") + operationPath := "/things" if operationPath[0] == '/' { operationPath = "." + operationPath } @@ -213,7 +213,7 @@ func NewAddThingRequestWithBody(server string, contentType string, body io.Reade return nil, err } - operationPath := fmt.Sprintf("/things") + operationPath := "/things" if operationPath[0] == '/' { operationPath = "." + operationPath } diff --git a/examples/client/client.gen.go b/examples/client/client.gen.go index 8ef6b78564..208e464460 100644 --- a/examples/client/client.gen.go +++ b/examples/client/client.gen.go @@ -6,7 +6,6 @@ package client import ( "context" "encoding/json" - "fmt" "io" "net/http" "net/url" @@ -131,7 +130,7 @@ func NewGetClientRequest(server string) (*http.Request, error) { return nil, err } - operationPath := fmt.Sprintf("/client") + operationPath := "/client" if operationPath[0] == '/' { operationPath = "." + operationPath } @@ -158,7 +157,7 @@ func NewUpdateClientRequest(server string) (*http.Request, error) { return nil, err } - operationPath := fmt.Sprintf("/client") + operationPath := "/client" if operationPath[0] == '/' { operationPath = "." + operationPath } diff --git a/examples/clienttypenameclash/client.gen.go b/examples/clienttypenameclash/client.gen.go index 219a2a0d4d..f461ac5c81 100644 --- a/examples/clienttypenameclash/client.gen.go +++ b/examples/clienttypenameclash/client.gen.go @@ -6,7 +6,6 @@ package client import ( "context" "encoding/json" - "fmt" "io" "net/http" "net/url" @@ -116,7 +115,7 @@ func NewUpdateClientRequest(server string) (*http.Request, error) { return nil, err } - operationPath := fmt.Sprintf("/client") + operationPath := "/client" if operationPath[0] == '/' { operationPath = "." + operationPath } diff --git a/examples/custom-client-type/custom-client-type.gen.go b/examples/custom-client-type/custom-client-type.gen.go index 6dd2ef0195..b1ad7b9f7a 100644 --- a/examples/custom-client-type/custom-client-type.gen.go +++ b/examples/custom-client-type/custom-client-type.gen.go @@ -6,7 +6,6 @@ package customclienttype import ( "context" "encoding/json" - "fmt" "io" "net/http" "net/url" @@ -116,7 +115,7 @@ func NewGetClientRequest(server string) (*http.Request, error) { return nil, err } - operationPath := fmt.Sprintf("/client") + operationPath := "/client" if operationPath[0] == '/' { operationPath = "." + operationPath } diff --git a/examples/generate/serverurls/gen.go b/examples/generate/serverurls/gen.go index 5f9e069d3e..06b91f7e58 100644 --- a/examples/generate/serverurls/gen.go +++ b/examples/generate/serverurls/gen.go @@ -62,6 +62,7 @@ func NewServerUrlTheProductionAPIServer(basePath ServerUrlTheProductionAPIServer u = strings.ReplaceAll(u, "{basePath}", string(basePath)) u = strings.ReplaceAll(u, "{noDefault}", string(noDefault)) + // TODO in the future, this will validate that the value is part of the ServerUrlTheProductionAPIServerPortVariable enum u = strings.ReplaceAll(u, "{port}", string(port)) u = strings.ReplaceAll(u, "{username}", string(username)) diff --git a/examples/petstore-expanded/petstore-client.gen.go b/examples/petstore-expanded/petstore-client.gen.go index 9bd928f497..98a206f14e 100644 --- a/examples/petstore-expanded/petstore-client.gen.go +++ b/examples/petstore-expanded/petstore-client.gen.go @@ -7,7 +7,6 @@ import ( "bytes" "context" "encoding/json" - "fmt" "io" "net/http" "net/url" @@ -215,7 +214,7 @@ func NewFindPetsRequest(server string, params *FindPetsParams) (*http.Request, e return nil, err } - operationPath := fmt.Sprintf("/pets") + operationPath := "/pets" if operationPath[0] == '/' { operationPath = "." + operationPath } @@ -291,7 +290,7 @@ func NewAddPetRequestWithBody(server string, contentType string, body io.Reader) return nil, err } - operationPath := fmt.Sprintf("/pets") + operationPath := "/pets" if operationPath[0] == '/' { operationPath = "." + operationPath } @@ -327,7 +326,7 @@ func NewDeletePetRequest(server string, id int64) (*http.Request, error) { return nil, err } - operationPath := fmt.Sprintf("/pets/%s", pathParam0) + operationPath := "/pets/" + pathParam0 if operationPath[0] == '/' { operationPath = "." + operationPath } @@ -361,7 +360,7 @@ func NewFindPetByIDRequest(server string, id int64) (*http.Request, error) { return nil, err } - operationPath := fmt.Sprintf("/pets/%s", pathParam0) + operationPath := "/pets/" + pathParam0 if operationPath[0] == '/' { operationPath = "." + operationPath } diff --git a/internal/test/any_of/codegen/inline/openapi.gen.go b/internal/test/any_of/codegen/inline/openapi.gen.go index d3f34bec6e..1f8e9ec378 100644 --- a/internal/test/any_of/codegen/inline/openapi.gen.go +++ b/internal/test/any_of/codegen/inline/openapi.gen.go @@ -6,7 +6,6 @@ package inline import ( "context" "encoding/json" - "fmt" "io" "net/http" "net/url" @@ -143,7 +142,7 @@ func NewGetPetsRequest(server string) (*http.Request, error) { return nil, err } - operationPath := fmt.Sprintf("/pets") + operationPath := "/pets" if operationPath[0] == '/' { operationPath = "." + operationPath } diff --git a/internal/test/any_of/codegen/ref_schema/openapi.gen.go b/internal/test/any_of/codegen/ref_schema/openapi.gen.go index 38f87c47be..ee74b4d335 100644 --- a/internal/test/any_of/codegen/ref_schema/openapi.gen.go +++ b/internal/test/any_of/codegen/ref_schema/openapi.gen.go @@ -6,7 +6,6 @@ package ref_schema import ( "context" "encoding/json" - "fmt" "io" "net/http" "net/url" @@ -242,7 +241,7 @@ func NewGetPetsRequest(server string) (*http.Request, error) { return nil, err } - operationPath := fmt.Sprintf("/pets") + operationPath := "/pets" if operationPath[0] == '/' { operationPath = "." + operationPath } diff --git a/internal/test/any_of/param/param.gen.go b/internal/test/any_of/param/param.gen.go index 3378ae3da1..2278405baf 100644 --- a/internal/test/any_of/param/param.gen.go +++ b/internal/test/any_of/param/param.gen.go @@ -6,7 +6,6 @@ package param import ( "context" "encoding/json" - "fmt" "io" "net/http" "net/url" @@ -271,7 +270,7 @@ func NewGetTestRequest(server string, params *GetTestParams) (*http.Request, err return nil, err } - operationPath := fmt.Sprintf("/test") + operationPath := "/test" if operationPath[0] == '/' { operationPath = "." + operationPath } diff --git a/internal/test/client/client.gen.go b/internal/test/client/client.gen.go index 275c72cf0f..58b315bc7f 100644 --- a/internal/test/client/client.gen.go +++ b/internal/test/client/client.gen.go @@ -7,7 +7,6 @@ import ( "bytes" "context" "encoding/json" - "fmt" "io" "net/http" "net/url" @@ -292,7 +291,7 @@ func NewPostBothRequestWithBody(server string, contentType string, body io.Reade return nil, err } - operationPath := fmt.Sprintf("/with_both_bodies") + operationPath := "/with_both_bodies" if operationPath[0] == '/' { operationPath = "." + operationPath } @@ -321,7 +320,7 @@ func NewGetBothRequest(server string) (*http.Request, error) { return nil, err } - operationPath := fmt.Sprintf("/with_both_responses") + operationPath := "/with_both_responses" if operationPath[0] == '/' { operationPath = "." + operationPath } @@ -359,7 +358,7 @@ func NewPostJsonRequestWithBody(server string, contentType string, body io.Reade return nil, err } - operationPath := fmt.Sprintf("/with_json_body") + operationPath := "/with_json_body" if operationPath[0] == '/' { operationPath = "." + operationPath } @@ -388,7 +387,7 @@ func NewGetJsonRequest(server string) (*http.Request, error) { return nil, err } - operationPath := fmt.Sprintf("/with_json_response") + operationPath := "/with_json_response" if operationPath[0] == '/' { operationPath = "." + operationPath } @@ -415,7 +414,7 @@ func NewPostOtherRequestWithBody(server string, contentType string, body io.Read return nil, err } - operationPath := fmt.Sprintf("/with_other_body") + operationPath := "/with_other_body" if operationPath[0] == '/' { operationPath = "." + operationPath } @@ -444,7 +443,7 @@ func NewGetOtherRequest(server string) (*http.Request, error) { return nil, err } - operationPath := fmt.Sprintf("/with_other_response") + operationPath := "/with_other_response" if operationPath[0] == '/' { operationPath = "." + operationPath } @@ -471,7 +470,7 @@ func NewGetJsonWithTrailingSlashRequest(server string) (*http.Request, error) { return nil, err } - operationPath := fmt.Sprintf("/with_trailing_slash/") + operationPath := "/with_trailing_slash/" if operationPath[0] == '/' { operationPath = "." + operationPath } @@ -509,7 +508,7 @@ func NewPostVendorJsonRequestWithBody(server string, contentType string, body io return nil, err } - operationPath := fmt.Sprintf("/with_vendor_json") + operationPath := "/with_vendor_json" if operationPath[0] == '/' { operationPath = "." + operationPath } diff --git a/internal/test/issues/issue-1039/client.gen.go b/internal/test/issues/issue-1039/client.gen.go index 3114f29862..c1edb191f6 100644 --- a/internal/test/issues/issue-1039/client.gen.go +++ b/internal/test/issues/issue-1039/client.gen.go @@ -7,7 +7,6 @@ import ( "bytes" "context" "encoding/json" - "fmt" "io" "net/http" "net/url" @@ -137,7 +136,7 @@ func NewExamplePatchRequestWithBody(server string, contentType string, body io.R return nil, err } - operationPath := fmt.Sprintf("/example") + operationPath := "/example" if operationPath[0] == '/' { operationPath = "." + operationPath } diff --git a/internal/test/issues/issue-1087/api.gen.go b/internal/test/issues/issue-1087/api.gen.go index 3c408cc8f9..a22c717757 100644 --- a/internal/test/issues/issue-1087/api.gen.go +++ b/internal/test/issues/issue-1087/api.gen.go @@ -131,7 +131,7 @@ func NewGetThingsRequest(server string) (*http.Request, error) { return nil, err } - operationPath := fmt.Sprintf("/api/my/path") + operationPath := "/api/my/path" if operationPath[0] == '/' { operationPath = "." + operationPath } diff --git a/internal/test/issues/issue-1180/issue.gen.go b/internal/test/issues/issue-1180/issue.gen.go index cc675b96da..4c689de158 100644 --- a/internal/test/issues/issue-1180/issue.gen.go +++ b/internal/test/issues/issue-1180/issue.gen.go @@ -125,7 +125,7 @@ func NewGetSimplePrimitiveRequest(server string, param string) (*http.Request, e return nil, err } - operationPath := fmt.Sprintf("/simplePrimitive/%s", pathParam0) + operationPath := "/simplePrimitive/" + pathParam0 if operationPath[0] == '/' { operationPath = "." + operationPath } diff --git a/internal/test/issues/issue-1182/pkg1/pkg1.gen.go b/internal/test/issues/issue-1182/pkg1/pkg1.gen.go index 0b01c5738e..e7db94ec9d 100644 --- a/internal/test/issues/issue-1182/pkg1/pkg1.gen.go +++ b/internal/test/issues/issue-1182/pkg1/pkg1.gen.go @@ -119,7 +119,7 @@ func NewTestGetRequest(server string) (*http.Request, error) { return nil, err } - operationPath := fmt.Sprintf("/test") + operationPath := "/test" if operationPath[0] == '/' { operationPath = "." + operationPath } diff --git a/internal/test/issues/issue-1189/issue1189.gen.go b/internal/test/issues/issue-1189/issue1189.gen.go index c1a869ad98..92d97e9938 100644 --- a/internal/test/issues/issue-1189/issue1189.gen.go +++ b/internal/test/issues/issue-1189/issue1189.gen.go @@ -293,7 +293,7 @@ func NewTestRequest(server string) (*http.Request, error) { return nil, err } - operationPath := fmt.Sprintf("/test") + operationPath := "/test" if operationPath[0] == '/' { operationPath = "." + operationPath } diff --git a/internal/test/issues/issue-1208-1209/issue-multi-json.gen.go b/internal/test/issues/issue-1208-1209/issue-multi-json.gen.go index 780d45e6a7..e1b67754ef 100644 --- a/internal/test/issues/issue-1208-1209/issue-multi-json.gen.go +++ b/internal/test/issues/issue-1208-1209/issue-multi-json.gen.go @@ -135,7 +135,7 @@ func NewTestRequest(server string) (*http.Request, error) { return nil, err } - operationPath := fmt.Sprintf("/test") + operationPath := "/test" if operationPath[0] == '/' { operationPath = "." + operationPath } diff --git a/internal/test/issues/issue-1212/pkg1/pkg1.gen.go b/internal/test/issues/issue-1212/pkg1/pkg1.gen.go index c24850b199..28498a36ea 100644 --- a/internal/test/issues/issue-1212/pkg1/pkg1.gen.go +++ b/internal/test/issues/issue-1212/pkg1/pkg1.gen.go @@ -121,7 +121,7 @@ func NewTestRequest(server string) (*http.Request, error) { return nil, err } - operationPath := fmt.Sprintf("/test") + operationPath := "/test" if operationPath[0] == '/' { operationPath = "." + operationPath } diff --git a/internal/test/issues/issue-1298/issue1298.gen.go b/internal/test/issues/issue-1298/issue1298.gen.go index 94734a5772..389e47070b 100644 --- a/internal/test/issues/issue-1298/issue1298.gen.go +++ b/internal/test/issues/issue-1298/issue1298.gen.go @@ -149,7 +149,7 @@ func NewTestRequestWithBody(server string, contentType string, body io.Reader) ( return nil, err } - operationPath := fmt.Sprintf("/test") + operationPath := "/test" if operationPath[0] == '/' { operationPath = "." + operationPath } diff --git a/internal/test/issues/issue-1397/issue1397.gen.go b/internal/test/issues/issue-1397/issue1397.gen.go index 2463641eb1..2088d79c8c 100644 --- a/internal/test/issues/issue-1397/issue1397.gen.go +++ b/internal/test/issues/issue-1397/issue1397.gen.go @@ -179,7 +179,7 @@ func NewTestRequestWithBody(server string, contentType string, body io.Reader) ( return nil, err } - operationPath := fmt.Sprintf("/test") + operationPath := "/test" if operationPath[0] == '/' { operationPath = "." + operationPath } diff --git a/internal/test/issues/issue-312/issue.gen.go b/internal/test/issues/issue-312/issue.gen.go index f0285c071f..199374be0c 100644 --- a/internal/test/issues/issue-312/issue.gen.go +++ b/internal/test/issues/issue-312/issue.gen.go @@ -179,7 +179,7 @@ func NewGetPetRequest(server string, petId string) (*http.Request, error) { return nil, err } - operationPath := fmt.Sprintf("/pets/%s", pathParam0) + operationPath := "/pets/" + pathParam0 if operationPath[0] == '/' { operationPath = "." + operationPath } @@ -217,7 +217,7 @@ func NewValidatePetsRequestWithBody(server string, contentType string, body io.R return nil, err } - operationPath := fmt.Sprintf("/pets:validate") + operationPath := "/pets:validate" if operationPath[0] == '/' { operationPath = "." + operationPath } diff --git a/internal/test/issues/issue-52/issue.gen.go b/internal/test/issues/issue-52/issue.gen.go index f024999d6a..26694296c6 100644 --- a/internal/test/issues/issue-52/issue.gen.go +++ b/internal/test/issues/issue-52/issue.gen.go @@ -132,7 +132,7 @@ func NewExampleGetRequest(server string) (*http.Request, error) { return nil, err } - operationPath := fmt.Sprintf("/example") + operationPath := "/example" if operationPath[0] == '/' { operationPath = "." + operationPath } diff --git a/internal/test/issues/issue-grab_import_names/issue.gen.go b/internal/test/issues/issue-grab_import_names/issue.gen.go index 52564c0dd9..13b47da05e 100644 --- a/internal/test/issues/issue-grab_import_names/issue.gen.go +++ b/internal/test/issues/issue-grab_import_names/issue.gen.go @@ -128,7 +128,7 @@ func NewGetFooRequest(server string, params *GetFooParams) (*http.Request, error return nil, err } - operationPath := fmt.Sprintf("/foo") + operationPath := "/foo" if operationPath[0] == '/' { operationPath = "." + operationPath } diff --git a/internal/test/issues/issue-illegal_enum_names/issue.gen.go b/internal/test/issues/issue-illegal_enum_names/issue.gen.go index 8f907486cf..119907abe9 100644 --- a/internal/test/issues/issue-illegal_enum_names/issue.gen.go +++ b/internal/test/issues/issue-illegal_enum_names/issue.gen.go @@ -135,7 +135,7 @@ func NewGetFooRequest(server string) (*http.Request, error) { return nil, err } - operationPath := fmt.Sprintf("/foo") + operationPath := "/foo" if operationPath[0] == '/' { operationPath = "." + operationPath } diff --git a/internal/test/issues/issue240/client.gen.go b/internal/test/issues/issue240/client.gen.go index 8a0a533589..8bf42e0868 100644 --- a/internal/test/issues/issue240/client.gen.go +++ b/internal/test/issues/issue240/client.gen.go @@ -6,7 +6,6 @@ package issue240 import ( "context" "encoding/json" - "fmt" "io" "net/http" "net/url" @@ -131,7 +130,7 @@ func NewGetClientRequest(server string) (*http.Request, error) { return nil, err } - operationPath := fmt.Sprintf("/client") + operationPath := "/client" if operationPath[0] == '/' { operationPath = "." + operationPath } @@ -158,7 +157,7 @@ func NewUpdateClientRequest(server string) (*http.Request, error) { return nil, err } - operationPath := fmt.Sprintf("/client") + operationPath := "/client" if operationPath[0] == '/' { operationPath = "." + operationPath } diff --git a/internal/test/outputoptions/name-normalizer/to-camel-case-with-additional-initialisms/name_normalizer.gen.go b/internal/test/outputoptions/name-normalizer/to-camel-case-with-additional-initialisms/name_normalizer.gen.go index b04b92ae87..2ec5c39394 100644 --- a/internal/test/outputoptions/name-normalizer/to-camel-case-with-additional-initialisms/name_normalizer.gen.go +++ b/internal/test/outputoptions/name-normalizer/to-camel-case-with-additional-initialisms/name_normalizer.gen.go @@ -222,7 +222,7 @@ func NewGetHTTPPetRequest(server string, petID string) (*http.Request, error) { return nil, err } - operationPath := fmt.Sprintf("/api/pets/%s", pathParam0) + operationPath := "/api/pets/" + pathParam0 if operationPath[0] == '/' { operationPath = "." + operationPath } diff --git a/internal/test/outputoptions/name-normalizer/to-camel-case-with-digits/name_normalizer.gen.go b/internal/test/outputoptions/name-normalizer/to-camel-case-with-digits/name_normalizer.gen.go index d2a11387d0..a58044dd53 100644 --- a/internal/test/outputoptions/name-normalizer/to-camel-case-with-digits/name_normalizer.gen.go +++ b/internal/test/outputoptions/name-normalizer/to-camel-case-with-digits/name_normalizer.gen.go @@ -222,7 +222,7 @@ func NewGetHttpPetRequest(server string, petId string) (*http.Request, error) { return nil, err } - operationPath := fmt.Sprintf("/api/pets/%s", pathParam0) + operationPath := "/api/pets/" + pathParam0 if operationPath[0] == '/' { operationPath = "." + operationPath } diff --git a/internal/test/outputoptions/name-normalizer/to-camel-case-with-initialisms/name_normalizer.gen.go b/internal/test/outputoptions/name-normalizer/to-camel-case-with-initialisms/name_normalizer.gen.go index 3b2a1fa751..9c9fe16b76 100644 --- a/internal/test/outputoptions/name-normalizer/to-camel-case-with-initialisms/name_normalizer.gen.go +++ b/internal/test/outputoptions/name-normalizer/to-camel-case-with-initialisms/name_normalizer.gen.go @@ -222,7 +222,7 @@ func NewGetHTTPPetRequest(server string, petID string) (*http.Request, error) { return nil, err } - operationPath := fmt.Sprintf("/api/pets/%s", pathParam0) + operationPath := "/api/pets/" + pathParam0 if operationPath[0] == '/' { operationPath = "." + operationPath } diff --git a/internal/test/outputoptions/name-normalizer/to-camel-case/name_normalizer.gen.go b/internal/test/outputoptions/name-normalizer/to-camel-case/name_normalizer.gen.go index 3e4ff389e2..b853a9371f 100644 --- a/internal/test/outputoptions/name-normalizer/to-camel-case/name_normalizer.gen.go +++ b/internal/test/outputoptions/name-normalizer/to-camel-case/name_normalizer.gen.go @@ -222,7 +222,7 @@ func NewGetHttpPetRequest(server string, petId string) (*http.Request, error) { return nil, err } - operationPath := fmt.Sprintf("/api/pets/%s", pathParam0) + operationPath := "/api/pets/" + pathParam0 if operationPath[0] == '/' { operationPath = "." + operationPath } diff --git a/internal/test/outputoptions/name-normalizer/unset/name_normalizer.gen.go b/internal/test/outputoptions/name-normalizer/unset/name_normalizer.gen.go index a2ef05ae8b..da64231c9d 100644 --- a/internal/test/outputoptions/name-normalizer/unset/name_normalizer.gen.go +++ b/internal/test/outputoptions/name-normalizer/unset/name_normalizer.gen.go @@ -222,7 +222,7 @@ func NewGetHttpPetRequest(server string, petId string) (*http.Request, error) { return nil, err } - operationPath := fmt.Sprintf("/api/pets/%s", pathParam0) + operationPath := "/api/pets/" + pathParam0 if operationPath[0] == '/' { operationPath = "." + operationPath } diff --git a/internal/test/parameters/parameters.gen.go b/internal/test/parameters/parameters.gen.go index 630770731a..f8090d3a1c 100644 --- a/internal/test/parameters/parameters.gen.go +++ b/internal/test/parameters/parameters.gen.go @@ -546,7 +546,7 @@ func NewGetContentObjectRequest(server string, param ComplexObject) (*http.Reque return nil, err } - operationPath := fmt.Sprintf("/contentObject/%s", pathParam0) + operationPath := "/contentObject/" + pathParam0 if operationPath[0] == '/' { operationPath = "." + operationPath } @@ -573,7 +573,7 @@ func NewGetCookieRequest(server string, params *GetCookieParams) (*http.Request, return nil, err } - operationPath := fmt.Sprintf("/cookie") + operationPath := "/cookie" if operationPath[0] == '/' { operationPath = "." + operationPath } @@ -724,7 +724,7 @@ func NewEnumParamsRequest(server string, params *EnumParamsParams) (*http.Reques return nil, err } - operationPath := fmt.Sprintf("/enums") + operationPath := "/enums" if operationPath[0] == '/' { operationPath = "." + operationPath } @@ -773,7 +773,7 @@ func NewGetHeaderRequest(server string, params *GetHeaderParams) (*http.Request, return nil, err } - operationPath := fmt.Sprintf("/header") + operationPath := "/header" if operationPath[0] == '/' { operationPath = "." + operationPath } @@ -901,7 +901,7 @@ func NewGetLabelExplodeArrayRequest(server string, param []int32) (*http.Request return nil, err } - operationPath := fmt.Sprintf("/labelExplodeArray/%s", pathParam0) + operationPath := "/labelExplodeArray/" + pathParam0 if operationPath[0] == '/' { operationPath = "." + operationPath } @@ -935,7 +935,7 @@ func NewGetLabelExplodeObjectRequest(server string, param Object) (*http.Request return nil, err } - operationPath := fmt.Sprintf("/labelExplodeObject/%s", pathParam0) + operationPath := "/labelExplodeObject/" + pathParam0 if operationPath[0] == '/' { operationPath = "." + operationPath } @@ -969,7 +969,7 @@ func NewGetLabelNoExplodeArrayRequest(server string, param []int32) (*http.Reque return nil, err } - operationPath := fmt.Sprintf("/labelNoExplodeArray/%s", pathParam0) + operationPath := "/labelNoExplodeArray/" + pathParam0 if operationPath[0] == '/' { operationPath = "." + operationPath } @@ -1003,7 +1003,7 @@ func NewGetLabelNoExplodeObjectRequest(server string, param Object) (*http.Reque return nil, err } - operationPath := fmt.Sprintf("/labelNoExplodeObject/%s", pathParam0) + operationPath := "/labelNoExplodeObject/" + pathParam0 if operationPath[0] == '/' { operationPath = "." + operationPath } @@ -1037,7 +1037,7 @@ func NewGetMatrixExplodeArrayRequest(server string, id []int32) (*http.Request, return nil, err } - operationPath := fmt.Sprintf("/matrixExplodeArray/%s", pathParam0) + operationPath := "/matrixExplodeArray/" + pathParam0 if operationPath[0] == '/' { operationPath = "." + operationPath } @@ -1071,7 +1071,7 @@ func NewGetMatrixExplodeObjectRequest(server string, id Object) (*http.Request, return nil, err } - operationPath := fmt.Sprintf("/matrixExplodeObject/%s", pathParam0) + operationPath := "/matrixExplodeObject/" + pathParam0 if operationPath[0] == '/' { operationPath = "." + operationPath } @@ -1105,7 +1105,7 @@ func NewGetMatrixNoExplodeArrayRequest(server string, id []int32) (*http.Request return nil, err } - operationPath := fmt.Sprintf("/matrixNoExplodeArray/%s", pathParam0) + operationPath := "/matrixNoExplodeArray/" + pathParam0 if operationPath[0] == '/' { operationPath = "." + operationPath } @@ -1139,7 +1139,7 @@ func NewGetMatrixNoExplodeObjectRequest(server string, id Object) (*http.Request return nil, err } - operationPath := fmt.Sprintf("/matrixNoExplodeObject/%s", pathParam0) + operationPath := "/matrixNoExplodeObject/" + pathParam0 if operationPath[0] == '/' { operationPath = "." + operationPath } @@ -1170,7 +1170,7 @@ func NewGetPassThroughRequest(server string, param string) (*http.Request, error return nil, err } - operationPath := fmt.Sprintf("/passThrough/%s", pathParam0) + operationPath := "/passThrough/" + pathParam0 if operationPath[0] == '/' { operationPath = "." + operationPath } @@ -1197,7 +1197,7 @@ func NewGetDeepObjectRequest(server string, params *GetDeepObjectParams) (*http. return nil, err } - operationPath := fmt.Sprintf("/queryDeepObject") + operationPath := "/queryDeepObject" if operationPath[0] == '/' { operationPath = "." + operationPath } @@ -1242,7 +1242,7 @@ func NewGetQueryFormRequest(server string, params *GetQueryFormParams) (*http.Re return nil, err } - operationPath := fmt.Sprintf("/queryForm") + operationPath := "/queryForm" if operationPath[0] == '/' { operationPath = "." + operationPath } @@ -1420,7 +1420,7 @@ func NewGetSimpleExplodeArrayRequest(server string, param []int32) (*http.Reques return nil, err } - operationPath := fmt.Sprintf("/simpleExplodeArray/%s", pathParam0) + operationPath := "/simpleExplodeArray/" + pathParam0 if operationPath[0] == '/' { operationPath = "." + operationPath } @@ -1454,7 +1454,7 @@ func NewGetSimpleExplodeObjectRequest(server string, param Object) (*http.Reques return nil, err } - operationPath := fmt.Sprintf("/simpleExplodeObject/%s", pathParam0) + operationPath := "/simpleExplodeObject/" + pathParam0 if operationPath[0] == '/' { operationPath = "." + operationPath } @@ -1488,7 +1488,7 @@ func NewGetSimpleNoExplodeArrayRequest(server string, param []int32) (*http.Requ return nil, err } - operationPath := fmt.Sprintf("/simpleNoExplodeArray/%s", pathParam0) + operationPath := "/simpleNoExplodeArray/" + pathParam0 if operationPath[0] == '/' { operationPath = "." + operationPath } @@ -1522,7 +1522,7 @@ func NewGetSimpleNoExplodeObjectRequest(server string, param Object) (*http.Requ return nil, err } - operationPath := fmt.Sprintf("/simpleNoExplodeObject/%s", pathParam0) + operationPath := "/simpleNoExplodeObject/" + pathParam0 if operationPath[0] == '/' { operationPath = "." + operationPath } @@ -1556,7 +1556,7 @@ func NewGetSimplePrimitiveRequest(server string, param int32) (*http.Request, er return nil, err } - operationPath := fmt.Sprintf("/simplePrimitive/%s", pathParam0) + operationPath := "/simplePrimitive/" + pathParam0 if operationPath[0] == '/' { operationPath = "." + operationPath } @@ -1587,7 +1587,7 @@ func NewGetStartingWithNumberRequest(server string, n1param string) (*http.Reque return nil, err } - operationPath := fmt.Sprintf("/startingWithNumber/%s", pathParam0) + operationPath := "/startingWithNumber/" + pathParam0 if operationPath[0] == '/' { operationPath = "." + operationPath } diff --git a/internal/test/schemas/schemas.gen.go b/internal/test/schemas/schemas.gen.go index 1a7f9e61b7..9fce1e1b44 100644 --- a/internal/test/schemas/schemas.gen.go +++ b/internal/test/schemas/schemas.gen.go @@ -374,7 +374,7 @@ func NewEnsureEverythingIsReferencedRequest(server string) (*http.Request, error return nil, err } - operationPath := fmt.Sprintf("/ensure-everything-is-referenced") + operationPath := "/ensure-everything-is-referenced" if operationPath[0] == '/' { operationPath = "." + operationPath } @@ -401,7 +401,7 @@ func NewIssue1051Request(server string) (*http.Request, error) { return nil, err } - operationPath := fmt.Sprintf("/issues/1051") + operationPath := "/issues/1051" if operationPath[0] == '/' { operationPath = "." + operationPath } @@ -428,7 +428,7 @@ func NewIssue127Request(server string) (*http.Request, error) { return nil, err } - operationPath := fmt.Sprintf("/issues/127") + operationPath := "/issues/127" if operationPath[0] == '/' { operationPath = "." + operationPath } @@ -466,7 +466,7 @@ func NewIssue185RequestWithBody(server string, contentType string, body io.Reade return nil, err } - operationPath := fmt.Sprintf("/issues/185") + operationPath := "/issues/185" if operationPath[0] == '/' { operationPath = "." + operationPath } @@ -502,7 +502,7 @@ func NewIssue209Request(server string, str StringInPath) (*http.Request, error) return nil, err } - operationPath := fmt.Sprintf("/issues/209/$%s", pathParam0) + operationPath := "/issues/209/$" + pathParam0 if operationPath[0] == '/' { operationPath = "." + operationPath } @@ -536,7 +536,7 @@ func NewIssue30Request(server string, pFallthrough string) (*http.Request, error return nil, err } - operationPath := fmt.Sprintf("/issues/30/%s", pathParam0) + operationPath := "/issues/30/" + pathParam0 if operationPath[0] == '/' { operationPath = "." + operationPath } @@ -563,7 +563,7 @@ func NewGetIssues375Request(server string) (*http.Request, error) { return nil, err } - operationPath := fmt.Sprintf("/issues/375") + operationPath := "/issues/375" if operationPath[0] == '/' { operationPath = "." + operationPath } @@ -597,7 +597,7 @@ func NewIssue41Request(server string, n1param N5StartsWithNumber) (*http.Request return nil, err } - operationPath := fmt.Sprintf("/issues/41/%s", pathParam0) + operationPath := "/issues/41/" + pathParam0 if operationPath[0] == '/' { operationPath = "." + operationPath } @@ -635,7 +635,7 @@ func NewIssue9RequestWithBody(server string, params *Issue9Params, contentType s return nil, err } - operationPath := fmt.Sprintf("/issues/9") + operationPath := "/issues/9" if operationPath[0] == '/' { operationPath = "." + operationPath } @@ -682,7 +682,7 @@ func NewIssue975Request(server string) (*http.Request, error) { return nil, err } - operationPath := fmt.Sprintf("/issues/975") + operationPath := "/issues/975" if operationPath[0] == '/' { operationPath = "." + operationPath } diff --git a/internal/test/strict-server/client/client.gen.go b/internal/test/strict-server/client/client.gen.go index 5bf1dffb3f..bb287f5f2a 100644 --- a/internal/test/strict-server/client/client.gen.go +++ b/internal/test/strict-server/client/client.gen.go @@ -7,7 +7,6 @@ import ( "bytes" "context" "encoding/json" - "fmt" "io" "net/http" "net/url" @@ -472,7 +471,7 @@ func NewJSONExampleRequestWithBody(server string, contentType string, body io.Re return nil, err } - operationPath := fmt.Sprintf("/json") + operationPath := "/json" if operationPath[0] == '/' { operationPath = "." + operationPath } @@ -501,7 +500,7 @@ func NewMultipartExampleRequestWithBody(server string, contentType string, body return nil, err } - operationPath := fmt.Sprintf("/multipart") + operationPath := "/multipart" if operationPath[0] == '/' { operationPath = "." + operationPath } @@ -530,7 +529,7 @@ func NewMultipartRelatedExampleRequestWithBody(server string, contentType string return nil, err } - operationPath := fmt.Sprintf("/multipart-related") + operationPath := "/multipart-related" if operationPath[0] == '/' { operationPath = "." + operationPath } @@ -588,7 +587,7 @@ func NewMultipleRequestAndResponseTypesRequestWithBody(server string, contentTyp return nil, err } - operationPath := fmt.Sprintf("/multiple") + operationPath := "/multiple" if operationPath[0] == '/' { operationPath = "." + operationPath } @@ -624,7 +623,7 @@ func NewReservedGoKeywordParametersRequest(server string, pType string) (*http.R return nil, err } - operationPath := fmt.Sprintf("/reserved-go-keyword-parameters/%s", pathParam0) + operationPath := "/reserved-go-keyword-parameters/" + pathParam0 if operationPath[0] == '/' { operationPath = "." + operationPath } @@ -662,7 +661,7 @@ func NewReusableResponsesRequestWithBody(server string, contentType string, body return nil, err } - operationPath := fmt.Sprintf("/reusable-responses") + operationPath := "/reusable-responses" if operationPath[0] == '/' { operationPath = "." + operationPath } @@ -698,7 +697,7 @@ func NewTextExampleRequestWithBody(server string, contentType string, body io.Re return nil, err } - operationPath := fmt.Sprintf("/text") + operationPath := "/text" if operationPath[0] == '/' { operationPath = "." + operationPath } @@ -727,7 +726,7 @@ func NewUnknownExampleRequestWithBody(server string, contentType string, body io return nil, err } - operationPath := fmt.Sprintf("/unknown") + operationPath := "/unknown" if operationPath[0] == '/' { operationPath = "." + operationPath } @@ -756,7 +755,7 @@ func NewUnspecifiedContentTypeRequestWithBody(server string, contentType string, return nil, err } - operationPath := fmt.Sprintf("/unspecified-content-type") + operationPath := "/unspecified-content-type" if operationPath[0] == '/' { operationPath = "." + operationPath } @@ -796,7 +795,7 @@ func NewURLEncodedExampleRequestWithBody(server string, contentType string, body return nil, err } - operationPath := fmt.Sprintf("/urlencoded") + operationPath := "/urlencoded" if operationPath[0] == '/' { operationPath = "." + operationPath } @@ -836,7 +835,7 @@ func NewHeadersExampleRequestWithBody(server string, params *HeadersExampleParam return nil, err } - operationPath := fmt.Sprintf("/with-headers") + operationPath := "/with-headers" if operationPath[0] == '/' { operationPath = "." + operationPath } @@ -900,7 +899,7 @@ func NewUnionExampleRequestWithBody(server string, contentType string, body io.R return nil, err } - operationPath := fmt.Sprintf("/with-union") + operationPath := "/with-union" if operationPath[0] == '/' { operationPath = "." + operationPath } diff --git a/pkg/codegen/template_helpers.go b/pkg/codegen/template_helpers.go index 49ee3aba64..1f0f9bf27f 100644 --- a/pkg/codegen/template_helpers.go +++ b/pkg/codegen/template_helpers.go @@ -322,7 +322,7 @@ var TemplateFunctions = template.FuncMap{ "genParamArgs": genParamArgs, "genParamTypes": genParamTypes, "genParamNames": genParamNames, - "genParamFmtString": ReplacePathParamsWithStr, + "getPathString": GenPathString, "swaggerUriToIrisUri": SwaggerUriToIrisUri, "swaggerUriToEchoUri": SwaggerUriToEchoUri, "swaggerUriToFiberUri": SwaggerUriToFiberUri, diff --git a/pkg/codegen/templates/client.tmpl b/pkg/codegen/templates/client.tmpl index 822e11097a..ebcadb0c84 100644 --- a/pkg/codegen/templates/client.tmpl +++ b/pkg/codegen/templates/client.tmpl @@ -183,7 +183,7 @@ func New{{$opid}}Request{{if .HasBody}}WithBody{{end}}(server string{{genParamAr return nil, err } - operationPath := fmt.Sprintf("{{genParamFmtString .Path}}"{{range $paramIdx, $param := .PathParams}}, pathParam{{$paramIdx}}{{end}}) + operationPath := {{getPathString .Path "pathParam"}} if operationPath[0] == '/' { operationPath = "." + operationPath } diff --git a/pkg/codegen/utils.go b/pkg/codegen/utils.go index 5326e672bf..03505c2039 100644 --- a/pkg/codegen/utils.go +++ b/pkg/codegen/utils.go @@ -648,9 +648,30 @@ func OrderedParamsFromUri(uri string) []string { return result } -// ReplacePathParamsWithStr replaces path parameters of the form {param} with %s -func ReplacePathParamsWithStr(uri string) string { - return pathParamRE.ReplaceAllString(uri, "%s") +// GenPathString constructs a string that embeds path-parameter variables into a URI template. +func GenPathString(uri, paramVarName string) string { + tmpl := pathParamRE.ReplaceAllString(uri, "%s") + + nParams := strings.Count(tmpl, "%s") + params := make([]any, nParams) + + for i := range nParams { + params[i] = fmt.Sprintf(`" + %s%d + "`, paramVarName, i) + } + + uri = fmt.Sprintf(tmpl, params...) + + uri, found := strings.CutPrefix(uri, `" + `) + if !found { + uri = `"` + uri + } + + uri, found = strings.CutSuffix(uri, ` + "`) + if !found { + uri = uri + `"` + } + + return uri } // SortParamsByPath reorders the given parameter definitions to match those in the path URI. diff --git a/pkg/codegen/utils_test.go b/pkg/codegen/utils_test.go index af71438473..4170c8d2c1 100644 --- a/pkg/codegen/utils_test.go +++ b/pkg/codegen/utils_test.go @@ -464,9 +464,53 @@ func TestOrderedParamsFromUri(t *testing.T) { assert.EqualValues(t, []string{}, result) } -func TestReplacePathParamsWithStr(t *testing.T) { - result := ReplacePathParamsWithStr("/path/{param1}/{.param2}/{;param3*}/foo") - assert.EqualValues(t, "/path/%s/%s/%s/foo", result) +func TestGenPathString(t *testing.T) { + tests := []struct { + uri string + want string + }{ + { + uri: "/test/{param1}/{.param2}/{;param3*}/foo", + want: `"/test/" + pathParam0 + "/" + pathParam1 + "/" + pathParam2 + "/foo"`, + }, + { + uri: "/test/{param}", + want: `"/test/" + pathParam0`, + }, + { + uri: "/test/{param}/test2/{param2}", + want: `"/test/" + pathParam0 + "/test2/" + pathParam1`, + }, + { + uri: "/test/test2", + want: `"/test/test2"`, + }, + { + uri: "test", + want: `"test"`, + }, + { + uri: "{param}/test", + want: `pathParam0 + "/test"`, + }, + { + uri: "/{param}/test/", + want: `"/" + pathParam0 + "/test/"`, + }, + { + uri: "/", + want: `"/"`, + }, + { + uri: "", + want: `""`, + }, + } + + for _, tst := range tests { + result := GenPathString(tst.uri, "pathParam") + assert.Equal(t, tst.want, result) + } } func TestStringToGoComment(t *testing.T) { From b690045fbe2124ef95bb6a8c8ab048925b262171 Mon Sep 17 00:00:00 2001 From: Sterligov Denis Date: Sat, 2 Aug 2025 16:52:52 +0500 Subject: [PATCH 2/2] add benchmark code for concat path --- pkg/codegen/utils_test.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/pkg/codegen/utils_test.go b/pkg/codegen/utils_test.go index 4170c8d2c1..97dd33d6fe 100644 --- a/pkg/codegen/utils_test.go +++ b/pkg/codegen/utils_test.go @@ -513,6 +513,29 @@ func TestGenPathString(t *testing.T) { } } +func Benchmark_concatPath1param(b *testing.B) { + b.ReportAllocs() + b.ResetTimer() + + pathParam0 := "pathParam0" + + for i := 0; i < b.N; i++ { + _ = "/test/" + pathParam0 + } +} + +func Benchmark_concatPath2param(b *testing.B) { + b.ReportAllocs() + b.ResetTimer() + + pathParam0 := "pathParam0" + pathParam1 := "pathParam1" + + for i := 0; i < b.N; i++ { + _ = "/test/" + pathParam0 + "/test2/" + pathParam1 + } +} + func TestStringToGoComment(t *testing.T) { testCases := []struct { input string