Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion languages/golang/templates/api.mustache
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{{! This template was customized to use the GenericOpenAPIError from the core module. See https://github.com/OpenAPITools/openapi-generator/blob/v7.25.0/modules/openapi-generator/src/main/resources/go/api.mustache for the original template }}
{{! This template was customized to use the GenericOpenAPIError from the core module. See https://github.com/OpenAPITools/openapi-generator/blob/v7.27.0/modules/openapi-generator/src/main/resources/go/api.mustache for the original template }}
{{>partial_header}}
package {{packageName}}

Expand Down Expand Up @@ -282,11 +282,33 @@ func (a *{{{classname}}}Service) {{nickname}}Execute(r {{#structPrefix}}{{&class
}
{{#headerParams}}
{{#required}}
{{#vendorExtensions.x-go-json-header}}
if jsonHeaderValue, err := parameterToJSONHeaderValue(r.{{paramName}}); err != nil {
{{! TEMPLATE CUSTOMIZATION - BEGIN - don't return http response - would require closing after each SDK API call to prevent resource leaks }}
return {{#returnType}}localVarReturnValue, {{/returnType}} reportError("failed to serialize {{{baseName}}} header: %v", err)
{{! TEMPLATE CUSTOMIZATION - END - don't return http response - would require closing after each SDK API call to prevent resource leaks }}
} else {
localVarHeaderParams["{{{baseName}}}"] = jsonHeaderValue
}
{{/vendorExtensions.x-go-json-header}}
{{^vendorExtensions.x-go-json-header}}
parameterAddToHeaderOrQuery(localVarHeaderParams, "{{{baseName}}}", r.{{paramName}}, "{{style}}", "{{collectionFormat}}")
{{/vendorExtensions.x-go-json-header}}
{{/required}}
{{^required}}
if r.{{paramName}} != nil {
{{#vendorExtensions.x-go-json-header}}
if jsonHeaderValue, err := parameterToJSONHeaderValue(r.{{paramName}}); err != nil {
{{! TEMPLATE CUSTOMIZATION - BEGIN - don't return http response - would require closing after each SDK API call to prevent resource leaks }}
return {{#returnType}}localVarReturnValue, {{/returnType}} reportError("failed to serialize {{{baseName}}} header: %v", err)
{{! TEMPLATE CUSTOMIZATION - END - don't return http response - would require closing after each SDK API call to prevent resource leaks }}
} else {
localVarHeaderParams["{{{baseName}}}"] = jsonHeaderValue
}
{{/vendorExtensions.x-go-json-header}}
{{^vendorExtensions.x-go-json-header}}
parameterAddToHeaderOrQuery(localVarHeaderParams, "{{{baseName}}}", r.{{paramName}}, "{{style}}", "{{collectionFormat}}")
{{/vendorExtensions.x-go-json-header}}
}
{{/required}}
{{/headerParams}}
Expand Down
61 changes: 58 additions & 3 deletions languages/golang/templates/client.mustache
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{{! This template was customized to support the custom configuration options from the core module. See https://github.com/OpenAPITools/openapi-generator/blob/v7.25.0/modules/openapi-generator/src/main/resources/go/client.mustache for the original template }}
{{! This template was customized to support the custom configuration options from the core module. See https://github.com/OpenAPITools/openapi-generator/blob/v7.27.0/modules/openapi-generator/src/main/resources/go/client.mustache for the original template }}
{{>partial_header}}
package {{packageName}}

Expand Down Expand Up @@ -192,6 +192,17 @@ func parameterValueToString( obj interface{}, key string ) string {
}
return fmt.Sprintf("%v", dataMap[key])
}
{{#x-go-has-json-header}}

// parameterToJSONHeaderValue encodes a content: application/json header.
func parameterToJSONHeaderValue(obj interface{}) (string, error) {
encoded, err := json.Marshal(obj)
if err != nil {
return "", err
}
return string(encoded), nil
}
{{/x-go-has-json-header}}

// parameterAddToHeaderOrQuery adds the provided object to the request header or url query
// supporting deep object syntax
Expand Down Expand Up @@ -228,10 +239,14 @@ func parameterAddToHeaderOrQuery(headerOrQueryParams interface{}, keyPrefix stri
for i:=0;i<lenIndValue;i++ {
var arrayValue = indValue.Index(i)
var keyPrefixForCollectionType = keyPrefix
var styleForElement = style
if style == "deepObject" {
keyPrefixForCollectionType = keyPrefix + "[" + strconv.Itoa(i) + "]"
} else if style == "form" {
// only the parameter's own map is flattened; a map inside an array keeps its bracketed path
styleForElement = ""
}
parameterAddToHeaderOrQuery(headerOrQueryParams, keyPrefixForCollectionType, arrayValue.Interface(), style, collectionType)
parameterAddToHeaderOrQuery(headerOrQueryParams, keyPrefixForCollectionType, arrayValue.Interface(), styleForElement, collectionType)
}
return

Expand All @@ -243,13 +258,36 @@ func parameterAddToHeaderOrQuery(headerOrQueryParams interface{}, keyPrefix stri
iter := indValue.MapRange()
for iter.Next() {
k,v := iter.Key(), iter.Value()
parameterAddToHeaderOrQuery(headerOrQueryParams, fmt.Sprintf("%s[%s]", keyPrefix, k.String()), v.Interface(), style, collectionType)
var keyPrefixForMapEntry = fmt.Sprintf("%s[%s]", keyPrefix, k.String())
var styleForMapEntry = style
if style == "form" {
// form style: one query parameter per entry, keyed by the property name; anything nested keeps its bracketed path
keyPrefixForMapEntry = k.String()
styleForMapEntry = ""
// a nil entry, or a nil item of a list entry, is left out rather than sent as "null"
entry, ok := parameterValueIndirect(v)
if !ok {
continue
}
if entry.Kind() == reflect.Slice {
for i := 0; i < entry.Len(); i++ {
if element, ok := parameterValueIndirect(entry.Index(i)); ok {
parameterAddToHeaderOrQuery(headerOrQueryParams, keyPrefixForMapEntry, element.Interface(), styleForMapEntry, collectionType)
}
}
continue
}
}
parameterAddToHeaderOrQuery(headerOrQueryParams, keyPrefixForMapEntry, v.Interface(), styleForMapEntry, collectionType)
}
return

case reflect.Interface:
fallthrough
case reflect.Ptr:
if v.IsNil() {
return
}
parameterAddToHeaderOrQuery(headerOrQueryParams, keyPrefix, v.Elem().Interface(), style, collectionType)
return

Expand Down Expand Up @@ -284,6 +322,18 @@ func parameterAddToHeaderOrQuery(headerOrQueryParams interface{}, keyPrefix stri
}
}

// parameterValueIndirect unwraps interfaces and pointers down to the value they hold,
// reporting false when that value is nil
func parameterValueIndirect(v reflect.Value) (reflect.Value, bool) {
for v.Kind() == reflect.Interface || v.Kind() == reflect.Ptr {
if v.IsNil() {
return v, false
}
v = v.Elem()
}
return v, v.IsValid()
}

// helper for converting interface{} parameters to json strings
func parameterToJson(obj interface{}) (string, error) {
jsonBuf, err := json.Marshal(obj)
Expand Down Expand Up @@ -455,7 +505,12 @@ func (c *APIClient) prepareRequest(
if len(headerParams) > 0 {
headers := http.Header{}
for h, v := range headerParams {
{{#useHttpHeaderSet}}
headers.Set(h, v)
{{/useHttpHeaderSet}}
{{^useHttpHeaderSet}}
headers[h] = []string{v}
{{/useHttpHeaderSet}}
}
localVarRequest.Header = headers
}
Expand Down
2 changes: 1 addition & 1 deletion languages/golang/templates/configuration.mustache
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{{! This template was ENTIRELY overwritten to support the custom configuration type and options from the STACKIT SDK core module. See https://github.com/OpenAPITools/openapi-generator/blob/v7.25.0/modules/openapi-generator/src/main/resources/go/configuration.mustache for the original template }}
{{! This template was ENTIRELY overwritten to support the custom configuration type and options from the STACKIT SDK core module. See https://github.com/OpenAPITools/openapi-generator/blob/v7.27.0/modules/openapi-generator/src/main/resources/go/configuration.mustache for the original template }}
{{>partial_header}}
package {{packageName}}

Expand Down
2 changes: 1 addition & 1 deletion scripts/generate-sdk/generate-sdk.sh
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ case "${LANGUAGE}" in
go)
# When the GENERATOR_VERSION changes, migrate also the templates in templates/go
# Renovate: datasource=github-tags depName=OpenAPITools/openapi-generator versioning=semver
GENERATOR_VERSION="v7.25.0"
GENERATOR_VERSION="v7.27.0"
;;
python)
# When the GENERATOR_VERSION changes, migrate also the templates in templates/python
Expand Down
Loading