diff --git a/modules/openapi-generator/src/main/resources/go/api.mustache b/modules/openapi-generator/src/main/resources/go/api.mustache index 2b04681ac91a..848c586b192e 100644 --- a/modules/openapi-generator/src/main/resources/go/api.mustache +++ b/modules/openapi-generator/src/main/resources/go/api.mustache @@ -132,11 +132,11 @@ func (a *{{{classname}}}Service) {{{nickname}}}(ctx context.Context{{#hasParams} {{#hasQueryParams}} {{#queryParams}} {{#required}} - localVarQueryParams.Add("{{baseName}}", parameterToString({{paramName}}, "{{#collectionFormat}}{{collectionFormat}}{{/collectionFormat}}")) + localVarQueryParams = addQueryParams(localVarQueryParams, "{{baseName}}", {{paramName}}, "{{#collectionFormat}}{{collectionFormat}}{{/collectionFormat}}") {{/required}} {{^required}} if localVarOptionals != nil && localVarOptionals.{{vendorExtensions.x-exportParamName}}.IsSet() { - localVarQueryParams.Add("{{baseName}}", parameterToString(localVarOptionals.{{vendorExtensions.x-exportParamName}}.Value(), "{{#collectionFormat}}{{collectionFormat}}{{/collectionFormat}}")) + localVarQueryParams = addQueryParams(localVarQueryParams, "{{baseName}}", localVarOptionals.{{vendorExtensions.x-exportParamName}}.Value(), "{{#collectionFormat}}{{collectionFormat}}{{/collectionFormat}}") } {{/required}} {{/queryParams}} diff --git a/modules/openapi-generator/src/main/resources/go/client.mustache b/modules/openapi-generator/src/main/resources/go/client.mustache index 1c4241ff043b..326a03fb2cd0 100644 --- a/modules/openapi-generator/src/main/resources/go/client.mustache +++ b/modules/openapi-generator/src/main/resources/go/client.mustache @@ -111,6 +111,21 @@ func contains(haystack []string, needle string) bool { return false } +// addQueryParams adds a query params depending on collectionFormat +// if collectionFormat == "multi" it will add multiple entries to query string e.g. +// ["q1", "q2, "q3"] will be converted to `paramName=q1¶mName=q2¶mName=q3` +func addQueryParams(q url.Values, paramName string, paramValue interface{}, collectionFormat string) url.Values { + if collectionFormat == "multi" && reflect.TypeOf(paramValue).Kind() == reflect.Slice { + paramSlice := reflect.ValueOf(paramValue) + + for i := 0; i < paramSlice.Len(); i++ { + q.Add(paramName, parameterToString(paramSlice.Index(i).Interface(), "")) + } + } + q.Add(paramName, parameterToString(paramValue, collectionFormat)) + return q +} + // Verify optional parameters are of the correct type. func typeCheckParameter(obj interface{}, expected string, name string) error { // Make sure there is an object.