diff --git a/README.md b/README.md index 1c7ecb5758d..7c1bf779d32 100644 --- a/README.md +++ b/README.md @@ -565,7 +565,7 @@ You can also use the codegen to generate a server for a couple different framewo ``` java -jar modules/swagger-codegen-cli/target/swagger-codegen-cli.jar generate \ -i http://petstore.swagger.io/v2/swagger.json \ - -l nodejs \ + -l nodejs-server \ -o samples/server/petstore/nodejs ``` diff --git a/modules/swagger-codegen/src/main/resources/go/api.mustache b/modules/swagger-codegen/src/main/resources/go/api.mustache index 50fd16bd244..a7c09eae4cd 100644 --- a/modules/swagger-codegen/src/main/resources/go/api.mustache +++ b/modules/swagger-codegen/src/main/resources/go/api.mustache @@ -65,6 +65,11 @@ func (a {{classname}}) {{nickname}} ({{#allParams}}{{paramName}} {{{dataType}}}{ _sling = _sling.Path(path) + // add default headers if any + for key := range a.Configuration.DefaultHeader { + _sling = _sling.Set(key, a.Configuration.DefaultHeader[key]) + } + {{#hasQueryParams}} type QueryParams struct { {{#queryParams}}{{paramName}} {{dataType}} `url:"{{baseName}},omitempty"` {{/queryParams}} diff --git a/modules/swagger-codegen/src/main/resources/go/configuration.mustache b/modules/swagger-codegen/src/main/resources/go/configuration.mustache index d78ba9b5e01..abc3b436d04 100644 --- a/modules/swagger-codegen/src/main/resources/go/configuration.mustache +++ b/modules/swagger-codegen/src/main/resources/go/configuration.mustache @@ -16,16 +16,23 @@ type Configuration struct { Host string `json:"host,omitempty"` Scheme string `json:"scheme,omitempty"` AccessToken string `json:"accessToken,omitempty"` + DefaultHeader map[string]string `json:"defaultHeader,omitempty"` } func NewConfiguration() *Configuration { + defaultHeader := make(map[string]string) return &Configuration{ BasePath: "{{basePath}}", UserName: "", Debug: false, + DefaultHeader: defaultHeader, } } func (c *Configuration) GetBasicAuthEncodedString() string { return base64.StdEncoding.EncodeToString([]byte(c.UserName + ":" + c.Password)) +} + +func (c *Configuration) AddDefaultHeader(key string, value string) { + c.DefaultHeader[key] = value } \ No newline at end of file diff --git a/modules/swagger-codegen/src/main/resources/go/model.mustache b/modules/swagger-codegen/src/main/resources/go/model.mustache index 9045bea1ed2..4102299c5e9 100644 --- a/modules/swagger-codegen/src/main/resources/go/model.mustache +++ b/modules/swagger-codegen/src/main/resources/go/model.mustache @@ -7,8 +7,10 @@ import ( ) {{#model}} +{{#description}}// {{{description}}}{{/description}} type {{classname}} struct { {{#vars}} + {{#description}}// {{{description}}}{{/description}} {{name}} {{{datatype}}} `json:"{{baseName}},omitempty"` {{/vars}} } diff --git a/samples/client/petstore/go/swagger/ApiResponse.go b/samples/client/petstore/go/swagger/ApiResponse.go index 196af29c15d..abb7971189d 100644 --- a/samples/client/petstore/go/swagger/ApiResponse.go +++ b/samples/client/petstore/go/swagger/ApiResponse.go @@ -3,8 +3,12 @@ package swagger import ( ) + type ApiResponse struct { + Code int32 `json:"code,omitempty"` + Type_ string `json:"type,omitempty"` + Message string `json:"message,omitempty"` } diff --git a/samples/client/petstore/go/swagger/Category.go b/samples/client/petstore/go/swagger/Category.go index 1739a41f1eb..1853dfe7239 100644 --- a/samples/client/petstore/go/swagger/Category.go +++ b/samples/client/petstore/go/swagger/Category.go @@ -3,7 +3,10 @@ package swagger import ( ) + type Category struct { + Id int64 `json:"id,omitempty"` + Name string `json:"name,omitempty"` } diff --git a/samples/client/petstore/go/swagger/Configuration.go b/samples/client/petstore/go/swagger/Configuration.go index 1763f4dbd38..f99c572ee1e 100644 --- a/samples/client/petstore/go/swagger/Configuration.go +++ b/samples/client/petstore/go/swagger/Configuration.go @@ -16,16 +16,23 @@ type Configuration struct { Host string `json:"host,omitempty"` Scheme string `json:"scheme,omitempty"` AccessToken string `json:"accessToken,omitempty"` + DefaultHeader map[string]string `json:"defaultHeader,omitempty"` } func NewConfiguration() *Configuration { + defaultHeader := make(map[string]string) return &Configuration{ BasePath: "http://petstore.swagger.io/v2", UserName: "", Debug: false, + DefaultHeader: defaultHeader, } } func (c *Configuration) GetBasicAuthEncodedString() string { return base64.StdEncoding.EncodeToString([]byte(c.UserName + ":" + c.Password)) +} + +func (c *Configuration) AddDefaultHeader(key string, value string) { + c.DefaultHeader[key] = value } \ No newline at end of file diff --git a/samples/client/petstore/go/swagger/Order.go b/samples/client/petstore/go/swagger/Order.go index 8b9ddfeb212..a199bcc857d 100644 --- a/samples/client/petstore/go/swagger/Order.go +++ b/samples/client/petstore/go/swagger/Order.go @@ -4,11 +4,18 @@ import ( "time" ) + type Order struct { + Id int64 `json:"id,omitempty"` + PetId int64 `json:"petId,omitempty"` + Quantity int32 `json:"quantity,omitempty"` + ShipDate time.Time `json:"shipDate,omitempty"` + // Order Status Status string `json:"status,omitempty"` + Complete bool `json:"complete,omitempty"` } diff --git a/samples/client/petstore/go/swagger/Pet.go b/samples/client/petstore/go/swagger/Pet.go index d9faa1cb90f..32b9e6d97fc 100644 --- a/samples/client/petstore/go/swagger/Pet.go +++ b/samples/client/petstore/go/swagger/Pet.go @@ -3,11 +3,18 @@ package swagger import ( ) + type Pet struct { + Id int64 `json:"id,omitempty"` + Category Category `json:"category,omitempty"` + Name string `json:"name,omitempty"` + PhotoUrls []string `json:"photoUrls,omitempty"` + Tags []Tag `json:"tags,omitempty"` + // pet status in the store Status string `json:"status,omitempty"` } diff --git a/samples/client/petstore/go/swagger/PetApi.go b/samples/client/petstore/go/swagger/PetApi.go index 986c01d7d23..4858ee14347 100644 --- a/samples/client/petstore/go/swagger/PetApi.go +++ b/samples/client/petstore/go/swagger/PetApi.go @@ -51,6 +51,11 @@ func (a PetApi) AddPet (body Pet) (error) { _sling = _sling.Path(path) + // add default headers if any + for key := range a.Configuration.DefaultHeader { + _sling = _sling.Set(key, a.Configuration.DefaultHeader[key]) + } + // accept header accepts := []string { "application/xml", "application/json" } for key := range accepts { @@ -119,6 +124,11 @@ func (a PetApi) DeletePet (petId int64, apiKey string) (error) { _sling = _sling.Path(path) + // add default headers if any + for key := range a.Configuration.DefaultHeader { + _sling = _sling.Set(key, a.Configuration.DefaultHeader[key]) + } + // accept header accepts := []string { "application/xml", "application/json" } for key := range accepts { @@ -185,6 +195,11 @@ func (a PetApi) FindPetsByStatus (status []string) ([]Pet, error) { _sling = _sling.Path(path) + // add default headers if any + for key := range a.Configuration.DefaultHeader { + _sling = _sling.Set(key, a.Configuration.DefaultHeader[key]) + } + type QueryParams struct { status []string `url:"status,omitempty"` } @@ -253,6 +268,11 @@ func (a PetApi) FindPetsByTags (tags []string) ([]Pet, error) { _sling = _sling.Path(path) + // add default headers if any + for key := range a.Configuration.DefaultHeader { + _sling = _sling.Set(key, a.Configuration.DefaultHeader[key]) + } + type QueryParams struct { tags []string `url:"tags,omitempty"` } @@ -318,6 +338,11 @@ func (a PetApi) GetPetById (petId int64) (Pet, error) { _sling = _sling.Path(path) + // add default headers if any + for key := range a.Configuration.DefaultHeader { + _sling = _sling.Set(key, a.Configuration.DefaultHeader[key]) + } + // accept header accepts := []string { "application/xml", "application/json" } for key := range accepts { @@ -382,6 +407,11 @@ func (a PetApi) UpdatePet (body Pet) (error) { _sling = _sling.Path(path) + // add default headers if any + for key := range a.Configuration.DefaultHeader { + _sling = _sling.Set(key, a.Configuration.DefaultHeader[key]) + } + // accept header accepts := []string { "application/xml", "application/json" } for key := range accepts { @@ -451,6 +481,11 @@ func (a PetApi) UpdatePetWithForm (petId int64, name string, status string) (err _sling = _sling.Path(path) + // add default headers if any + for key := range a.Configuration.DefaultHeader { + _sling = _sling.Set(key, a.Configuration.DefaultHeader[key]) + } + // accept header accepts := []string { "application/xml", "application/json" } for key := range accepts { @@ -523,6 +558,11 @@ func (a PetApi) UploadFile (petId int64, additionalMetadata string, file *os.Fil _sling = _sling.Path(path) + // add default headers if any + for key := range a.Configuration.DefaultHeader { + _sling = _sling.Set(key, a.Configuration.DefaultHeader[key]) + } + // accept header accepts := []string { "application/json" } for key := range accepts { diff --git a/samples/client/petstore/go/swagger/StoreApi.go b/samples/client/petstore/go/swagger/StoreApi.go index 6da5a5c35e2..ed255e00834 100644 --- a/samples/client/petstore/go/swagger/StoreApi.go +++ b/samples/client/petstore/go/swagger/StoreApi.go @@ -46,6 +46,11 @@ func (a StoreApi) DeleteOrder (orderId string) (error) { _sling = _sling.Path(path) + // add default headers if any + for key := range a.Configuration.DefaultHeader { + _sling = _sling.Set(key, a.Configuration.DefaultHeader[key]) + } + // accept header accepts := []string { "application/xml", "application/json" } for key := range accepts { @@ -105,6 +110,11 @@ func (a StoreApi) GetInventory () (map[string]int32, error) { _sling = _sling.Path(path) + // add default headers if any + for key := range a.Configuration.DefaultHeader { + _sling = _sling.Set(key, a.Configuration.DefaultHeader[key]) + } + // accept header accepts := []string { "application/json" } for key := range accepts { @@ -165,6 +175,11 @@ func (a StoreApi) GetOrderById (orderId int64) (Order, error) { _sling = _sling.Path(path) + // add default headers if any + for key := range a.Configuration.DefaultHeader { + _sling = _sling.Set(key, a.Configuration.DefaultHeader[key]) + } + // accept header accepts := []string { "application/xml", "application/json" } for key := range accepts { @@ -224,6 +239,11 @@ func (a StoreApi) PlaceOrder (body Order) (Order, error) { _sling = _sling.Path(path) + // add default headers if any + for key := range a.Configuration.DefaultHeader { + _sling = _sling.Set(key, a.Configuration.DefaultHeader[key]) + } + // accept header accepts := []string { "application/xml", "application/json" } for key := range accepts { diff --git a/samples/client/petstore/go/swagger/Tag.go b/samples/client/petstore/go/swagger/Tag.go index 77b55099c4a..7347106078a 100644 --- a/samples/client/petstore/go/swagger/Tag.go +++ b/samples/client/petstore/go/swagger/Tag.go @@ -3,7 +3,10 @@ package swagger import ( ) + type Tag struct { + Id int64 `json:"id,omitempty"` + Name string `json:"name,omitempty"` } diff --git a/samples/client/petstore/go/swagger/User.go b/samples/client/petstore/go/swagger/User.go index 03f0821252b..8f459db4674 100644 --- a/samples/client/petstore/go/swagger/User.go +++ b/samples/client/petstore/go/swagger/User.go @@ -3,13 +3,22 @@ package swagger import ( ) + type User struct { + Id int64 `json:"id,omitempty"` + Username string `json:"username,omitempty"` + FirstName string `json:"firstName,omitempty"` + LastName string `json:"lastName,omitempty"` + Email string `json:"email,omitempty"` + Password string `json:"password,omitempty"` + Phone string `json:"phone,omitempty"` + // User Status UserStatus int32 `json:"userStatus,omitempty"` } diff --git a/samples/client/petstore/go/swagger/UserApi.go b/samples/client/petstore/go/swagger/UserApi.go index 2148336c228..ab464a99395 100644 --- a/samples/client/petstore/go/swagger/UserApi.go +++ b/samples/client/petstore/go/swagger/UserApi.go @@ -45,6 +45,11 @@ func (a UserApi) CreateUser (body User) (error) { _sling = _sling.Path(path) + // add default headers if any + for key := range a.Configuration.DefaultHeader { + _sling = _sling.Set(key, a.Configuration.DefaultHeader[key]) + } + // accept header accepts := []string { "application/xml", "application/json" } for key := range accepts { @@ -106,6 +111,11 @@ func (a UserApi) CreateUsersWithArrayInput (body []User) (error) { _sling = _sling.Path(path) + // add default headers if any + for key := range a.Configuration.DefaultHeader { + _sling = _sling.Set(key, a.Configuration.DefaultHeader[key]) + } + // accept header accepts := []string { "application/xml", "application/json" } for key := range accepts { @@ -167,6 +177,11 @@ func (a UserApi) CreateUsersWithListInput (body []User) (error) { _sling = _sling.Path(path) + // add default headers if any + for key := range a.Configuration.DefaultHeader { + _sling = _sling.Set(key, a.Configuration.DefaultHeader[key]) + } + // accept header accepts := []string { "application/xml", "application/json" } for key := range accepts { @@ -229,6 +244,11 @@ func (a UserApi) DeleteUser (username string) (error) { _sling = _sling.Path(path) + // add default headers if any + for key := range a.Configuration.DefaultHeader { + _sling = _sling.Set(key, a.Configuration.DefaultHeader[key]) + } + // accept header accepts := []string { "application/xml", "application/json" } for key := range accepts { @@ -289,6 +309,11 @@ func (a UserApi) GetUserByName (username string) (User, error) { _sling = _sling.Path(path) + // add default headers if any + for key := range a.Configuration.DefaultHeader { + _sling = _sling.Set(key, a.Configuration.DefaultHeader[key]) + } + // accept header accepts := []string { "application/xml", "application/json" } for key := range accepts { @@ -349,6 +374,11 @@ func (a UserApi) LoginUser (username string, password string) (string, error) { _sling = _sling.Path(path) + // add default headers if any + for key := range a.Configuration.DefaultHeader { + _sling = _sling.Set(key, a.Configuration.DefaultHeader[key]) + } + type QueryParams struct { username string `url:"username,omitempty"` password string `url:"password,omitempty"` @@ -412,6 +442,11 @@ func (a UserApi) LogoutUser () (error) { _sling = _sling.Path(path) + // add default headers if any + for key := range a.Configuration.DefaultHeader { + _sling = _sling.Set(key, a.Configuration.DefaultHeader[key]) + } + // accept header accepts := []string { "application/xml", "application/json" } for key := range accepts { @@ -473,6 +508,11 @@ func (a UserApi) UpdateUser (username string, body User) (error) { _sling = _sling.Path(path) + // add default headers if any + for key := range a.Configuration.DefaultHeader { + _sling = _sling.Set(key, a.Configuration.DefaultHeader[key]) + } + // accept header accepts := []string { "application/xml", "application/json" } for key := range accepts {