Skip to content

Commit

Permalink
[Go] properly set header parameters on api clients (OpenAPITools#14637)
Browse files Browse the repository at this point in the history
* [Go] properly set header parameters on api clients

* update samples

* update samples

---------

Co-authored-by: Paul Sokolik <[email protected]>
  • Loading branch information
wing328 and sokolikp authored Feb 7, 2023
1 parent d90bdcb commit cda3517
Show file tree
Hide file tree
Showing 11 changed files with 143 additions and 139 deletions.
20 changes: 10 additions & 10 deletions modules/openapi-generator/src/main/resources/go/api.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -189,15 +189,15 @@ func (a *{{{classname}}}Service) {{nickname}}Execute(r {{#structPrefix}}{{&class
if reflect.TypeOf(t).Kind() == reflect.Slice {
s := reflect.ValueOf(t)
for i := 0; i < s.Len(); i++ {
parameterAddToQuery(localVarQueryParams, "{{baseName}}", s.Index(i), "{{collectionFormat}}")
parameterAddToHeaderOrQuery(localVarQueryParams, "{{baseName}}", s.Index(i), "{{collectionFormat}}")
}
} else {
parameterAddToQuery(localVarQueryParams, "{{baseName}}", t, "{{collectionFormat}}")
parameterAddToHeaderOrQuery(localVarQueryParams, "{{baseName}}", t, "{{collectionFormat}}")
}
}
{{/isCollectionFormatMulti}}
{{^isCollectionFormatMulti}}
parameterAddToQuery(localVarQueryParams, "{{baseName}}", r.{{paramName}}, "{{collectionFormat}}")
parameterAddToHeaderOrQuery(localVarQueryParams, "{{baseName}}", r.{{paramName}}, "{{collectionFormat}}")
{{/isCollectionFormatMulti}}
{{/required}}
{{^required}}
Expand All @@ -207,14 +207,14 @@ func (a *{{{classname}}}Service) {{nickname}}Execute(r {{#structPrefix}}{{&class
if reflect.TypeOf(t).Kind() == reflect.Slice {
s := reflect.ValueOf(t)
for i := 0; i < s.Len(); i++ {
parameterAddToQuery(localVarQueryParams, "{{baseName}}", s.Index(i), "{{collectionFormat}}")
parameterAddToHeaderOrQuery(localVarQueryParams, "{{baseName}}", s.Index(i), "{{collectionFormat}}")
}
} else {
parameterAddToQuery(localVarQueryParams, "{{baseName}}", t, "{{collectionFormat}}")
parameterAddToHeaderOrQuery(localVarQueryParams, "{{baseName}}", t, "{{collectionFormat}}")
}
{{/isCollectionFormatMulti}}
{{^isCollectionFormatMulti}}
parameterAddToQuery(localVarQueryParams, "{{baseName}}", r.{{paramName}}, "{{collectionFormat}}")
parameterAddToHeaderOrQuery(localVarQueryParams, "{{baseName}}", r.{{paramName}}, "{{collectionFormat}}")
{{/isCollectionFormatMulti}}
}
{{/required}}
Expand Down Expand Up @@ -242,11 +242,11 @@ func (a *{{{classname}}}Service) {{nickname}}Execute(r {{#structPrefix}}{{&class
}
{{#headerParams}}
{{#required}}
parameterAddToQuery(localVarQueryParams, "{{baseName}}", r.{{paramName}}, "{{collectionFormat}}")
parameterAddToHeaderOrQuery(localVarHeaderParams, "{{baseName}}", r.{{paramName}}, "{{collectionFormat}}")
{{/required}}
{{^required}}
if r.{{paramName}} != nil {
parameterAddToQuery(localVarQueryParams, "{{baseName}}", r.{{paramName}}, "{{collectionFormat}}")
parameterAddToHeaderOrQuery(localVarHeaderParams, "{{baseName}}", r.{{paramName}}, "{{collectionFormat}}")
}
{{/required}}
{{/headerParams}}
Expand All @@ -272,7 +272,7 @@ func (a *{{{classname}}}Service) {{nickname}}Execute(r {{#structPrefix}}{{&class
{{/isFile}}
{{^isFile}}
{{#required}}
parameterAddToQuery(localVarFormParams, "{{baseName}}", r.{{paramName}}, "{{collectionFormat}}")
parameterAddToHeaderOrQuery(localVarFormParams, "{{baseName}}", r.{{paramName}}, "{{collectionFormat}}")
{{/required}}
{{^required}}
{{#isModel}}
Expand All @@ -286,7 +286,7 @@ func (a *{{{classname}}}Service) {{nickname}}Execute(r {{#structPrefix}}{{&class
{{/isModel}}
{{^isModel}}
if r.{{paramName}} != nil {
parameterAddToQuery(localVarFormParams, "{{baseName}}", r.{{paramName}}, "{{collectionFormat}}")
parameterAddToHeaderOrQuery(localVarFormParams, "{{baseName}}", r.{{paramName}}, "{{collectionFormat}}")
}
{{/isModel}}
{{/required}}
Expand Down
17 changes: 9 additions & 8 deletions modules/openapi-generator/src/main/resources/go/client.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,9 @@ func parameterValueToString( obj interface{}, key string ) string {
return fmt.Sprintf("%v", dataMap[key])
}

// parameterAddToQuery adds the provided object to the url query supporting deep object syntax
func parameterAddToQuery(queryParams interface{}, keyPrefix string, obj interface{}, collectionType string) {
// parameterAddToHeaderOrQuery adds the provided object to the request header or url query
// supporting deep object syntax
func parameterAddToHeaderOrQuery(headerOrQueryParams interface{}, keyPrefix string, obj interface{}, collectionType string) {
var v = reflect.ValueOf(obj)
var value = ""
if v == reflect.ValueOf(nil) {
Expand All @@ -167,11 +168,11 @@ func parameterAddToQuery(queryParams interface{}, keyPrefix string, obj interfac
if err != nil {
return
}
parameterAddToQuery(queryParams, keyPrefix, dataMap, collectionType)
parameterAddToHeaderOrQuery(headerOrQueryParams, keyPrefix, dataMap, collectionType)
return
}
if t, ok := obj.(time.Time); ok {
parameterAddToQuery(queryParams, keyPrefix, t.Format(time.RFC3339), collectionType)
parameterAddToHeaderOrQuery(headerOrQueryParams, keyPrefix, t.Format(time.RFC3339), collectionType)
return
}
value = v.Type().String() + " value"
Expand All @@ -183,7 +184,7 @@ func parameterAddToQuery(queryParams interface{}, keyPrefix string, obj interfac
var lenIndValue = indValue.Len()
for i:=0;i<lenIndValue;i++ {
var arrayValue = indValue.Index(i)
parameterAddToQuery(queryParams, keyPrefix, arrayValue.Interface(), collectionType)
parameterAddToHeaderOrQuery(headerOrQueryParams, keyPrefix, arrayValue.Interface(), collectionType)
}
return

Expand All @@ -195,14 +196,14 @@ func parameterAddToQuery(queryParams interface{}, keyPrefix string, obj interfac
iter := indValue.MapRange()
for iter.Next() {
k,v := iter.Key(), iter.Value()
parameterAddToQuery(queryParams, fmt.Sprintf("%s[%s]", keyPrefix, k.String()), v.Interface(), collectionType)
parameterAddToHeaderOrQuery(headerOrQueryParams, fmt.Sprintf("%s[%s]", keyPrefix, k.String()), v.Interface(), collectionType)
}
return

case reflect.Interface:
fallthrough
case reflect.Ptr:
parameterAddToQuery(queryParams, keyPrefix, v.Elem().Interface(), collectionType)
parameterAddToHeaderOrQuery(headerOrQueryParams, keyPrefix, v.Elem().Interface(), collectionType)
return

case reflect.Int, reflect.Int8, reflect.Int16,
Expand All @@ -222,7 +223,7 @@ func parameterAddToQuery(queryParams interface{}, keyPrefix string, obj interfac
}
}

switch valuesMap := queryParams.(type) {
switch valuesMap := headerOrQueryParams.(type) {
case url.Values:
if collectionType == "csv" && valuesMap.Get(keyPrefix) != "" {
valuesMap.Set(keyPrefix, valuesMap.Get(keyPrefix) + "," + value)
Expand Down
72 changes: 36 additions & 36 deletions samples/client/petstore/go/go-petstore/api_fake.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 7 additions & 7 deletions samples/client/petstore/go/go-petstore/api_pet.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions samples/client/petstore/go/go-petstore/api_user.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit cda3517

Please sign in to comment.