From cda3517891f23349271a9d14bfa3006c5136c175 Mon Sep 17 00:00:00 2001 From: William Cheng Date: Tue, 7 Feb 2023 21:45:24 +0800 Subject: [PATCH] [Go] properly set header parameters on api clients (#14637) * [Go] properly set header parameters on api clients * update samples * update samples --------- Co-authored-by: Paul Sokolik --- .../src/main/resources/go/api.mustache | 20 ++--- .../src/main/resources/go/client.mustache | 17 ++-- .../petstore/go/go-petstore/api_fake.go | 72 ++++++++-------- .../client/petstore/go/go-petstore/api_pet.go | 14 +-- .../petstore/go/go-petstore/api_user.go | 4 +- .../client/petstore/go/go-petstore/client.go | 17 ++-- .../x-auth-id-alias/go-experimental/client.go | 17 ++-- .../petstore/go/go-petstore/api_fake.go | 86 +++++++++---------- .../client/petstore/go/go-petstore/api_pet.go | 14 +-- .../petstore/go/go-petstore/api_user.go | 4 +- .../client/petstore/go/go-petstore/client.go | 17 ++-- 11 files changed, 143 insertions(+), 139 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/go/api.mustache b/modules/openapi-generator/src/main/resources/go/api.mustache index bc4429d011b3..37ba74967616 100644 --- a/modules/openapi-generator/src/main/resources/go/api.mustache +++ b/modules/openapi-generator/src/main/resources/go/api.mustache @@ -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}} @@ -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}} @@ -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}} @@ -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}} @@ -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}} diff --git a/modules/openapi-generator/src/main/resources/go/client.mustache b/modules/openapi-generator/src/main/resources/go/client.mustache index e5214a957918..eeb1197939d8 100644 --- a/modules/openapi-generator/src/main/resources/go/client.mustache +++ b/modules/openapi-generator/src/main/resources/go/client.mustache @@ -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) { @@ -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" @@ -183,7 +184,7 @@ func parameterAddToQuery(queryParams interface{}, keyPrefix string, obj interfac var lenIndValue = indValue.Len() for i:=0;i