From d72b501e40aa2aafef204a22c432a7c769046b9a Mon Sep 17 00:00:00 2001 From: Guo Huang Date: Sat, 16 Apr 2016 16:43:04 -0700 Subject: [PATCH] added required parameters validation for Go --- .../src/main/resources/go/api.mustache | 9 +++- samples/client/petstore/go/swagger/pet_api.go | 40 +++++++++++++---- .../client/petstore/go/swagger/store_api.go | 16 +++++-- .../client/petstore/go/swagger/user_api.go | 44 +++++++++++++++---- 4 files changed, 88 insertions(+), 21 deletions(-) diff --git a/modules/swagger-codegen/src/main/resources/go/api.mustache b/modules/swagger-codegen/src/main/resources/go/api.mustache index f08ba380ec0..35ed984929d 100644 --- a/modules/swagger-codegen/src/main/resources/go/api.mustache +++ b/modules/swagger-codegen/src/main/resources/go/api.mustache @@ -39,7 +39,14 @@ func New{{classname}}WithBasePath(basePath string) *{{classname}}{ {{/allParams}} * @return {{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}void{{/returnType}} */ func (a {{classname}}) {{nickname}} ({{#allParams}}{{paramName}} {{{dataType}}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) ({{#returnType}}{{{returnType}}}, {{/returnType}}error) { - + {{#allParams}} + {{#required}} + // verify the required parameter '{{paramName}}' is set + if &{{paramName}} == nil { + return {{#returnType}}*new({{{returnType}}}), {{/returnType}}errors.New("Missing required parameter '{{paramName}}' when calling {{classname}}->{{operationId}}") + } + {{/required}} + {{/allParams}} _sling := sling.New().{{httpMethod}}(a.Configuration.BasePath) {{#authMethods}}// authentication ({{name}}) required diff --git a/samples/client/petstore/go/swagger/pet_api.go b/samples/client/petstore/go/swagger/pet_api.go index cdaed35ce12..461df7449f3 100644 --- a/samples/client/petstore/go/swagger/pet_api.go +++ b/samples/client/petstore/go/swagger/pet_api.go @@ -36,7 +36,10 @@ func NewPetApiWithBasePath(basePath string) *PetApi{ * @return void */ func (a PetApi) AddPet (body Pet) (error) { - + // verify the required parameter 'body' is set + if &body == nil { + return errors.New("Missing required parameter 'body' when calling PetApi->AddPet") + } _sling := sling.New().Post(a.Configuration.BasePath) // authentication (petstore_auth) required @@ -124,7 +127,10 @@ func (a PetApi) AddPet (body Pet) (error) { * @return void */ func (a PetApi) DeletePet (petId int64, apiKey string) (error) { - + // verify the required parameter 'petId' is set + if &petId == nil { + return errors.New("Missing required parameter 'petId' when calling PetApi->DeletePet") + } _sling := sling.New().Delete(a.Configuration.BasePath) // authentication (petstore_auth) required @@ -210,7 +216,10 @@ func (a PetApi) DeletePet (petId int64, apiKey string) (error) { * @return []Pet */ func (a PetApi) FindPetsByStatus (status []string) ([]Pet, error) { - + // verify the required parameter 'status' is set + if &status == nil { + return *new([]Pet), errors.New("Missing required parameter 'status' when calling PetApi->FindPetsByStatus") + } _sling := sling.New().Get(a.Configuration.BasePath) // authentication (petstore_auth) required @@ -297,7 +306,10 @@ func (a PetApi) FindPetsByStatus (status []string) ([]Pet, error) { * @return []Pet */ func (a PetApi) FindPetsByTags (tags []string) ([]Pet, error) { - + // verify the required parameter 'tags' is set + if &tags == nil { + return *new([]Pet), errors.New("Missing required parameter 'tags' when calling PetApi->FindPetsByTags") + } _sling := sling.New().Get(a.Configuration.BasePath) // authentication (petstore_auth) required @@ -384,7 +396,10 @@ func (a PetApi) FindPetsByTags (tags []string) ([]Pet, error) { * @return Pet */ func (a PetApi) GetPetById (petId int64) (Pet, error) { - + // verify the required parameter 'petId' is set + if &petId == nil { + return *new(Pet), errors.New("Missing required parameter 'petId' when calling PetApi->GetPetById") + } _sling := sling.New().Get(a.Configuration.BasePath) // authentication (api_key) required @@ -467,7 +482,10 @@ func (a PetApi) GetPetById (petId int64) (Pet, error) { * @return void */ func (a PetApi) UpdatePet (body Pet) (error) { - + // verify the required parameter 'body' is set + if &body == nil { + return errors.New("Missing required parameter 'body' when calling PetApi->UpdatePet") + } _sling := sling.New().Put(a.Configuration.BasePath) // authentication (petstore_auth) required @@ -556,7 +574,10 @@ func (a PetApi) UpdatePet (body Pet) (error) { * @return void */ func (a PetApi) UpdatePetWithForm (petId int64, name string, status string) (error) { - + // verify the required parameter 'petId' is set + if &petId == nil { + return errors.New("Missing required parameter 'petId' when calling PetApi->UpdatePetWithForm") + } _sling := sling.New().Post(a.Configuration.BasePath) // authentication (petstore_auth) required @@ -648,7 +669,10 @@ func (a PetApi) UpdatePetWithForm (petId int64, name string, status string) (err * @return ApiResponse */ func (a PetApi) UploadFile (petId int64, additionalMetadata string, file *os.File) (ApiResponse, error) { - + // verify the required parameter 'petId' is set + if &petId == nil { + return *new(ApiResponse), errors.New("Missing required parameter 'petId' when calling PetApi->UploadFile") + } _sling := sling.New().Post(a.Configuration.BasePath) // authentication (petstore_auth) required diff --git a/samples/client/petstore/go/swagger/store_api.go b/samples/client/petstore/go/swagger/store_api.go index 3fe6f3f8f2b..2fa59c1efd1 100644 --- a/samples/client/petstore/go/swagger/store_api.go +++ b/samples/client/petstore/go/swagger/store_api.go @@ -35,7 +35,10 @@ func NewStoreApiWithBasePath(basePath string) *StoreApi{ * @return void */ func (a StoreApi) DeleteOrder (orderId string) (error) { - + // verify the required parameter 'orderId' is set + if &orderId == nil { + return errors.New("Missing required parameter 'orderId' when calling StoreApi->DeleteOrder") + } _sling := sling.New().Delete(a.Configuration.BasePath) @@ -112,7 +115,6 @@ func (a StoreApi) DeleteOrder (orderId string) (error) { * @return map[string]int32 */ func (a StoreApi) GetInventory () (map[string]int32, error) { - _sling := sling.New().Get(a.Configuration.BasePath) // authentication (api_key) required @@ -193,7 +195,10 @@ func (a StoreApi) GetInventory () (map[string]int32, error) { * @return Order */ func (a StoreApi) GetOrderById (orderId int64) (Order, error) { - + // verify the required parameter 'orderId' is set + if &orderId == nil { + return *new(Order), errors.New("Missing required parameter 'orderId' when calling StoreApi->GetOrderById") + } _sling := sling.New().Get(a.Configuration.BasePath) @@ -271,7 +276,10 @@ func (a StoreApi) GetOrderById (orderId int64) (Order, error) { * @return Order */ func (a StoreApi) PlaceOrder (body Order) (Order, error) { - + // verify the required parameter 'body' is set + if &body == nil { + return *new(Order), errors.New("Missing required parameter 'body' when calling StoreApi->PlaceOrder") + } _sling := sling.New().Post(a.Configuration.BasePath) diff --git a/samples/client/petstore/go/swagger/user_api.go b/samples/client/petstore/go/swagger/user_api.go index ca95f8042fc..b6639d9e65f 100644 --- a/samples/client/petstore/go/swagger/user_api.go +++ b/samples/client/petstore/go/swagger/user_api.go @@ -35,7 +35,10 @@ func NewUserApiWithBasePath(basePath string) *UserApi{ * @return void */ func (a UserApi) CreateUser (body User) (error) { - + // verify the required parameter 'body' is set + if &body == nil { + return errors.New("Missing required parameter 'body' when calling UserApi->CreateUser") + } _sling := sling.New().Post(a.Configuration.BasePath) @@ -114,7 +117,10 @@ func (a UserApi) CreateUser (body User) (error) { * @return void */ func (a UserApi) CreateUsersWithArrayInput (body []User) (error) { - + // verify the required parameter 'body' is set + if &body == nil { + return errors.New("Missing required parameter 'body' when calling UserApi->CreateUsersWithArrayInput") + } _sling := sling.New().Post(a.Configuration.BasePath) @@ -193,7 +199,10 @@ func (a UserApi) CreateUsersWithArrayInput (body []User) (error) { * @return void */ func (a UserApi) CreateUsersWithListInput (body []User) (error) { - + // verify the required parameter 'body' is set + if &body == nil { + return errors.New("Missing required parameter 'body' when calling UserApi->CreateUsersWithListInput") + } _sling := sling.New().Post(a.Configuration.BasePath) @@ -272,7 +281,10 @@ func (a UserApi) CreateUsersWithListInput (body []User) (error) { * @return void */ func (a UserApi) DeleteUser (username string) (error) { - + // verify the required parameter 'username' is set + if &username == nil { + return errors.New("Missing required parameter 'username' when calling UserApi->DeleteUser") + } _sling := sling.New().Delete(a.Configuration.BasePath) @@ -350,7 +362,10 @@ func (a UserApi) DeleteUser (username string) (error) { * @return User */ func (a UserApi) GetUserByName (username string) (User, error) { - + // verify the required parameter 'username' is set + if &username == nil { + return *new(User), errors.New("Missing required parameter 'username' when calling UserApi->GetUserByName") + } _sling := sling.New().Get(a.Configuration.BasePath) @@ -429,7 +444,14 @@ func (a UserApi) GetUserByName (username string) (User, error) { * @return string */ func (a UserApi) LoginUser (username string, password string) (string, error) { - + // verify the required parameter 'username' is set + if &username == nil { + return *new(string), errors.New("Missing required parameter 'username' when calling UserApi->LoginUser") + } + // verify the required parameter 'password' is set + if &password == nil { + return *new(string), errors.New("Missing required parameter 'password' when calling UserApi->LoginUser") + } _sling := sling.New().Get(a.Configuration.BasePath) @@ -510,7 +532,6 @@ Password string `url:"password,omitempty"` * @return void */ func (a UserApi) LogoutUser () (error) { - _sling := sling.New().Get(a.Configuration.BasePath) @@ -588,7 +609,14 @@ func (a UserApi) LogoutUser () (error) { * @return void */ func (a UserApi) UpdateUser (username string, body User) (error) { - + // verify the required parameter 'username' is set + if &username == nil { + return errors.New("Missing required parameter 'username' when calling UserApi->UpdateUser") + } + // verify the required parameter 'body' is set + if &body == nil { + return errors.New("Missing required parameter 'body' when calling UserApi->UpdateUser") + } _sling := sling.New().Put(a.Configuration.BasePath)