diff --git a/modules/openapi-generator/src/main/resources/go-experimental/configuration.mustache b/modules/openapi-generator/src/main/resources/go-experimental/configuration.mustache index 1fd52f5e057..9d52ea1f528 100644 --- a/modules/openapi-generator/src/main/resources/go-experimental/configuration.mustache +++ b/modules/openapi-generator/src/main/resources/go-experimental/configuration.mustache @@ -2,7 +2,9 @@ package {{packageName}} import ( + "fmt" "net/http" + "strings" ) // contextKeys are used to identify the type of value in the context. @@ -41,6 +43,20 @@ type APIKey struct { Prefix string } +// ServerVariable stores the information about a server variable +type ServerVariable struct { + Description string + DefaultValue string + EnumValues []string +} + +// ServerConfiguration stores the information about a server +type ServerConfiguration struct { + Url string + Description string + Variables map[string]ServerVariable +} + // Configuration stores the configuration of the API client type Configuration struct { BasePath string `json:"basePath,omitempty"` @@ -49,6 +65,7 @@ type Configuration struct { DefaultHeader map[string]string `json:"defaultHeader,omitempty"` UserAgent string `json:"userAgent,omitempty"` Debug bool `json:"debug,omitempty"` + Servers []ServerConfiguration HTTPClient *http.Client } @@ -59,6 +76,38 @@ func NewConfiguration() *Configuration { DefaultHeader: make(map[string]string), UserAgent: "{{#httpUserAgent}}{{{.}}}{{/httpUserAgent}}{{^httpUserAgent}}OpenAPI-Generator/{{{packageVersion}}}/go{{/httpUserAgent}}", Debug: false, + {{#servers}} + {{#-first}} + Servers: []ServerConfiguration{{ + {{/-first}} + Url: "{{{url}}}", + Description: "{{{description}}}{{^description}}No description provided{{/description}}", + {{#variables}} + {{#-first}} + Variables: map[string]ServerVariable{ + {{/-first}} + "{{{name}}}": ServerVariable{ + Description: "{{{description}}}{{^description}}No description provided{{/description}}", + DefaultValue: "{{{defaultValue}}}", + {{#enumValues}} + {{#-first}} + EnumValues: []string{ + {{/-first}} + "{{{.}}}", + {{#-last}} + }, + {{/-last}} + {{/enumValues}} + }, + {{#-last}} + }, + {{/-last}} + {{/variables}} + }, + {{#-last}} + }, + {{/-last}} + {{/servers}} } return cfg } @@ -67,3 +116,31 @@ func NewConfiguration() *Configuration { func (c *Configuration) AddDefaultHeader(key string, value string) { c.DefaultHeader[key] = value } + +// ServerUrl returns URL based on server settings +func (c *Configuration) ServerUrl(index int, variables map[string]string) (string, error) { + if index < 0 || len(c.Servers) <= index { + return "", fmt.Errorf("Index %v out of range %v", index, len(c.Servers) - 1) + } + server := c.Servers[index] + url := server.Url + + // go through variables and replace placeholders + for name, variable := range server.Variables { + if value, ok := variables[name]; ok { + found := bool(len(variable.EnumValues) == 0) + for _, enumValue := range variable.EnumValues { + if value == enumValue { + found = true + } + } + if !found { + return "", fmt.Errorf("The variable %s in the server URL has invalid value %v. Must be %v", name, value, variable.EnumValues) + } + url = strings.Replace(url, "{"+name+"}", value, -1) + } else { + url = strings.Replace(url, "{"+name+"}", variable.DefaultValue, -1) + } + } + return url, nil +} diff --git a/modules/openapi-generator/src/main/resources/go/configuration.mustache b/modules/openapi-generator/src/main/resources/go/configuration.mustache index 17bc0aab306..e6fa96d2057 100644 --- a/modules/openapi-generator/src/main/resources/go/configuration.mustache +++ b/modules/openapi-generator/src/main/resources/go/configuration.mustache @@ -2,7 +2,9 @@ package {{packageName}} import ( + "fmt" "net/http" + "strings" ) // contextKeys are used to identify the type of value in the context. @@ -41,6 +43,20 @@ type APIKey struct { Prefix string } +// ServerVariable stores the information about a server variable +type ServerVariable struct { + Description string + DefaultValue string + EnumValues []string +} + +// ServerConfiguration stores the information about a server +type ServerConfiguration struct { + Url string + Description string + Variables map[string]ServerVariable +} + // Configuration stores the configuration of the API client type Configuration struct { BasePath string `json:"basePath,omitempty"` @@ -49,6 +65,7 @@ type Configuration struct { DefaultHeader map[string]string `json:"defaultHeader,omitempty"` UserAgent string `json:"userAgent,omitempty"` Debug bool `json:"debug,omitempty"` + Servers []ServerConfiguration HTTPClient *http.Client } @@ -59,6 +76,38 @@ func NewConfiguration() *Configuration { DefaultHeader: make(map[string]string), UserAgent: "{{#httpUserAgent}}{{{.}}}{{/httpUserAgent}}{{^httpUserAgent}}OpenAPI-Generator/{{{packageVersion}}}/go{{/httpUserAgent}}", Debug: false, + {{#servers}} + {{#-first}} + Servers: []ServerConfiguration{{ + {{/-first}} + Url: "{{{url}}}", + Description: "{{{description}}}{{^description}}No description provided{{/description}}", + {{#variables}} + {{#-first}} + Variables: map[string]ServerVariable{ + {{/-first}} + "{{{name}}}": ServerVariable{ + Description: "{{{description}}}{{^description}}No description provided{{/description}}", + DefaultValue: "{{{defaultValue}}}", + {{#enumValues}} + {{#-first}} + EnumValues: []string{ + {{/-first}} + "{{{.}}}", + {{#-last}} + }, + {{/-last}} + {{/enumValues}} + }, + {{#-last}} + }, + {{/-last}} + {{/variables}} + }, + {{#-last}} + }, + {{/-last}} + {{/servers}} } return cfg } @@ -67,3 +116,31 @@ func NewConfiguration() *Configuration { func (c *Configuration) AddDefaultHeader(key string, value string) { c.DefaultHeader[key] = value } + +// ServerUrl returns URL based on server settings +func (c *Configuration) ServerUrl(index int, variables map[string]string) (string, error) { + if index < 0 || len(c.Servers) <= index { + return "", fmt.Errorf("Index %v out of range %v", index, len(c.Servers) - 1) + } + server := c.Servers[index] + url := server.Url + + // go through variables and replace placeholders + for name, variable := range server.Variables { + if value, ok := variables[name]; ok { + found := bool(len(variable.EnumValues) == 0) + for _, enumValue := range variable.EnumValues { + if value == enumValue { + found = true + } + } + if !found { + return "", fmt.Errorf("The variable %s in the server URL has invalid value %v. Must be %v", name, value, variable.EnumValues) + } + url = strings.Replace(url, "{"+name+"}", value, -1) + } else { + url = strings.Replace(url, "{"+name+"}", variable.DefaultValue, -1) + } + } + return url, nil +} diff --git a/samples/client/petstore/go-experimental/go-petstore/configuration.go b/samples/client/petstore/go-experimental/go-petstore/configuration.go index 15ed61df374..5a4a915d1d3 100644 --- a/samples/client/petstore/go-experimental/go-petstore/configuration.go +++ b/samples/client/petstore/go-experimental/go-petstore/configuration.go @@ -10,7 +10,9 @@ package petstore import ( + "fmt" "net/http" + "strings" ) // contextKeys are used to identify the type of value in the context. @@ -49,6 +51,20 @@ type APIKey struct { Prefix string } +// ServerVariable stores the information about a server variable +type ServerVariable struct { + Description string + DefaultValue string + EnumValues []string +} + +// ServerConfiguration stores the information about a server +type ServerConfiguration struct { + Url string + Description string + Variables map[string]ServerVariable +} + // Configuration stores the configuration of the API client type Configuration struct { BasePath string `json:"basePath,omitempty"` @@ -57,6 +73,7 @@ type Configuration struct { DefaultHeader map[string]string `json:"defaultHeader,omitempty"` UserAgent string `json:"userAgent,omitempty"` Debug bool `json:"debug,omitempty"` + Servers []ServerConfiguration HTTPClient *http.Client } @@ -67,6 +84,11 @@ func NewConfiguration() *Configuration { DefaultHeader: make(map[string]string), UserAgent: "OpenAPI-Generator/1.0.0/go", Debug: false, + Servers: []ServerConfiguration{{ + Url: "http://petstore.swagger.io:80/v2", + Description: "No description provided", + }, + }, } return cfg } @@ -75,3 +97,31 @@ func NewConfiguration() *Configuration { func (c *Configuration) AddDefaultHeader(key string, value string) { c.DefaultHeader[key] = value } + +// ServerUrl returns URL based on server settings +func (c *Configuration) ServerUrl(index int, variables map[string]string) (string, error) { + if index < 0 || len(c.Servers) <= index { + return "", fmt.Errorf("Index %v out of range %v", index, len(c.Servers) - 1) + } + server := c.Servers[index] + url := server.Url + + // go through variables and replace placeholders + for name, variable := range server.Variables { + if value, ok := variables[name]; ok { + found := bool(len(variable.EnumValues) == 0) + for _, enumValue := range variable.EnumValues { + if value == enumValue { + found = true + } + } + if !found { + return "", fmt.Errorf("The variable %s in the server URL has invalid value %v. Must be %v", name, value, variable.EnumValues) + } + url = strings.Replace(url, "{"+name+"}", value, -1) + } else { + url = strings.Replace(url, "{"+name+"}", variable.DefaultValue, -1) + } + } + return url, nil +} diff --git a/samples/client/petstore/go/go-petstore-withXml/configuration.go b/samples/client/petstore/go/go-petstore-withXml/configuration.go index 9618bd142cb..59728f054c7 100644 --- a/samples/client/petstore/go/go-petstore-withXml/configuration.go +++ b/samples/client/petstore/go/go-petstore-withXml/configuration.go @@ -11,7 +11,9 @@ package petstore import ( + "fmt" "net/http" + "strings" ) // contextKeys are used to identify the type of value in the context. @@ -50,6 +52,20 @@ type APIKey struct { Prefix string } +// ServerVariable stores the information about a server variable +type ServerVariable struct { + Description string + DefaultValue string + EnumValues []string +} + +// ServerConfiguration stores the information about a server +type ServerConfiguration struct { + Url string + Description string + Variables map[string]ServerVariable +} + // Configuration stores the configuration of the API client type Configuration struct { BasePath string `json:"basePath,omitempty"` @@ -58,6 +74,7 @@ type Configuration struct { DefaultHeader map[string]string `json:"defaultHeader,omitempty"` UserAgent string `json:"userAgent,omitempty"` Debug bool `json:"debug,omitempty"` + Servers []ServerConfiguration HTTPClient *http.Client } @@ -68,6 +85,11 @@ func NewConfiguration() *Configuration { DefaultHeader: make(map[string]string), UserAgent: "OpenAPI-Generator/1.0.0/go", Debug: false, + Servers: []ServerConfiguration{{ + Url: "http://petstore.swagger.io:80/v2", + Description: "No description provided", + }, + }, } return cfg } @@ -76,3 +98,31 @@ func NewConfiguration() *Configuration { func (c *Configuration) AddDefaultHeader(key string, value string) { c.DefaultHeader[key] = value } + +// ServerUrl returns URL based on server settings +func (c *Configuration) ServerUrl(index int, variables map[string]string) (string, error) { + if index < 0 || len(c.Servers) <= index { + return "", fmt.Errorf("Index %v out of range %v", index, len(c.Servers) - 1) + } + server := c.Servers[index] + url := server.Url + + // go through variables and replace placeholders + for name, variable := range server.Variables { + if value, ok := variables[name]; ok { + found := bool(len(variable.EnumValues) == 0) + for _, enumValue := range variable.EnumValues { + if value == enumValue { + found = true + } + } + if !found { + return "", fmt.Errorf("The variable %s in the server URL has invalid value %v. Must be %v", name, value, variable.EnumValues) + } + url = strings.Replace(url, "{"+name+"}", value, -1) + } else { + url = strings.Replace(url, "{"+name+"}", variable.DefaultValue, -1) + } + } + return url, nil +} diff --git a/samples/client/petstore/go/go-petstore/configuration.go b/samples/client/petstore/go/go-petstore/configuration.go index 40bb03ddcb5..34f8f242922 100644 --- a/samples/client/petstore/go/go-petstore/configuration.go +++ b/samples/client/petstore/go/go-petstore/configuration.go @@ -10,7 +10,9 @@ package petstore import ( + "fmt" "net/http" + "strings" ) // contextKeys are used to identify the type of value in the context. @@ -49,6 +51,20 @@ type APIKey struct { Prefix string } +// ServerVariable stores the information about a server variable +type ServerVariable struct { + Description string + DefaultValue string + EnumValues []string +} + +// ServerConfiguration stores the information about a server +type ServerConfiguration struct { + Url string + Description string + Variables map[string]ServerVariable +} + // Configuration stores the configuration of the API client type Configuration struct { BasePath string `json:"basePath,omitempty"` @@ -57,6 +73,7 @@ type Configuration struct { DefaultHeader map[string]string `json:"defaultHeader,omitempty"` UserAgent string `json:"userAgent,omitempty"` Debug bool `json:"debug,omitempty"` + Servers []ServerConfiguration HTTPClient *http.Client } @@ -67,6 +84,11 @@ func NewConfiguration() *Configuration { DefaultHeader: make(map[string]string), UserAgent: "OpenAPI-Generator/1.0.0/go", Debug: false, + Servers: []ServerConfiguration{{ + Url: "http://petstore.swagger.io:80/v2", + Description: "No description provided", + }, + }, } return cfg } @@ -75,3 +97,31 @@ func NewConfiguration() *Configuration { func (c *Configuration) AddDefaultHeader(key string, value string) { c.DefaultHeader[key] = value } + +// ServerUrl returns URL based on server settings +func (c *Configuration) ServerUrl(index int, variables map[string]string) (string, error) { + if index < 0 || len(c.Servers) <= index { + return "", fmt.Errorf("Index %v out of range %v", index, len(c.Servers) - 1) + } + server := c.Servers[index] + url := server.Url + + // go through variables and replace placeholders + for name, variable := range server.Variables { + if value, ok := variables[name]; ok { + found := bool(len(variable.EnumValues) == 0) + for _, enumValue := range variable.EnumValues { + if value == enumValue { + found = true + } + } + if !found { + return "", fmt.Errorf("The variable %s in the server URL has invalid value %v. Must be %v", name, value, variable.EnumValues) + } + url = strings.Replace(url, "{"+name+"}", value, -1) + } else { + url = strings.Replace(url, "{"+name+"}", variable.DefaultValue, -1) + } + } + return url, nil +} diff --git a/samples/openapi3/client/petstore/go-experimental/go-petstore/.openapi-generator/VERSION b/samples/openapi3/client/petstore/go-experimental/go-petstore/.openapi-generator/VERSION index e4955748d3e..58592f031f6 100644 --- a/samples/openapi3/client/petstore/go-experimental/go-petstore/.openapi-generator/VERSION +++ b/samples/openapi3/client/petstore/go-experimental/go-petstore/.openapi-generator/VERSION @@ -1 +1 @@ -4.2.2-SNAPSHOT \ No newline at end of file +4.2.3-SNAPSHOT \ No newline at end of file diff --git a/samples/openapi3/client/petstore/go-experimental/go-petstore/configuration.go b/samples/openapi3/client/petstore/go-experimental/go-petstore/configuration.go index 54c8ae0f91d..04cac069b34 100644 --- a/samples/openapi3/client/petstore/go-experimental/go-petstore/configuration.go +++ b/samples/openapi3/client/petstore/go-experimental/go-petstore/configuration.go @@ -10,7 +10,9 @@ package openapi import ( + "fmt" "net/http" + "strings" ) // contextKeys are used to identify the type of value in the context. @@ -49,6 +51,20 @@ type APIKey struct { Prefix string } +// ServerVariable stores the information about a server variable +type ServerVariable struct { + Description string + DefaultValue string + EnumValues []string +} + +// ServerConfiguration stores the information about a server +type ServerConfiguration struct { + Url string + Description string + Variables map[string]ServerVariable +} + // Configuration stores the configuration of the API client type Configuration struct { BasePath string `json:"basePath,omitempty"` @@ -57,6 +73,7 @@ type Configuration struct { DefaultHeader map[string]string `json:"defaultHeader,omitempty"` UserAgent string `json:"userAgent,omitempty"` Debug bool `json:"debug,omitempty"` + Servers []ServerConfiguration HTTPClient *http.Client } @@ -67,6 +84,43 @@ func NewConfiguration() *Configuration { DefaultHeader: make(map[string]string), UserAgent: "OpenAPI-Generator/1.0.0/go", Debug: false, + Servers: []ServerConfiguration{{ + Url: "http://{server}.swagger.io:{port}/v2", + Description: "petstore server", + Variables: map[string]ServerVariable{ + "server": ServerVariable{ + Description: "No description provided", + DefaultValue: "petstore", + EnumValues: []string{ + "petstore", + "qa-petstore", + "dev-petstore", + }, + }, + "port": ServerVariable{ + Description: "No description provided", + DefaultValue: "80", + EnumValues: []string{ + "80", + "8080", + }, + }, + }, + }, + Url: "https://localhost:8080/{version}", + Description: "The local server", + Variables: map[string]ServerVariable{ + "version": ServerVariable{ + Description: "No description provided", + DefaultValue: "v2", + EnumValues: []string{ + "v1", + "v2", + }, + }, + }, + }, + }, } return cfg } @@ -75,3 +129,31 @@ func NewConfiguration() *Configuration { func (c *Configuration) AddDefaultHeader(key string, value string) { c.DefaultHeader[key] = value } + +// ServerUrl returns URL based on server settings +func (c *Configuration) ServerUrl(index int, variables map[string]string) (string, error) { + if index < 0 || len(c.Servers) <= index { + return "", fmt.Errorf("Index %v out of range %v", index, len(c.Servers) - 1) + } + server := c.Servers[index] + url := server.Url + + // go through variables and replace placeholders + for name, variable := range server.Variables { + if value, ok := variables[name]; ok { + found := bool(len(variable.EnumValues) == 0) + for _, enumValue := range variable.EnumValues { + if value == enumValue { + found = true + } + } + if !found { + return "", fmt.Errorf("The variable %s in the server URL has invalid value %v. Must be %v", name, value, variable.EnumValues) + } + url = strings.Replace(url, "{"+name+"}", value, -1) + } else { + url = strings.Replace(url, "{"+name+"}", variable.DefaultValue, -1) + } + } + return url, nil +} diff --git a/samples/openapi3/client/petstore/go/go-petstore/configuration.go b/samples/openapi3/client/petstore/go/go-petstore/configuration.go index 40bb03ddcb5..333de81350d 100644 --- a/samples/openapi3/client/petstore/go/go-petstore/configuration.go +++ b/samples/openapi3/client/petstore/go/go-petstore/configuration.go @@ -10,7 +10,9 @@ package petstore import ( + "fmt" "net/http" + "strings" ) // contextKeys are used to identify the type of value in the context. @@ -49,6 +51,20 @@ type APIKey struct { Prefix string } +// ServerVariable stores the information about a server variable +type ServerVariable struct { + Description string + DefaultValue string + EnumValues []string +} + +// ServerConfiguration stores the information about a server +type ServerConfiguration struct { + Url string + Description string + Variables map[string]ServerVariable +} + // Configuration stores the configuration of the API client type Configuration struct { BasePath string `json:"basePath,omitempty"` @@ -57,6 +73,7 @@ type Configuration struct { DefaultHeader map[string]string `json:"defaultHeader,omitempty"` UserAgent string `json:"userAgent,omitempty"` Debug bool `json:"debug,omitempty"` + Servers []ServerConfiguration HTTPClient *http.Client } @@ -67,6 +84,43 @@ func NewConfiguration() *Configuration { DefaultHeader: make(map[string]string), UserAgent: "OpenAPI-Generator/1.0.0/go", Debug: false, + Servers: []ServerConfiguration{{ + Url: "http://{server}.swagger.io:{port}/v2", + Description: "petstore server", + Variables: map[string]ServerVariable{ + "server": ServerVariable{ + Description: "No description provided", + DefaultValue: "petstore", + EnumValues: []string{ + "petstore", + "qa-petstore", + "dev-petstore", + }, + }, + "port": ServerVariable{ + Description: "No description provided", + DefaultValue: "80", + EnumValues: []string{ + "80", + "8080", + }, + }, + }, + }, + Url: "https://localhost:8080/{version}", + Description: "The local server", + Variables: map[string]ServerVariable{ + "version": ServerVariable{ + Description: "No description provided", + DefaultValue: "v2", + EnumValues: []string{ + "v1", + "v2", + }, + }, + }, + }, + }, } return cfg } @@ -75,3 +129,31 @@ func NewConfiguration() *Configuration { func (c *Configuration) AddDefaultHeader(key string, value string) { c.DefaultHeader[key] = value } + +// ServerUrl returns URL based on server settings +func (c *Configuration) ServerUrl(index int, variables map[string]string) (string, error) { + if index < 0 || len(c.Servers) <= index { + return "", fmt.Errorf("Index %v out of range %v", index, len(c.Servers) - 1) + } + server := c.Servers[index] + url := server.Url + + // go through variables and replace placeholders + for name, variable := range server.Variables { + if value, ok := variables[name]; ok { + found := bool(len(variable.EnumValues) == 0) + for _, enumValue := range variable.EnumValues { + if value == enumValue { + found = true + } + } + if !found { + return "", fmt.Errorf("The variable %s in the server URL has invalid value %v. Must be %v", name, value, variable.EnumValues) + } + url = strings.Replace(url, "{"+name+"}", value, -1) + } else { + url = strings.Replace(url, "{"+name+"}", variable.DefaultValue, -1) + } + } + return url, nil +}