swagger-api / swagger-api/swagger-codegen
[GO] optional array parameterToString question
Nobody has claimed this yet.
- Dominant language
- Mustache
- Stars
- 17.8k
- Forks
- 6k
- PR merge metrics
- No merged PRs in 30d
Description
Description
i have set the optional query array params Ids like: []string{"a1","b2"}.
i expect the query string : Ids=a1&Ids=b2 , but got Ids=a1b2.
Swagger-codegen version
swagger-codegen version
3.0.42
Swagger declaration file content or url
- name: Ids
in: query
description: id array
required: false
style: form
explode: true
schema:
type: array
items:
type: string
Command line used for generation
swagger-codegen generate -i ./api/swagger.yaml -l go -o ./
Steps to reproduce
type DefaultPostOpts struct {
Ids optional.Interface
}
func (a *DefaultApiService) ApiPost(ctx context.Context, localVarOptionals *DefaultPostOpts) (Response, *http.Response, error) {
....
if localVarOptionals != nil && localVarOptionals.Ids.IsSet() {
localVarQueryParams.Add("Ids", parameterToString(localVarOptionals.Ids.Value(), "multi"))
}
....
}
client.DefaultApi.ApiPost(ctx, &DefaultPostOpts{
Ids: optional.NewInterface([]string{"a1","b2"}),
})
Related issues/PRs
Suggest a fix/enhancement
if localVarOptionals != nil && localVarOptionals.Ids.IsSet() {
appendQueryParams(localVarQueryParams,localVarOptionals.Ids.Value(),"Ids")
}
func appendQueryParams(queryParams url.Values, obj interface{}, key string) {
value := reflect.ValueOf(obj)
kind := value.Kind()
switch kind {
case reflect.Slice, reflect.Array:
for i := 0; i < value.Len(); i++ {
element := value.Index(i)
queryParams.Add(key, fmt.Sprintf("%v", element.Interface()))
}
case reflect.Map:
for _, mapKey := range value.MapKeys() {
value := value.MapIndex(mapKey)
queryParams.Add(key, fmt.Sprintf("%v", value.Interface()))
}
default:
queryParams.Add(key, fmt.Sprintf("%v", obj))
}
}
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the generated Go client's parameterToString call and the localVarQueryParams construction shown in the reproduction, using the provided Swagger YAML and generation command as inputs. Trace how optional.Interface containing []string is serialized. Done means the generated request represents the two values as repeated Ids query parameters, with regression coverage if the relevant test location can be found.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- go
- Domain
- api
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100