From 10480ad8e0e330fe23a68f83547bd25dcea806fa Mon Sep 17 00:00:00 2001 From: Romain LE DISEZ Date: Fri, 26 Apr 2024 18:03:38 +0200 Subject: [PATCH] [go-server] Set default values in object properties When a default value is set for an object property, ensure it is set into the struct before decoding the JSON body. Fix OpenAPITools#4579 --- .../go-server/controller-api.mustache | 5 +++++ .../main/resources/go-server/model.mustache | 22 ++++++++++++++++++- .../petstore/go/go-petstore/go/api_pet.go | 4 ++-- .../petstore/go/go-petstore/go/api_store.go | 2 +- .../petstore/go/go-petstore/go/api_user.go | 4 ++-- .../go/go-petstore/go/model_an_object.go | 9 ++++++++ .../go/go-petstore/go/model_api_response.go | 8 +++++++ .../go/go-petstore/go/model_category.go | 8 +++++++ .../petstore/go/go-petstore/go/model_order.go | 9 ++++++++ .../go/go-petstore/go/model_order_info.go | 8 +++++++ .../petstore/go/go-petstore/go/model_pet.go | 9 ++++++++ .../go/go-petstore/go/model_special_info.go | 8 +++++++ .../petstore/go/go-petstore/go/model_tag.go | 8 +++++++ .../petstore/go/go-petstore/go/model_user.go | 8 +++++++ .../no-body-path-params/go/api_body.go | 2 +- .../no-body-path-params/go/api_both.go | 2 +- .../go/model_body_request.go | 8 +++++++ .../petstore/go-api-server/go/api_pet.go | 4 ++-- .../petstore/go-api-server/go/api_store.go | 2 +- .../petstore/go-api-server/go/api_user.go | 4 ++-- .../go-api-server/go/model_an_object.go | 9 ++++++++ .../go-api-server/go/model_api_response.go | 8 +++++++ .../go-api-server/go/model_category.go | 8 +++++++ .../petstore/go-api-server/go/model_colour.go | 1 - .../petstore/go-api-server/go/model_gender.go | 1 - .../petstore/go-api-server/go/model_order.go | 9 ++++++++ .../go-api-server/go/model_order_info.go | 8 +++++++ .../petstore/go-api-server/go/model_pet.go | 9 ++++++++ .../go-api-server/go/model_special_info.go | 8 +++++++ .../go-api-server/go/model_species.go | 1 - .../petstore/go-api-server/go/model_tag.go | 8 +++++++ .../petstore/go-api-server/go/model_user.go | 8 +++++++ .../petstore/go-chi-server/go/api_pet.go | 4 ++-- .../petstore/go-chi-server/go/api_store.go | 2 +- .../petstore/go-chi-server/go/api_user.go | 4 ++-- .../go-chi-server/go/model_an_object.go | 9 ++++++++ .../go-chi-server/go/model_api_response.go | 8 +++++++ .../go-chi-server/go/model_category.go | 8 +++++++ .../petstore/go-chi-server/go/model_colour.go | 1 - .../petstore/go-chi-server/go/model_gender.go | 1 - .../petstore/go-chi-server/go/model_order.go | 9 ++++++++ .../go-chi-server/go/model_order_info.go | 8 +++++++ .../petstore/go-chi-server/go/model_pet.go | 9 ++++++++ .../go-chi-server/go/model_special_info.go | 8 +++++++ .../go-chi-server/go/model_species.go | 1 - .../petstore/go-chi-server/go/model_tag.go | 8 +++++++ .../petstore/go-chi-server/go/model_user.go | 8 +++++++ 47 files changed, 276 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..bbe2f0ffb32d 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,12 @@ func (c *{{classname}}Controller) {{nickname}}(w http.ResponseWriter, r *http.Re {{paramName}}Param := r.Header.Get("{{baseName}}") {{/isHeaderParam}} {{#isBodyParam}} + {{#isArray}} {{paramName}}Param := {{dataType}}{} + {{/isArray}} + {{^isArray}} + {{paramName}}Param := New{{dataType}}WithDefaults() + {{/isArray}} d := json.NewDecoder(r.Body) {{^isAdditionalPropertiesTrue}} d.DisallowUnknownFields() diff --git a/modules/openapi-generator/src/main/resources/go-server/model.mustache b/modules/openapi-generator/src/main/resources/go-server/model.mustache index 6ef715feb3a2..82cc06638dd2 100644 --- a/modules/openapi-generator/src/main/resources/go-server/model.mustache +++ b/modules/openapi-generator/src/main/resources/go-server/model.mustache @@ -74,7 +74,27 @@ type {{classname}} struct { {{/deprecated}} {{name}} {{#isNullable}}*{{/isNullable}}{{{dataType}}} `json:"{{baseName}}{{^required}},omitempty{{/required}}"{{#vendorExtensions.x-go-custom-tag}} {{{.}}}{{/vendorExtensions.x-go-custom-tag}}` {{/vars}} -}{{/isEnum}} +} + +{{^isArray}} +// New{{classname}}WithDefaults instantiates a new {{classname}} object +// This constructor will only assign default values to properties that have it defined, +// but it doesn't guarantee that properties required by API are set +func New{{classname}}WithDefaults() {{classname}} { + this := {{classname}}{} +{{#vars}} +{{#defaultValue}} +{{^isArray}} + this.{{name}} = {{#isBoolean}}{{{.}}}{{/isBoolean}}{{#isNumeric}}{{{.}}}{{/isNumeric}}{{^isBoolean}}{{^isNumeric}}"{{{.}}}"{{/isNumeric}}{{/isBoolean}} +{{/isArray}} +{{/defaultValue}} +{{#isModel}} + {{#isNullable}}*{{/isNullable}}this.{{name}} = New{{dataType}}WithDefaults() +{{/isModel}} +{{/vars}} + return this +}{{/isArray}} +{{/isEnum}} // Assert{{classname}}Required checks if the required fields are not zero-ed func Assert{{classname}}Required(obj {{classname}}) error { 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 5b043ad51bcd..6e0f98d1b182 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 { // AddPet - Add a new pet to the store func (c *PetAPIController) AddPet(w http.ResponseWriter, r *http.Request) { - petParam := Pet{} + petParam := NewPetWithDefaults() d := json.NewDecoder(r.Body) d.DisallowUnknownFields() if err := d.Decode(&petParam); err != nil { @@ -208,7 +208,7 @@ func (c *PetAPIController) GetPetById(w http.ResponseWriter, r *http.Request) { // UpdatePet - Update an existing pet func (c *PetAPIController) UpdatePet(w http.ResponseWriter, r *http.Request) { - petParam := Pet{} + petParam := NewPetWithDefaults() 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 e364efe39889..40e7f7f657bb 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 @@ -127,7 +127,7 @@ func (c *StoreAPIController) GetOrderById(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{} + orderParam := NewOrderWithDefaults() 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 1d28ab73b753..f9a1df672957 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{} + userParam := NewUserWithDefaults() d := json.NewDecoder(r.Body) d.DisallowUnknownFields() if err := d.Decode(&userParam); err != nil { @@ -348,7 +348,7 @@ func (c *UserAPIController) UpdateUser(w http.ResponseWriter, r *http.Request) { c.errorHandler(w, r, &RequiredError{"username"}, nil) return } - userParam := User{} + userParam := NewUserWithDefaults() d := json.NewDecoder(r.Body) d.DisallowUnknownFields() if err := d.Decode(&userParam); err != nil { diff --git a/samples/openapi3/server/petstore/go/go-petstore/go/model_an_object.go b/samples/openapi3/server/petstore/go/go-petstore/go/model_an_object.go index e5bdcff24953..9c4693922567 100644 --- a/samples/openapi3/server/petstore/go/go-petstore/go/model_an_object.go +++ b/samples/openapi3/server/petstore/go/go-petstore/go/model_an_object.go @@ -22,6 +22,15 @@ type AnObject struct { Pet []Pet `json:"Pet,omitempty"` } +// NewAnObjectWithDefaults instantiates a new AnObject object +// This constructor will only assign default values to properties that have it defined, +// but it doesn't guarantee that properties required by API are set +func NewAnObjectWithDefaults() AnObject { + this := AnObject{} + this.Tag = NewTagWithDefaults() + return this +} + // AssertAnObjectRequired checks if the required fields are not zero-ed func AssertAnObjectRequired(obj AnObject) error { if err := AssertTagRequired(obj.Tag); err != nil { diff --git a/samples/openapi3/server/petstore/go/go-petstore/go/model_api_response.go b/samples/openapi3/server/petstore/go/go-petstore/go/model_api_response.go index c6bbed4687a3..4a3d782f8e1c 100644 --- a/samples/openapi3/server/petstore/go/go-petstore/go/model_api_response.go +++ b/samples/openapi3/server/petstore/go/go-petstore/go/model_api_response.go @@ -23,6 +23,14 @@ type ApiResponse struct { Message string `json:"message,omitempty"` } +// NewApiResponseWithDefaults instantiates a new ApiResponse object +// This constructor will only assign default values to properties that have it defined, +// but it doesn't guarantee that properties required by API are set +func NewApiResponseWithDefaults() ApiResponse { + this := ApiResponse{} + return this +} + // AssertApiResponseRequired checks if the required fields are not zero-ed func AssertApiResponseRequired(obj ApiResponse) error { return nil diff --git a/samples/openapi3/server/petstore/go/go-petstore/go/model_category.go b/samples/openapi3/server/petstore/go/go-petstore/go/model_category.go index fe474de1c47e..94d1b66329f0 100644 --- a/samples/openapi3/server/petstore/go/go-petstore/go/model_category.go +++ b/samples/openapi3/server/petstore/go/go-petstore/go/model_category.go @@ -21,6 +21,14 @@ type Category struct { Name string `json:"name,omitempty"` } +// NewCategoryWithDefaults instantiates a new Category object +// This constructor will only assign default values to properties that have it defined, +// but it doesn't guarantee that properties required by API are set +func NewCategoryWithDefaults() Category { + this := Category{} + return this +} + // AssertCategoryRequired checks if the required fields are not zero-ed func AssertCategoryRequired(obj Category) error { return nil diff --git a/samples/openapi3/server/petstore/go/go-petstore/go/model_order.go b/samples/openapi3/server/petstore/go/go-petstore/go/model_order.go index 6680fe1d1927..994b6a126f02 100644 --- a/samples/openapi3/server/petstore/go/go-petstore/go/model_order.go +++ b/samples/openapi3/server/petstore/go/go-petstore/go/model_order.go @@ -37,6 +37,15 @@ type Order struct { Comment *string `json:"comment"` } +// NewOrderWithDefaults instantiates a new Order object +// This constructor will only assign default values to properties that have it defined, +// but it doesn't guarantee that properties required by API are set +func NewOrderWithDefaults() Order { + this := Order{} + this.Complete = false + return this +} + // AssertOrderRequired checks if the required fields are not zero-ed func AssertOrderRequired(obj Order) error { elements := map[string]interface{}{ diff --git a/samples/openapi3/server/petstore/go/go-petstore/go/model_order_info.go b/samples/openapi3/server/petstore/go/go-petstore/go/model_order_info.go index 21f5d24946c2..fd9897fe6fb9 100644 --- a/samples/openapi3/server/petstore/go/go-petstore/go/model_order_info.go +++ b/samples/openapi3/server/petstore/go/go-petstore/go/model_order_info.go @@ -27,6 +27,14 @@ type OrderInfo struct { ShipDate time.Time `json:"shipDate,omitempty"` } +// NewOrderInfoWithDefaults instantiates a new OrderInfo object +// This constructor will only assign default values to properties that have it defined, +// but it doesn't guarantee that properties required by API are set +func NewOrderInfoWithDefaults() OrderInfo { + this := OrderInfo{} + return this +} + // AssertOrderInfoRequired checks if the required fields are not zero-ed func AssertOrderInfoRequired(obj OrderInfo) error { return nil diff --git a/samples/openapi3/server/petstore/go/go-petstore/go/model_pet.go b/samples/openapi3/server/petstore/go/go-petstore/go/model_pet.go index 48537769fb05..b76ac19bc0b1 100644 --- a/samples/openapi3/server/petstore/go/go-petstore/go/model_pet.go +++ b/samples/openapi3/server/petstore/go/go-petstore/go/model_pet.go @@ -31,6 +31,15 @@ type Pet struct { Status string `json:"status,omitempty"` } +// NewPetWithDefaults instantiates a new Pet object +// This constructor will only assign default values to properties that have it defined, +// but it doesn't guarantee that properties required by API are set +func NewPetWithDefaults() Pet { + this := Pet{} + *this.Category = NewCategoryWithDefaults() + return this +} + // AssertPetRequired checks if the required fields are not zero-ed func AssertPetRequired(obj Pet) error { elements := map[string]interface{}{ diff --git a/samples/openapi3/server/petstore/go/go-petstore/go/model_special_info.go b/samples/openapi3/server/petstore/go/go-petstore/go/model_special_info.go index c09677118547..073b4c469b36 100644 --- a/samples/openapi3/server/petstore/go/go-petstore/go/model_special_info.go +++ b/samples/openapi3/server/petstore/go/go-petstore/go/model_special_info.go @@ -23,6 +23,14 @@ type SpecialInfo struct { Type string `json:"type,omitempty"` } +// NewSpecialInfoWithDefaults instantiates a new SpecialInfo object +// This constructor will only assign default values to properties that have it defined, +// but it doesn't guarantee that properties required by API are set +func NewSpecialInfoWithDefaults() SpecialInfo { + this := SpecialInfo{} + return this +} + // AssertSpecialInfoRequired checks if the required fields are not zero-ed func AssertSpecialInfoRequired(obj SpecialInfo) error { elements := map[string]interface{}{ diff --git a/samples/openapi3/server/petstore/go/go-petstore/go/model_tag.go b/samples/openapi3/server/petstore/go/go-petstore/go/model_tag.go index 88fb735f34cc..e06cba80f6bd 100644 --- a/samples/openapi3/server/petstore/go/go-petstore/go/model_tag.go +++ b/samples/openapi3/server/petstore/go/go-petstore/go/model_tag.go @@ -21,6 +21,14 @@ type Tag struct { Name string `json:"name,omitempty"` } +// NewTagWithDefaults instantiates a new Tag object +// This constructor will only assign default values to properties that have it defined, +// but it doesn't guarantee that properties required by API are set +func NewTagWithDefaults() Tag { + this := Tag{} + return this +} + // AssertTagRequired checks if the required fields are not zero-ed func AssertTagRequired(obj Tag) error { return nil diff --git a/samples/openapi3/server/petstore/go/go-petstore/go/model_user.go b/samples/openapi3/server/petstore/go/go-petstore/go/model_user.go index 973b8986d38e..17c382eadbdd 100644 --- a/samples/openapi3/server/petstore/go/go-petstore/go/model_user.go +++ b/samples/openapi3/server/petstore/go/go-petstore/go/model_user.go @@ -40,6 +40,14 @@ type User struct { DeepSliceMap [][]AnObject `json:"deepSliceMap,omitempty"` } +// NewUserWithDefaults instantiates a new User object +// This constructor will only assign default values to properties that have it defined, +// but it doesn't guarantee that properties required by API are set +func NewUserWithDefaults() User { + this := User{} + return this +} + // AssertUserRequired checks if the required fields are not zero-ed func AssertUserRequired(obj User) error { elements := map[string]interface{}{ 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..d17c25828efc 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{} + bodyRequestParam := NewBodyRequestWithDefaults() 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..4e7e0b88a3e2 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{} + bodyRequestParam := NewBodyRequestWithDefaults() 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/model_body_request.go b/samples/server/others/go-server/no-body-path-params/go/model_body_request.go index 92b220c01b2f..d7540fa6f475 100644 --- a/samples/server/others/go-server/no-body-path-params/go/model_body_request.go +++ b/samples/server/others/go-server/no-body-path-params/go/model_body_request.go @@ -18,6 +18,14 @@ type BodyRequest struct { Param string `json:"param,omitempty"` } +// NewBodyRequestWithDefaults instantiates a new BodyRequest object +// This constructor will only assign default values to properties that have it defined, +// but it doesn't guarantee that properties required by API are set +func NewBodyRequestWithDefaults() BodyRequest { + this := BodyRequest{} + return this +} + // AssertBodyRequestRequired checks if the required fields are not zero-ed func AssertBodyRequestRequired(obj BodyRequest) error { return 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 8451d4cf8922..df14d0939f65 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 { // AddPet - Add a new pet to the store func (c *PetAPIController) AddPet(w http.ResponseWriter, r *http.Request) { - petParam := Pet{} + petParam := NewPetWithDefaults() d := json.NewDecoder(r.Body) d.DisallowUnknownFields() if err := d.Decode(&petParam); err != nil { @@ -543,7 +543,7 @@ func (c *PetAPIController) SearchPet(w http.ResponseWriter, r *http.Request) { // UpdatePet - Update an existing pet func (c *PetAPIController) UpdatePet(w http.ResponseWriter, r *http.Request) { - petParam := Pet{} + petParam := NewPetWithDefaults() 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 cc1caead0e8d..45470438b811 100644 --- a/samples/server/petstore/go-api-server/go/api_store.go +++ b/samples/server/petstore/go-api-server/go/api_store.go @@ -129,7 +129,7 @@ func (c *StoreAPIController) GetOrderById(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{} + orderParam := NewOrderWithDefaults() 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 f1917fa9b785..1015d981a979 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{} + userParam := NewUserWithDefaults() d := json.NewDecoder(r.Body) d.DisallowUnknownFields() if err := d.Decode(&userParam); err != nil { @@ -295,7 +295,7 @@ func (c *UserAPIController) UpdateUser(w http.ResponseWriter, r *http.Request) { c.errorHandler(w, r, &RequiredError{"username"}, nil) return } - userParam := User{} + userParam := NewUserWithDefaults() d := json.NewDecoder(r.Body) d.DisallowUnknownFields() if err := d.Decode(&userParam); err != nil { diff --git a/samples/server/petstore/go-api-server/go/model_an_object.go b/samples/server/petstore/go-api-server/go/model_an_object.go index e5bdcff24953..9c4693922567 100644 --- a/samples/server/petstore/go-api-server/go/model_an_object.go +++ b/samples/server/petstore/go-api-server/go/model_an_object.go @@ -22,6 +22,15 @@ type AnObject struct { Pet []Pet `json:"Pet,omitempty"` } +// NewAnObjectWithDefaults instantiates a new AnObject object +// This constructor will only assign default values to properties that have it defined, +// but it doesn't guarantee that properties required by API are set +func NewAnObjectWithDefaults() AnObject { + this := AnObject{} + this.Tag = NewTagWithDefaults() + return this +} + // AssertAnObjectRequired checks if the required fields are not zero-ed func AssertAnObjectRequired(obj AnObject) error { if err := AssertTagRequired(obj.Tag); err != nil { diff --git a/samples/server/petstore/go-api-server/go/model_api_response.go b/samples/server/petstore/go-api-server/go/model_api_response.go index c6bbed4687a3..4a3d782f8e1c 100644 --- a/samples/server/petstore/go-api-server/go/model_api_response.go +++ b/samples/server/petstore/go-api-server/go/model_api_response.go @@ -23,6 +23,14 @@ type ApiResponse struct { Message string `json:"message,omitempty"` } +// NewApiResponseWithDefaults instantiates a new ApiResponse object +// This constructor will only assign default values to properties that have it defined, +// but it doesn't guarantee that properties required by API are set +func NewApiResponseWithDefaults() ApiResponse { + this := ApiResponse{} + return this +} + // AssertApiResponseRequired checks if the required fields are not zero-ed func AssertApiResponseRequired(obj ApiResponse) error { return nil diff --git a/samples/server/petstore/go-api-server/go/model_category.go b/samples/server/petstore/go-api-server/go/model_category.go index fe474de1c47e..94d1b66329f0 100644 --- a/samples/server/petstore/go-api-server/go/model_category.go +++ b/samples/server/petstore/go-api-server/go/model_category.go @@ -21,6 +21,14 @@ type Category struct { Name string `json:"name,omitempty"` } +// NewCategoryWithDefaults instantiates a new Category object +// This constructor will only assign default values to properties that have it defined, +// but it doesn't guarantee that properties required by API are set +func NewCategoryWithDefaults() Category { + this := Category{} + return this +} + // AssertCategoryRequired checks if the required fields are not zero-ed func AssertCategoryRequired(obj Category) error { return nil diff --git a/samples/server/petstore/go-api-server/go/model_colour.go b/samples/server/petstore/go-api-server/go/model_colour.go index a77312b65379..26675c94f920 100644 --- a/samples/server/petstore/go-api-server/go/model_colour.go +++ b/samples/server/petstore/go-api-server/go/model_colour.go @@ -55,7 +55,6 @@ func NewColourFromValue(v string) (Colour, error) { } - // AssertColourRequired checks if the required fields are not zero-ed func AssertColourRequired(obj Colour) error { return nil diff --git a/samples/server/petstore/go-api-server/go/model_gender.go b/samples/server/petstore/go-api-server/go/model_gender.go index 410b61fd08d8..dc37635f550c 100644 --- a/samples/server/petstore/go-api-server/go/model_gender.go +++ b/samples/server/petstore/go-api-server/go/model_gender.go @@ -55,7 +55,6 @@ func NewGenderFromValue(v string) (Gender, error) { } - // AssertGenderRequired checks if the required fields are not zero-ed func AssertGenderRequired(obj Gender) error { return nil diff --git a/samples/server/petstore/go-api-server/go/model_order.go b/samples/server/petstore/go-api-server/go/model_order.go index 7e66f8115a7e..5d2700e5bab2 100644 --- a/samples/server/petstore/go-api-server/go/model_order.go +++ b/samples/server/petstore/go-api-server/go/model_order.go @@ -37,6 +37,15 @@ type Order struct { Comment *string `json:"comment"` } +// NewOrderWithDefaults instantiates a new Order object +// This constructor will only assign default values to properties that have it defined, +// but it doesn't guarantee that properties required by API are set +func NewOrderWithDefaults() Order { + this := Order{} + this.Complete = false + return this +} + // AssertOrderRequired checks if the required fields are not zero-ed func AssertOrderRequired(obj Order) error { elements := map[string]interface{}{ diff --git a/samples/server/petstore/go-api-server/go/model_order_info.go b/samples/server/petstore/go-api-server/go/model_order_info.go index 21f5d24946c2..fd9897fe6fb9 100644 --- a/samples/server/petstore/go-api-server/go/model_order_info.go +++ b/samples/server/petstore/go-api-server/go/model_order_info.go @@ -27,6 +27,14 @@ type OrderInfo struct { ShipDate time.Time `json:"shipDate,omitempty"` } +// NewOrderInfoWithDefaults instantiates a new OrderInfo object +// This constructor will only assign default values to properties that have it defined, +// but it doesn't guarantee that properties required by API are set +func NewOrderInfoWithDefaults() OrderInfo { + this := OrderInfo{} + return this +} + // AssertOrderInfoRequired checks if the required fields are not zero-ed func AssertOrderInfoRequired(obj OrderInfo) error { return nil diff --git a/samples/server/petstore/go-api-server/go/model_pet.go b/samples/server/petstore/go-api-server/go/model_pet.go index 48537769fb05..b76ac19bc0b1 100644 --- a/samples/server/petstore/go-api-server/go/model_pet.go +++ b/samples/server/petstore/go-api-server/go/model_pet.go @@ -31,6 +31,15 @@ type Pet struct { Status string `json:"status,omitempty"` } +// NewPetWithDefaults instantiates a new Pet object +// This constructor will only assign default values to properties that have it defined, +// but it doesn't guarantee that properties required by API are set +func NewPetWithDefaults() Pet { + this := Pet{} + *this.Category = NewCategoryWithDefaults() + return this +} + // AssertPetRequired checks if the required fields are not zero-ed func AssertPetRequired(obj Pet) error { elements := map[string]interface{}{ diff --git a/samples/server/petstore/go-api-server/go/model_special_info.go b/samples/server/petstore/go-api-server/go/model_special_info.go index 7be94aa62e33..e9211891340b 100644 --- a/samples/server/petstore/go-api-server/go/model_special_info.go +++ b/samples/server/petstore/go-api-server/go/model_special_info.go @@ -21,6 +21,14 @@ type SpecialInfo struct { Type string `json:"type,omitempty"` } +// NewSpecialInfoWithDefaults instantiates a new SpecialInfo object +// This constructor will only assign default values to properties that have it defined, +// but it doesn't guarantee that properties required by API are set +func NewSpecialInfoWithDefaults() SpecialInfo { + this := SpecialInfo{} + return this +} + // AssertSpecialInfoRequired checks if the required fields are not zero-ed func AssertSpecialInfoRequired(obj SpecialInfo) error { return nil diff --git a/samples/server/petstore/go-api-server/go/model_species.go b/samples/server/petstore/go-api-server/go/model_species.go index f236e29c48a1..d138f34d672e 100644 --- a/samples/server/petstore/go-api-server/go/model_species.go +++ b/samples/server/petstore/go-api-server/go/model_species.go @@ -64,7 +64,6 @@ func NewSpeciesFromValue(v string) (Species, error) { } - // AssertSpeciesRequired checks if the required fields are not zero-ed func AssertSpeciesRequired(obj Species) error { return nil diff --git a/samples/server/petstore/go-api-server/go/model_tag.go b/samples/server/petstore/go-api-server/go/model_tag.go index 88fb735f34cc..e06cba80f6bd 100644 --- a/samples/server/petstore/go-api-server/go/model_tag.go +++ b/samples/server/petstore/go-api-server/go/model_tag.go @@ -21,6 +21,14 @@ type Tag struct { Name string `json:"name,omitempty"` } +// NewTagWithDefaults instantiates a new Tag object +// This constructor will only assign default values to properties that have it defined, +// but it doesn't guarantee that properties required by API are set +func NewTagWithDefaults() Tag { + this := Tag{} + return this +} + // AssertTagRequired checks if the required fields are not zero-ed func AssertTagRequired(obj Tag) error { return nil diff --git a/samples/server/petstore/go-api-server/go/model_user.go b/samples/server/petstore/go-api-server/go/model_user.go index 973b8986d38e..17c382eadbdd 100644 --- a/samples/server/petstore/go-api-server/go/model_user.go +++ b/samples/server/petstore/go-api-server/go/model_user.go @@ -40,6 +40,14 @@ type User struct { DeepSliceMap [][]AnObject `json:"deepSliceMap,omitempty"` } +// NewUserWithDefaults instantiates a new User object +// This constructor will only assign default values to properties that have it defined, +// but it doesn't guarantee that properties required by API are set +func NewUserWithDefaults() User { + this := User{} + return this +} + // AssertUserRequired checks if the required fields are not zero-ed func AssertUserRequired(obj User) error { elements := map[string]interface{}{ 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 a4b5448a0686..d76f1e7e4978 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 { // AddPet - Add a new pet to the store func (c *PetAPIController) AddPet(w http.ResponseWriter, r *http.Request) { - petParam := Pet{} + petParam := NewPetWithDefaults() d := json.NewDecoder(r.Body) d.DisallowUnknownFields() if err := d.Decode(&petParam); err != nil { @@ -537,7 +537,7 @@ func (c *PetAPIController) SearchPet(w http.ResponseWriter, r *http.Request) { // UpdatePet - Update an existing pet func (c *PetAPIController) UpdatePet(w http.ResponseWriter, r *http.Request) { - petParam := Pet{} + petParam := NewPetWithDefaults() 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 e364efe39889..40e7f7f657bb 100644 --- a/samples/server/petstore/go-chi-server/go/api_store.go +++ b/samples/server/petstore/go-chi-server/go/api_store.go @@ -127,7 +127,7 @@ func (c *StoreAPIController) GetOrderById(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{} + orderParam := NewOrderWithDefaults() 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 9fd0c1c5fc75..9a5a9b93bff2 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{} + userParam := NewUserWithDefaults() d := json.NewDecoder(r.Body) d.DisallowUnknownFields() if err := d.Decode(&userParam); err != nil { @@ -292,7 +292,7 @@ func (c *UserAPIController) UpdateUser(w http.ResponseWriter, r *http.Request) { c.errorHandler(w, r, &RequiredError{"username"}, nil) return } - userParam := User{} + userParam := NewUserWithDefaults() d := json.NewDecoder(r.Body) d.DisallowUnknownFields() if err := d.Decode(&userParam); err != nil { diff --git a/samples/server/petstore/go-chi-server/go/model_an_object.go b/samples/server/petstore/go-chi-server/go/model_an_object.go index e5bdcff24953..9c4693922567 100644 --- a/samples/server/petstore/go-chi-server/go/model_an_object.go +++ b/samples/server/petstore/go-chi-server/go/model_an_object.go @@ -22,6 +22,15 @@ type AnObject struct { Pet []Pet `json:"Pet,omitempty"` } +// NewAnObjectWithDefaults instantiates a new AnObject object +// This constructor will only assign default values to properties that have it defined, +// but it doesn't guarantee that properties required by API are set +func NewAnObjectWithDefaults() AnObject { + this := AnObject{} + this.Tag = NewTagWithDefaults() + return this +} + // AssertAnObjectRequired checks if the required fields are not zero-ed func AssertAnObjectRequired(obj AnObject) error { if err := AssertTagRequired(obj.Tag); err != nil { diff --git a/samples/server/petstore/go-chi-server/go/model_api_response.go b/samples/server/petstore/go-chi-server/go/model_api_response.go index c6bbed4687a3..4a3d782f8e1c 100644 --- a/samples/server/petstore/go-chi-server/go/model_api_response.go +++ b/samples/server/petstore/go-chi-server/go/model_api_response.go @@ -23,6 +23,14 @@ type ApiResponse struct { Message string `json:"message,omitempty"` } +// NewApiResponseWithDefaults instantiates a new ApiResponse object +// This constructor will only assign default values to properties that have it defined, +// but it doesn't guarantee that properties required by API are set +func NewApiResponseWithDefaults() ApiResponse { + this := ApiResponse{} + return this +} + // AssertApiResponseRequired checks if the required fields are not zero-ed func AssertApiResponseRequired(obj ApiResponse) error { return nil diff --git a/samples/server/petstore/go-chi-server/go/model_category.go b/samples/server/petstore/go-chi-server/go/model_category.go index fe474de1c47e..94d1b66329f0 100644 --- a/samples/server/petstore/go-chi-server/go/model_category.go +++ b/samples/server/petstore/go-chi-server/go/model_category.go @@ -21,6 +21,14 @@ type Category struct { Name string `json:"name,omitempty"` } +// NewCategoryWithDefaults instantiates a new Category object +// This constructor will only assign default values to properties that have it defined, +// but it doesn't guarantee that properties required by API are set +func NewCategoryWithDefaults() Category { + this := Category{} + return this +} + // AssertCategoryRequired checks if the required fields are not zero-ed func AssertCategoryRequired(obj Category) error { return nil diff --git a/samples/server/petstore/go-chi-server/go/model_colour.go b/samples/server/petstore/go-chi-server/go/model_colour.go index a77312b65379..26675c94f920 100644 --- a/samples/server/petstore/go-chi-server/go/model_colour.go +++ b/samples/server/petstore/go-chi-server/go/model_colour.go @@ -55,7 +55,6 @@ func NewColourFromValue(v string) (Colour, error) { } - // AssertColourRequired checks if the required fields are not zero-ed func AssertColourRequired(obj Colour) error { return nil diff --git a/samples/server/petstore/go-chi-server/go/model_gender.go b/samples/server/petstore/go-chi-server/go/model_gender.go index 410b61fd08d8..dc37635f550c 100644 --- a/samples/server/petstore/go-chi-server/go/model_gender.go +++ b/samples/server/petstore/go-chi-server/go/model_gender.go @@ -55,7 +55,6 @@ func NewGenderFromValue(v string) (Gender, error) { } - // AssertGenderRequired checks if the required fields are not zero-ed func AssertGenderRequired(obj Gender) error { return nil diff --git a/samples/server/petstore/go-chi-server/go/model_order.go b/samples/server/petstore/go-chi-server/go/model_order.go index 7e66f8115a7e..5d2700e5bab2 100644 --- a/samples/server/petstore/go-chi-server/go/model_order.go +++ b/samples/server/petstore/go-chi-server/go/model_order.go @@ -37,6 +37,15 @@ type Order struct { Comment *string `json:"comment"` } +// NewOrderWithDefaults instantiates a new Order object +// This constructor will only assign default values to properties that have it defined, +// but it doesn't guarantee that properties required by API are set +func NewOrderWithDefaults() Order { + this := Order{} + this.Complete = false + return this +} + // AssertOrderRequired checks if the required fields are not zero-ed func AssertOrderRequired(obj Order) error { elements := map[string]interface{}{ diff --git a/samples/server/petstore/go-chi-server/go/model_order_info.go b/samples/server/petstore/go-chi-server/go/model_order_info.go index 21f5d24946c2..fd9897fe6fb9 100644 --- a/samples/server/petstore/go-chi-server/go/model_order_info.go +++ b/samples/server/petstore/go-chi-server/go/model_order_info.go @@ -27,6 +27,14 @@ type OrderInfo struct { ShipDate time.Time `json:"shipDate,omitempty"` } +// NewOrderInfoWithDefaults instantiates a new OrderInfo object +// This constructor will only assign default values to properties that have it defined, +// but it doesn't guarantee that properties required by API are set +func NewOrderInfoWithDefaults() OrderInfo { + this := OrderInfo{} + return this +} + // AssertOrderInfoRequired checks if the required fields are not zero-ed func AssertOrderInfoRequired(obj OrderInfo) error { return nil diff --git a/samples/server/petstore/go-chi-server/go/model_pet.go b/samples/server/petstore/go-chi-server/go/model_pet.go index 48537769fb05..b76ac19bc0b1 100644 --- a/samples/server/petstore/go-chi-server/go/model_pet.go +++ b/samples/server/petstore/go-chi-server/go/model_pet.go @@ -31,6 +31,15 @@ type Pet struct { Status string `json:"status,omitempty"` } +// NewPetWithDefaults instantiates a new Pet object +// This constructor will only assign default values to properties that have it defined, +// but it doesn't guarantee that properties required by API are set +func NewPetWithDefaults() Pet { + this := Pet{} + *this.Category = NewCategoryWithDefaults() + return this +} + // AssertPetRequired checks if the required fields are not zero-ed func AssertPetRequired(obj Pet) error { elements := map[string]interface{}{ diff --git a/samples/server/petstore/go-chi-server/go/model_special_info.go b/samples/server/petstore/go-chi-server/go/model_special_info.go index 7be94aa62e33..e9211891340b 100644 --- a/samples/server/petstore/go-chi-server/go/model_special_info.go +++ b/samples/server/petstore/go-chi-server/go/model_special_info.go @@ -21,6 +21,14 @@ type SpecialInfo struct { Type string `json:"type,omitempty"` } +// NewSpecialInfoWithDefaults instantiates a new SpecialInfo object +// This constructor will only assign default values to properties that have it defined, +// but it doesn't guarantee that properties required by API are set +func NewSpecialInfoWithDefaults() SpecialInfo { + this := SpecialInfo{} + return this +} + // AssertSpecialInfoRequired checks if the required fields are not zero-ed func AssertSpecialInfoRequired(obj SpecialInfo) error { return nil diff --git a/samples/server/petstore/go-chi-server/go/model_species.go b/samples/server/petstore/go-chi-server/go/model_species.go index f236e29c48a1..d138f34d672e 100644 --- a/samples/server/petstore/go-chi-server/go/model_species.go +++ b/samples/server/petstore/go-chi-server/go/model_species.go @@ -64,7 +64,6 @@ func NewSpeciesFromValue(v string) (Species, error) { } - // AssertSpeciesRequired checks if the required fields are not zero-ed func AssertSpeciesRequired(obj Species) error { return nil diff --git a/samples/server/petstore/go-chi-server/go/model_tag.go b/samples/server/petstore/go-chi-server/go/model_tag.go index 88fb735f34cc..e06cba80f6bd 100644 --- a/samples/server/petstore/go-chi-server/go/model_tag.go +++ b/samples/server/petstore/go-chi-server/go/model_tag.go @@ -21,6 +21,14 @@ type Tag struct { Name string `json:"name,omitempty"` } +// NewTagWithDefaults instantiates a new Tag object +// This constructor will only assign default values to properties that have it defined, +// but it doesn't guarantee that properties required by API are set +func NewTagWithDefaults() Tag { + this := Tag{} + return this +} + // AssertTagRequired checks if the required fields are not zero-ed func AssertTagRequired(obj Tag) error { return nil diff --git a/samples/server/petstore/go-chi-server/go/model_user.go b/samples/server/petstore/go-chi-server/go/model_user.go index 973b8986d38e..17c382eadbdd 100644 --- a/samples/server/petstore/go-chi-server/go/model_user.go +++ b/samples/server/petstore/go-chi-server/go/model_user.go @@ -40,6 +40,14 @@ type User struct { DeepSliceMap [][]AnObject `json:"deepSliceMap,omitempty"` } +// NewUserWithDefaults instantiates a new User object +// This constructor will only assign default values to properties that have it defined, +// but it doesn't guarantee that properties required by API are set +func NewUserWithDefaults() User { + this := User{} + return this +} + // AssertUserRequired checks if the required fields are not zero-ed func AssertUserRequired(obj User) error { elements := map[string]interface{}{