From dcf472a0348f4cf3d082576f7e8d76f1c92c630d Mon Sep 17 00:00:00 2001 From: Dragos Vingarzan Date: Wed, 15 Jan 2025 09:36:33 +0100 Subject: [PATCH] ISSUE: #17373 [BUG] [Go-server] invalid composite literal type string (#20467) * modules/openapi-generator/src/main/resources/go-server: alternative initialization of variable, to avoid extra checks and custom code * propagated changes to samples --- .../src/main/resources/go-server/controller-api.mustache | 2 +- .../openapi3/server/petstore/go/go-petstore/go/api_pet.go | 4 ++-- .../server/petstore/go/go-petstore/go/api_store.go | 2 +- .../server/petstore/go/go-petstore/go/api_user.go | 8 ++++---- .../others/go-server/no-body-path-params/go/api_body.go | 2 +- .../others/go-server/no-body-path-params/go/api_both.go | 2 +- samples/server/petstore/go-api-server/go/api_pet.go | 4 ++-- samples/server/petstore/go-api-server/go/api_store.go | 2 +- samples/server/petstore/go-api-server/go/api_user.go | 8 ++++---- samples/server/petstore/go-chi-server/go/api_pet.go | 4 ++-- samples/server/petstore/go-chi-server/go/api_store.go | 2 +- samples/server/petstore/go-chi-server/go/api_user.go | 8 ++++---- 12 files changed, 24 insertions(+), 24 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/go-server/controller-api.mustache b/modules/openapi-generator/src/main/resources/go-server/controller-api.mustache index 5dc6fd9aebd1..f1067422e244 100644 --- a/modules/openapi-generator/src/main/resources/go-server/controller-api.mustache +++ b/modules/openapi-generator/src/main/resources/go-server/controller-api.mustache @@ -591,7 +591,7 @@ func (c *{{classname}}Controller) {{nickname}}(w http.ResponseWriter, r *http.Re {{paramName}}Param := r.Header.Get("{{baseName}}") {{/isHeaderParam}} {{#isBodyParam}} - {{paramName}}Param := {{dataType}}{} + var {{paramName}}Param {{dataType}} d := json.NewDecoder(r.Body) {{^isAdditionalPropertiesTrue}} d.DisallowUnknownFields() diff --git a/samples/openapi3/server/petstore/go/go-petstore/go/api_pet.go b/samples/openapi3/server/petstore/go/go-petstore/go/api_pet.go index 25d09e53378b..7d274059ab24 100644 --- a/samples/openapi3/server/petstore/go/go-petstore/go/api_pet.go +++ b/samples/openapi3/server/petstore/go/go-petstore/go/api_pet.go @@ -97,7 +97,7 @@ func (c *PetAPIController) Routes() Routes { // UpdatePet - Update an existing pet func (c *PetAPIController) UpdatePet(w http.ResponseWriter, r *http.Request) { - petParam := Pet{} + var petParam Pet d := json.NewDecoder(r.Body) d.DisallowUnknownFields() if err := d.Decode(&petParam); err != nil { @@ -124,7 +124,7 @@ func (c *PetAPIController) UpdatePet(w http.ResponseWriter, r *http.Request) { // AddPet - Add a new pet to the store func (c *PetAPIController) AddPet(w http.ResponseWriter, r *http.Request) { - petParam := Pet{} + var petParam Pet d := json.NewDecoder(r.Body) d.DisallowUnknownFields() if err := d.Decode(&petParam); err != nil { diff --git a/samples/openapi3/server/petstore/go/go-petstore/go/api_store.go b/samples/openapi3/server/petstore/go/go-petstore/go/api_store.go index be4a6289e97a..6105320bb0a1 100644 --- a/samples/openapi3/server/petstore/go/go-petstore/go/api_store.go +++ b/samples/openapi3/server/petstore/go/go-petstore/go/api_store.go @@ -88,7 +88,7 @@ func (c *StoreAPIController) GetInventory(w http.ResponseWriter, r *http.Request // PlaceOrder - Place an order for a pet func (c *StoreAPIController) PlaceOrder(w http.ResponseWriter, r *http.Request) { - orderParam := Order{} + var orderParam Order d := json.NewDecoder(r.Body) d.DisallowUnknownFields() if err := d.Decode(&orderParam); err != nil { diff --git a/samples/openapi3/server/petstore/go/go-petstore/go/api_user.go b/samples/openapi3/server/petstore/go/go-petstore/go/api_user.go index 37cebbe7117d..12eb9d263710 100644 --- a/samples/openapi3/server/petstore/go/go-petstore/go/api_user.go +++ b/samples/openapi3/server/petstore/go/go-petstore/go/api_user.go @@ -96,7 +96,7 @@ func (c *UserAPIController) Routes() Routes { // CreateUser - Create user func (c *UserAPIController) CreateUser(w http.ResponseWriter, r *http.Request) { - userParam := User{} + var userParam User d := json.NewDecoder(r.Body) d.DisallowUnknownFields() if err := d.Decode(&userParam); err != nil { @@ -123,7 +123,7 @@ func (c *UserAPIController) CreateUser(w http.ResponseWriter, r *http.Request) { // CreateUsersWithArrayInput - Creates list of users with given input array func (c *UserAPIController) CreateUsersWithArrayInput(w http.ResponseWriter, r *http.Request) { - userParam := []User{} + var userParam []User d := json.NewDecoder(r.Body) d.DisallowUnknownFields() if err := d.Decode(&userParam); err != nil { @@ -148,7 +148,7 @@ func (c *UserAPIController) CreateUsersWithArrayInput(w http.ResponseWriter, r * // CreateUsersWithListInput - Creates list of users with given input array func (c *UserAPIController) CreateUsersWithListInput(w http.ResponseWriter, r *http.Request) { - userParam := []User{} + var userParam []User d := json.NewDecoder(r.Body) d.DisallowUnknownFields() if err := d.Decode(&userParam); err != nil { @@ -312,7 +312,7 @@ func (c *UserAPIController) UpdateUser(w http.ResponseWriter, r *http.Request) { c.errorHandler(w, r, &RequiredError{"username"}, nil) return } - userParam := User{} + var userParam User d := json.NewDecoder(r.Body) d.DisallowUnknownFields() if err := d.Decode(&userParam); err != nil { diff --git a/samples/server/others/go-server/no-body-path-params/go/api_body.go b/samples/server/others/go-server/no-body-path-params/go/api_body.go index 02b5ad81f38c..af1630622d33 100644 --- a/samples/server/others/go-server/no-body-path-params/go/api_body.go +++ b/samples/server/others/go-server/no-body-path-params/go/api_body.go @@ -59,7 +59,7 @@ func (c *BodyAPIController) Routes() Routes { // Body - summary func (c *BodyAPIController) Body(w http.ResponseWriter, r *http.Request) { - bodyRequestParam := BodyRequest{} + var bodyRequestParam BodyRequest d := json.NewDecoder(r.Body) d.DisallowUnknownFields() if err := d.Decode(&bodyRequestParam); err != nil { diff --git a/samples/server/others/go-server/no-body-path-params/go/api_both.go b/samples/server/others/go-server/no-body-path-params/go/api_both.go index c100791a92d9..45c1c08e3191 100644 --- a/samples/server/others/go-server/no-body-path-params/go/api_both.go +++ b/samples/server/others/go-server/no-body-path-params/go/api_both.go @@ -67,7 +67,7 @@ func (c *BothAPIController) Both(w http.ResponseWriter, r *http.Request) { c.errorHandler(w, r, &RequiredError{"pathParam"}, nil) return } - bodyRequestParam := BodyRequest{} + var bodyRequestParam BodyRequest d := json.NewDecoder(r.Body) d.DisallowUnknownFields() if err := d.Decode(&bodyRequestParam); err != nil { diff --git a/samples/server/petstore/go-api-server/go/api_pet.go b/samples/server/petstore/go-api-server/go/api_pet.go index 68f4fd031de0..a2fa6dcc2228 100644 --- a/samples/server/petstore/go-api-server/go/api_pet.go +++ b/samples/server/petstore/go-api-server/go/api_pet.go @@ -128,7 +128,7 @@ func (c *PetAPIController) Routes() Routes { // UpdatePet - Update an existing pet func (c *PetAPIController) UpdatePet(w http.ResponseWriter, r *http.Request) { - petParam := Pet{} + var petParam Pet d := json.NewDecoder(r.Body) d.DisallowUnknownFields() if err := d.Decode(&petParam); err != nil { @@ -155,7 +155,7 @@ func (c *PetAPIController) UpdatePet(w http.ResponseWriter, r *http.Request) { // AddPet - Add a new pet to the store func (c *PetAPIController) AddPet(w http.ResponseWriter, r *http.Request) { - petParam := Pet{} + var petParam Pet d := json.NewDecoder(r.Body) d.DisallowUnknownFields() if err := d.Decode(&petParam); err != nil { diff --git a/samples/server/petstore/go-api-server/go/api_store.go b/samples/server/petstore/go-api-server/go/api_store.go index d8b75d9ec99b..7a68a997db37 100644 --- a/samples/server/petstore/go-api-server/go/api_store.go +++ b/samples/server/petstore/go-api-server/go/api_store.go @@ -88,7 +88,7 @@ func (c *StoreAPIController) GetInventory(w http.ResponseWriter, r *http.Request // PlaceOrder - Place an order for a pet func (c *StoreAPIController) PlaceOrder(w http.ResponseWriter, r *http.Request) { - orderParam := Order{} + var orderParam Order d := json.NewDecoder(r.Body) d.DisallowUnknownFields() if err := d.Decode(&orderParam); err != nil { diff --git a/samples/server/petstore/go-api-server/go/api_user.go b/samples/server/petstore/go-api-server/go/api_user.go index 6486c8b720f9..a11f40421573 100644 --- a/samples/server/petstore/go-api-server/go/api_user.go +++ b/samples/server/petstore/go-api-server/go/api_user.go @@ -96,7 +96,7 @@ func (c *UserAPIController) Routes() Routes { // CreateUser - Create user func (c *UserAPIController) CreateUser(w http.ResponseWriter, r *http.Request) { - userParam := User{} + var userParam User d := json.NewDecoder(r.Body) d.DisallowUnknownFields() if err := d.Decode(&userParam); err != nil { @@ -123,7 +123,7 @@ func (c *UserAPIController) CreateUser(w http.ResponseWriter, r *http.Request) { // CreateUsersWithArrayInput - Creates list of users with given input array func (c *UserAPIController) CreateUsersWithArrayInput(w http.ResponseWriter, r *http.Request) { - userParam := []User{} + var userParam []User d := json.NewDecoder(r.Body) d.DisallowUnknownFields() if err := d.Decode(&userParam); err != nil { @@ -148,7 +148,7 @@ func (c *UserAPIController) CreateUsersWithArrayInput(w http.ResponseWriter, r * // CreateUsersWithListInput - Creates list of users with given input array func (c *UserAPIController) CreateUsersWithListInput(w http.ResponseWriter, r *http.Request) { - userParam := []User{} + var userParam []User d := json.NewDecoder(r.Body) d.DisallowUnknownFields() if err := d.Decode(&userParam); err != nil { @@ -258,7 +258,7 @@ func (c *UserAPIController) UpdateUser(w http.ResponseWriter, r *http.Request) { c.errorHandler(w, r, &RequiredError{"username"}, nil) return } - userParam := User{} + var userParam User d := json.NewDecoder(r.Body) d.DisallowUnknownFields() if err := d.Decode(&userParam); err != nil { diff --git a/samples/server/petstore/go-chi-server/go/api_pet.go b/samples/server/petstore/go-chi-server/go/api_pet.go index c455813ad35d..9dd652958a3e 100644 --- a/samples/server/petstore/go-chi-server/go/api_pet.go +++ b/samples/server/petstore/go-chi-server/go/api_pet.go @@ -128,7 +128,7 @@ func (c *PetAPIController) Routes() Routes { // UpdatePet - Update an existing pet func (c *PetAPIController) UpdatePet(w http.ResponseWriter, r *http.Request) { - petParam := Pet{} + var petParam Pet d := json.NewDecoder(r.Body) d.DisallowUnknownFields() if err := d.Decode(&petParam); err != nil { @@ -155,7 +155,7 @@ func (c *PetAPIController) UpdatePet(w http.ResponseWriter, r *http.Request) { // AddPet - Add a new pet to the store func (c *PetAPIController) AddPet(w http.ResponseWriter, r *http.Request) { - petParam := Pet{} + var petParam Pet d := json.NewDecoder(r.Body) d.DisallowUnknownFields() if err := d.Decode(&petParam); err != nil { diff --git a/samples/server/petstore/go-chi-server/go/api_store.go b/samples/server/petstore/go-chi-server/go/api_store.go index be4a6289e97a..6105320bb0a1 100644 --- a/samples/server/petstore/go-chi-server/go/api_store.go +++ b/samples/server/petstore/go-chi-server/go/api_store.go @@ -88,7 +88,7 @@ func (c *StoreAPIController) GetInventory(w http.ResponseWriter, r *http.Request // PlaceOrder - Place an order for a pet func (c *StoreAPIController) PlaceOrder(w http.ResponseWriter, r *http.Request) { - orderParam := Order{} + var orderParam Order d := json.NewDecoder(r.Body) d.DisallowUnknownFields() if err := d.Decode(&orderParam); err != nil { diff --git a/samples/server/petstore/go-chi-server/go/api_user.go b/samples/server/petstore/go-chi-server/go/api_user.go index c1ff526edc79..a80c9cb20637 100644 --- a/samples/server/petstore/go-chi-server/go/api_user.go +++ b/samples/server/petstore/go-chi-server/go/api_user.go @@ -96,7 +96,7 @@ func (c *UserAPIController) Routes() Routes { // CreateUser - Create user func (c *UserAPIController) CreateUser(w http.ResponseWriter, r *http.Request) { - userParam := User{} + var userParam User d := json.NewDecoder(r.Body) d.DisallowUnknownFields() if err := d.Decode(&userParam); err != nil { @@ -123,7 +123,7 @@ func (c *UserAPIController) CreateUser(w http.ResponseWriter, r *http.Request) { // CreateUsersWithArrayInput - Creates list of users with given input array func (c *UserAPIController) CreateUsersWithArrayInput(w http.ResponseWriter, r *http.Request) { - userParam := []User{} + var userParam []User d := json.NewDecoder(r.Body) d.DisallowUnknownFields() if err := d.Decode(&userParam); err != nil { @@ -148,7 +148,7 @@ func (c *UserAPIController) CreateUsersWithArrayInput(w http.ResponseWriter, r * // CreateUsersWithListInput - Creates list of users with given input array func (c *UserAPIController) CreateUsersWithListInput(w http.ResponseWriter, r *http.Request) { - userParam := []User{} + var userParam []User d := json.NewDecoder(r.Body) d.DisallowUnknownFields() if err := d.Decode(&userParam); err != nil { @@ -256,7 +256,7 @@ func (c *UserAPIController) UpdateUser(w http.ResponseWriter, r *http.Request) { c.errorHandler(w, r, &RequiredError{"username"}, nil) return } - userParam := User{} + var userParam User d := json.NewDecoder(r.Body) d.DisallowUnknownFields() if err := d.Decode(&userParam); err != nil {